From 7bbe2c73e06cb76c0d769b759be463dacb6badfa Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Fri, 25 Apr 2025 18:38:54 +0800 Subject: [PATCH] add AbilityRunningRecord unittest cases for ability_runtime Signed-off-by: zhangzezhong --- .../ability_connect_manager_first_test.cpp | 520 ++++++++++++++++++ .../ability_running_record_test.cpp | 96 ++++ .../app_preloader_test/app_preloader_test.cpp | 155 ++++++ .../include/bundle_mgr_helper.h | 60 +- 4 files changed, 826 insertions(+), 5 deletions(-) diff --git a/test/unittest/ability_connect_manager_first_test/ability_connect_manager_first_test.cpp b/test/unittest/ability_connect_manager_first_test/ability_connect_manager_first_test.cpp index 7ff56ff210..831df63efd 100644 --- a/test/unittest/ability_connect_manager_first_test/ability_connect_manager_first_test.cpp +++ b/test/unittest/ability_connect_manager_first_test/ability_connect_manager_first_test.cpp @@ -425,5 +425,525 @@ HWTEST_F(AbilityConnectManagerTest, HandleActiveAbility_003, TestSize.Level1) connectManager->HandleActiveAbility(abilityRecord, connectRecord); TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_003 end"); } + +/* + * Feature: AbilityConnectManager + * Function: HandleRestartResidentTask + */ +HWTEST_F(AbilityConnectManagerTest, HandleRestartResidentTask_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandleRestartResidentTask_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::string deviceName = "device"; + std::string abilityName = "TestAbility"; + std::string appName = "hiservcie"; + std::string bundleName = "com.example.bundle"; + std::string moduleName = "entry"; + abilityRequest_ = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName); + serviceRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_); + + connectManager->HandleRestartResidentTask(abilityRequest_); + ASSERT_TRUE(connectManager->restartResidentTaskList_.empty()); + TAG_LOGI(AAFwkTag::TEST, "HandleRestartResidentTask_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: HandleRestartResidentTask + */ +HWTEST_F(AbilityConnectManagerTest, HandleRestartResidentTask_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandleRestartResidentTask_002 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + connectManager->restartResidentTaskList_.clear(); + std::string deviceName = "device"; + std::string abilityName = "TestAbility"; + std::string appName = "hiservcie"; + std::string bundleName = "com.example.bundle"; + std::string moduleName = "entry"; + abilityRequest_ = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName); + serviceRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_); + + connectManager->HandleRestartResidentTask(abilityRequest_); + ASSERT_EQ(connectManager->restartResidentTaskList_.size(), 0); + TAG_LOGI(AAFwkTag::TEST, "HandleRestartResidentTask_002 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: IsNeedToRestart + */ +HWTEST_F(AbilityConnectManagerTest, IsNeedToRestart_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "IsNeedToRestart_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = serviceRecord_; + bool result = connectManager->IsNeedToRestart(abilityRecord, "com.example.bundle", "TestAbility"); + ASSERT_TRUE(result); + TAG_LOGI(AAFwkTag::TEST, "IsNeedToRestart_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: HandleUIExtensionDied + */ +HWTEST_F(AbilityConnectManagerTest, HandleUIExtensionDied_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandleUIExtensionDied_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = serviceRecord_; + connectManager->HandleUIExtensionDied(abilityRecord); + EXPECT_EQ(connectManager->uiExtensionMap_.size(), 0); + TAG_LOGI(AAFwkTag::TEST, "HandleUIExtensionDied_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: GetUIExtensionSessionInfo + */ +HWTEST_F(AbilityConnectManagerTest, GetUIExtensionSessionInfo_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionSessionInfo_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = serviceRecord_; + AbilityRequest abilityRequest; + sptr callerToken = abilityRecord->GetToken(); + UIExtensionSessionInfo info; + + int32_t result = connectManager->GetUIExtensionSessionInfo(callerToken, info); + ASSERT_EQ(result, OHOS::ERR_INVALID_VALUE); + TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionSessionInfo_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: GetUIExtensionSessionInfo + */ +HWTEST_F(AbilityConnectManagerTest, GetUIExtensionSessionInfo_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionSessionInfo_002 start"); + std::shared_ptr connectManager = std::make_shared(0); + ASSERT_NE(connectManager, nullptr); + + sptr nullToken = nullptr; + UIExtensionSessionInfo sessionInfo; + std::string deviceName = "device"; + std::string abilityName = "ServiceAbility"; + std::string appName = "hiservcie"; + std::string bundleName = "com.ix.hiservcie"; + std::string moduleName = "entry"; + abilityRequest_ = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName); + serviceRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_); + std::shared_ptr abilityRecord = serviceRecord_; + sptr token = abilityRecord->GetToken(); + + connectManager->uiExtensionAbilityRecordMgr_ = nullptr; + int32_t ret = connectManager->GetUIExtensionSessionInfo(token, sessionInfo); + EXPECT_NE(ret, OHOS::ERR_INVALID_VALUE); + TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionSessionInfo_002 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: IsStrictMode + */ +HWTEST_F(AbilityConnectManagerTest, IsStrictMode_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "IsStrictMode_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + bool result = connectManager->IsStrictMode(nullptr); + ASSERT_FALSE(result); + TAG_LOGI(AAFwkTag::TEST, "IsStrictMode_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: IsStrictMode + */ +HWTEST_F(AbilityConnectManagerTest, IsStrictMode_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "IsStrictMode_002 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = serviceRecord_; + abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::INPUTMETHOD; + Want want; + want.SetParam("strictMode", true); + abilityRecord->SetWant(want); + + bool result = connectManager->IsStrictMode(abilityRecord); + ASSERT_TRUE(result); + TAG_LOGI(AAFwkTag::TEST, "IsStrictMode_002 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: TerminateAbilityInner + */ +HWTEST_F(AbilityConnectManagerTest, TerminateAbilityInner_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest_); + auto abilityInfo = abilityRecord->GetAbilityInfo(); + abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI; + auto result = ConnectManager()->TerminateAbilityInner(abilityRecord->GetToken()); + EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result); + TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: TerminateAbilityInner + */ +HWTEST_F(AbilityConnectManagerTest, TerminateAbilityInner_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_002 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = serviceRecord_; + auto abilityInfo = abilityRecord->GetAbilityInfo(); + abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI; + OHOS::sptr callback = new AbilityConnectCallback(); + std::shared_ptr connectionRe = + std::make_shared(abilityRecord->GetToken(), abilityRecord, callback, nullptr); + EXPECT_NE(connectionRe, nullptr); + + abilityRecord->currentState_ = AbilityState::FOREGROUND; + abilityRecord->connRecordList_.push_back(connectionRe); + auto result = connectManager->TerminateAbilityInner(abilityRecord->GetToken()); + EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result); + TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_002 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: TerminateAbilityInner + */ +HWTEST_F(AbilityConnectManagerTest, TerminateAbilityInner_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_003 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = serviceRecord_; + auto abilityInfo = abilityRecord->GetAbilityInfo(); + abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI; + abilityRecord->connRecordList_.clear(); + auto result = connectManager->TerminateAbilityInner(abilityRecord->GetToken()); + EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result); + TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_003 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: IsWindowExtensionFocused + */ +HWTEST_F(AbilityConnectManagerTest, IsWindowExtensionFocused_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "IsWindowExtensionFocused_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + ASSERT_NE(connectManager, nullptr); + + uint32_t extensionTokenId = 12345; + std::shared_ptr abilityRecord = serviceRecord_; + sptr focusToken = abilityRecord->GetToken(); + ASSERT_NE(focusToken, nullptr); + + sptr sessionInfo = MockSessionInfo(1); + sessionInfo->callerToken = focusToken; + connectManager->windowExtensionMap_.emplace(focusToken, std::make_pair(extensionTokenId, sessionInfo)); + + bool result = connectManager->IsWindowExtensionFocused(extensionTokenId, focusToken); + EXPECT_TRUE(result); + + uint32_t invalidTokenId = 54321; + result = connectManager->IsWindowExtensionFocused(invalidTokenId, focusToken); + EXPECT_FALSE(result); + TAG_LOGI(AAFwkTag::TEST, "IsWindowExtensionFocused_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: IsWindowExtensionFocused + */ +HWTEST_F(AbilityConnectManagerTest, IsWindowExtensionFocused_0002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "IsWindowExtensionFocused_002 start"); + std::shared_ptr connectManager = std::make_shared(0); + ASSERT_NE(connectManager, nullptr); + + uint32_t extensionTokenId = 12345; + std::shared_ptr abilityRecord = serviceRecord_; + sptr focusToken = abilityRecord->GetToken(); + ASSERT_NE(focusToken, nullptr); + + sptr sessionInfo = MockSessionInfo(1); + sessionInfo->callerToken = focusToken; + connectManager->windowExtensionMap_.emplace(focusToken, std::make_pair(extensionTokenId, nullptr)); + + bool result = connectManager->IsWindowExtensionFocused(extensionTokenId, focusToken); + EXPECT_FALSE(result); + TAG_LOGI(AAFwkTag::TEST, "IsWindowExtensionFocused_002 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: NeedExtensionControl + */ +HWTEST_F(AbilityConnectManagerTest, NeedExtensionControl_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "NeedExtensionControl_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr nullRecord = nullptr; + bool result = connectManager->NeedExtensionControl(nullRecord); + EXPECT_FALSE(result); + + std::shared_ptr abilityRecord = serviceRecord_; + abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::SERVICE; + result = connectManager->NeedExtensionControl(abilityRecord); + EXPECT_FALSE(result); + + abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::DATASHARE; + result = connectManager->NeedExtensionControl(abilityRecord); + EXPECT_FALSE(result); + TAG_LOGI(AAFwkTag::TEST, "NeedExtensionControl_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: NeedExtensionControl + */ +HWTEST_F(AbilityConnectManagerTest, NeedExtensionControl_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "NeedExtensionControl_002 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = serviceRecord_; + abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::FORM; + abilityRecord->SetCustomProcessFlag(""); + bool result = connectManager->NeedExtensionControl(abilityRecord); + EXPECT_TRUE(result); + + abilityRecord->SetCustomProcessFlag("Test customProces"); + uint32_t extensionProcessMode = 0; + abilityRecord->SetExtensionProcessMode(extensionProcessMode); + result = connectManager->NeedExtensionControl(abilityRecord); + EXPECT_FALSE(result); + TAG_LOGI(AAFwkTag::TEST, "NeedExtensionControl_002 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: StartAbilityLocked + */ +HWTEST_F(AbilityConnectManagerTest, StartAbilityLocked_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbilityLocked_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = serviceRecord_; + sptr focusToken = abilityRecord->GetToken(); + AbilityRequest abilityRequest; + abilityRequest.abilityInfo.deviceId = "id"; + abilityRequest.abilityInfo.bundleName = "bundle"; + abilityRequest.abilityInfo.name = "name"; + abilityRequest.abilityInfo.moduleName = "module"; + abilityRequest.abilityInfo.extensionAbilityType = ExtensionAbilityType::UI; + std::string stringUri = "id/bundle/module/name"; + abilityRequest.callerToken = abilityRecord->GetToken(); + AppExecFwk::ElementName element(abilityRequest.abilityInfo.deviceId, abilityRequest.abilityInfo.bundleName, + abilityRequest.abilityInfo.name, abilityRequest.abilityInfo.moduleName); + EXPECT_EQ(element.GetURI(), stringUri); + connectManager->uiExtensionAbilityRecordMgr_ = nullptr; + + int result = connectManager->StartAbilityLocked(abilityRequest); + EXPECT_NE(result, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "StartAbilityLocked_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: DoForegroundUIExtension + */ +HWTEST_F(AbilityConnectManagerTest, DoForegroundUIExtension_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "DoForegroundUIExtension_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr nullRecord = nullptr; + AbilityRequest abilityRequest; + abilityRequest.abilityInfo.deviceId = "id"; + abilityRequest.abilityInfo.bundleName = "bundle"; + abilityRequest.abilityInfo.name = "name"; + abilityRequest.abilityInfo.moduleName = "module"; + abilityRequest.abilityInfo.extensionAbilityType = ExtensionAbilityType::UI; + connectManager->DoForegroundUIExtension(nullRecord, abilityRequest); + + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + abilityRecord->isReady_ = true; + abilityRecord->currentState_ = AbilityState::FOREGROUND; + + connectManager->DoForegroundUIExtension(abilityRecord, abilityRequest); + std::string expectName = "name"; + EXPECT_EQ(expectName, abilityRequest.abilityInfo.name); + TAG_LOGI(AAFwkTag::TEST, "DoForegroundUIExtension_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: GetOrCreateExtensionRecord + */ +HWTEST_F(AbilityConnectManagerTest, GetOrCreateExtensionRecord_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetOrCreateExtensionRecord_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + AbilityRequest abilityRequest; + abilityRequest.appInfo.bundleName = "com.example.unittest"; + abilityRequest.abilityInfo.name = "MainAbility"; + abilityRequest.abilityInfo.type = AbilityType::PAGE; + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + std::string hostBundName = "bundleName"; + bool isCreate = false; + bool isLoadedAbility = false; + connectManager->uiExtensionAbilityRecordMgr_ = nullptr; + + int32_t result = connectManager->GetOrCreateExtensionRecord( + abilityRequest, isCreate, hostBundName, abilityRecord, isLoadedAbility); + EXPECT_EQ(result, ERR_NULL_OBJECT); + TAG_LOGI(AAFwkTag::TEST, "GetOrCreateExtensionRecord_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: GetOrCreateExtensionRecord + */ +HWTEST_F(AbilityConnectManagerTest, GetOrCreateServiceRecord_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetOrCreateServiceRecord_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + AbilityRequest abilityRequest; + abilityRequest.abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI_SERVICE; + abilityRequest.abilityInfo.bundleName = "com.example.test"; + abilityRequest.abilityInfo.name = "TestAbility"; + + std::shared_ptr resultService = nullptr; + bool isLoadedAbility = false; + connectManager->GetOrCreateServiceRecord(abilityRequest, false, resultService, isLoadedAbility); + + ASSERT_NE(resultService, nullptr); + EXPECT_EQ(resultService->GetAbilityInfo().name, "TestAbility"); + TAG_LOGI(AAFwkTag::TEST, "GetOrCreateServiceRecord_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: GetOrCreateExtensionRecord + */ +HWTEST_F(AbilityConnectManagerTest, UnloadUIExtensionAbility_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "UnloadUIExtensionAbility_001 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + AbilityRequest abilityRequest; + abilityRequest.abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::WORK_SCHEDULER; + abilityRequest.abilityInfo.bundleName = "com.example.test"; + abilityRequest.abilityInfo.name = "TestAbility"; + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + OHOS::sptr callback = new AbilityConnectCallback(); + std::shared_ptr connection = + std::make_shared(abilityRecord->GetToken(), abilityRecord, callback, nullptr); + sptr token = abilityRecord->GetToken(); + connection->SetConnectState(ConnectionState::DISCONNECTING); + abilityRecord->abilityInfo_.type = AbilityType::PAGE; + abilityRecord->SetAbilityState(AbilityState::INACTIVE); + abilityRecord->connRecordList_.push_back(connection); + std::string hostBundName = "bundleName"; + + int result = connectManager->UnloadUIExtensionAbility(abilityRecord, hostBundName); + EXPECT_NE(result, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "UnloadUIExtensionAbility_001 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: HandleActiveAbility + */ +HWTEST_F(AbilityConnectManagerTest, HandleActiveAbility_004, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_004 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + AbilityRequest abilityRequest; + abilityRequest.abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::WORK_SCHEDULER; + abilityRequest.abilityInfo.bundleName = "com.example.test"; + abilityRequest.abilityInfo.name = "TestAbility"; + std::shared_ptr targetService = AbilityRecord::CreateAbilityRecord(abilityRequest); + Want want; + want.SetParam(PARAM_RESV_CALLER_APP_ID, std::string("app")); + targetService->SetWant(want); + targetService->abilityInfo_.extensionAbilityType = ExtensionAbilityType::UI_SERVICE; + std::shared_ptr connectRecord = nullptr; + + connectManager->HandleActiveAbility(targetService, connectRecord); + EXPECT_NE(targetService->GetWant().GetStringParam(PARAM_RESV_CALLER_APP_ID), "app"); + TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_004 end"); +} + +/* + * Feature: AbilityConnectManager + * Function: HandleActiveAbility + */ +HWTEST_F(AbilityConnectManagerTest, HandleActiveAbility_005, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_005 start"); + std::shared_ptr connectManager = std::make_shared(0); + EXPECT_NE(connectManager, nullptr); + + std::shared_ptr abilityRecord = serviceRecord_; + AbilityRequest abilityRequest; + abilityRequest.abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI_SERVICE; + abilityRequest.abilityInfo.bundleName = "com.example.test"; + abilityRequest.abilityInfo.name = "TestAbility"; + std::shared_ptr targetService = AbilityRecord::CreateAbilityRecord(abilityRequest); + Want want; + want.SetParam(PARAM_RESV_CALLER_APP_ID, std::string("app")); + targetService->SetWant(want); + sptr callerToken = abilityRecord->GetToken(); + OHOS::sptr callback1 = new AbilityConnectCallback(); + std::shared_ptr connectRecord = + std::make_shared(callerToken, abilityRecord, callback1, nullptr); + + connectManager->HandleActiveAbility(targetService, connectRecord); + EXPECT_NE(targetService->GetWant().GetStringParam(PARAM_RESV_CALLER_APP_ID), "app"); + TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_005 end"); +} } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/ability_running_record_test/ability_running_record_test.cpp b/test/unittest/ability_running_record_test/ability_running_record_test.cpp index 193f4209d7..077c126b5d 100644 --- a/test/unittest/ability_running_record_test/ability_running_record_test.cpp +++ b/test/unittest/ability_running_record_test/ability_running_record_test.cpp @@ -25,6 +25,11 @@ using namespace testing::ext; namespace OHOS { namespace AppExecFwk { +namespace { +constexpr const char* BUNDLE_NAME_SCENEBOARD = "com.ohos.sceneboard"; +constexpr const char* SCENEBOARD_ABILITY_NAME = "com.ohos.sceneboard.MainAbility"; +static const std::string EMPTY_NAME; +} class AbilityRunningRecordTest : public testing::Test { public: static void SetUpTestCase(); @@ -82,6 +87,23 @@ HWTEST_F(AbilityRunningRecordTest, GetName_001, TestSize.Level1) TAG_LOGD(AAFwkTag::TEST, "GetName_001 end."); } +/* + * Feature: AbilityRunningRecord + * Function: GetName + * SubFunction: NA + * FunctionPoints: AbilityRunningRecord GetName + * EnvConditions: NA + * CaseDescription: GetName + */ +HWTEST_F(AbilityRunningRecordTest, GetName_002, TestSize.Level1) +{ + TAG_LOGD(AAFwkTag::TEST, "GetName_002 start."); + auto record = std::make_shared(nullptr, nullptr, 0); + ASSERT_NE(record, nullptr); + EXPECT_EQ(record->GetName(), EMPTY_NAME); + TAG_LOGD(AAFwkTag::TEST, "GetName_002 end."); +} + /* * Feature: AbilityRunningRecord * Function: GetBundleName @@ -104,6 +126,23 @@ HWTEST_F(AbilityRunningRecordTest, GetBundleName_001, TestSize.Level1) TAG_LOGD(AAFwkTag::TEST, "GetBundleName_001 end."); } +/* + * Feature: AbilityRunningRecord + * Function: GetBundleName + * SubFunction: NA + * FunctionPoints: AbilityRunningRecord GetBundleName + * EnvConditions: NA + * CaseDescription: GetBundleName + */ +HWTEST_F(AbilityRunningRecordTest, GetBundleName_002, TestSize.Level1) +{ + TAG_LOGD(AAFwkTag::TEST, "GetBundleName_002 start."); + auto record = std::make_shared(nullptr, nullptr, 0); + ASSERT_NE(record, nullptr); + EXPECT_EQ(record->GetBundleName(), EMPTY_NAME); + TAG_LOGD(AAFwkTag::TEST, "GetBundleName_002 end."); +} + /* * Feature: AbilityRunningRecord * Function: GetModuleName @@ -126,6 +165,23 @@ HWTEST_F(AbilityRunningRecordTest, GetModuleName_001, TestSize.Level1) TAG_LOGD(AAFwkTag::TEST, "GetModuleName_001 end."); } +/* + * Feature: AbilityRunningRecord + * Function: GetModuleName + * SubFunction: NA + * FunctionPoints: AbilityRunningRecord GetModuleName + * EnvConditions: NA + * CaseDescription: GetModuleName + */ +HWTEST_F(AbilityRunningRecordTest, GetModuleName_002, TestSize.Level1) +{ + TAG_LOGD(AAFwkTag::TEST, "GetModuleName_002 start."); + auto record = std::make_shared(nullptr, nullptr, 0); + ASSERT_NE(record, nullptr); + EXPECT_EQ(record->GetModuleName(), EMPTY_NAME); + TAG_LOGD(AAFwkTag::TEST, "GetModuleName_002 end."); +} + /* * Feature: AbilityRunningRecord * Function: GetAbilityInfo @@ -365,5 +421,45 @@ HWTEST_F(AbilityRunningRecordTest, IsHook_001, TestSize.Level1) record->SetWant(want); EXPECT_FALSE(record->IsHook()); } + +/* + * Feature: AbilityRunningRecord + * Function: IsSceneBoard + * SubFunction: NA + * FunctionPoints: AbilityRunningRecord IsSceneBoard + * EnvConditions: NA + * CaseDescription: IsSceneBoard + */ +HWTEST_F(AbilityRunningRecordTest, IsSceneBoard_001, TestSize.Level1) +{ + TAG_LOGD(AAFwkTag::TEST, "IsSceneBoard_001 start."); + auto record = std::make_shared(nullptr, nullptr, 1); + ASSERT_NE(record, nullptr); + EXPECT_FALSE(record->IsSceneBoard()); + TAG_LOGD(AAFwkTag::TEST, "IsSceneBoard_001 end."); +} + +/* + * Feature: AbilityRunningRecord + * Function: IsSceneBoard + * SubFunction: NA + * FunctionPoints: AbilityRunningRecord IsSceneBoard + * EnvConditions: NA + * CaseDescription: IsSceneBoard + */ +HWTEST_F(AbilityRunningRecordTest, IsSceneBoard_002, TestSize.Level1) +{ + TAG_LOGD(AAFwkTag::TEST, "IsSceneBoard_002 start."); + auto abilityInfo = std::make_shared(); + abilityInfo->name = SCENEBOARD_ABILITY_NAME; + abilityInfo->bundleName = BUNDLE_NAME_SCENEBOARD; + auto record = std::make_shared(abilityInfo, nullptr, 1); + ASSERT_NE(record, nullptr); + EXPECT_TRUE(record->IsSceneBoard()); + + abilityInfo->bundleName = "invalid_bundle_name"; + EXPECT_FALSE(record->IsSceneBoard()); + TAG_LOGD(AAFwkTag::TEST, "IsSceneBoard_002 end."); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_preloader_test/app_preloader_test.cpp b/test/unittest/app_preloader_test/app_preloader_test.cpp index 182cd38895..7fb0402383 100755 --- a/test/unittest/app_preloader_test/app_preloader_test.cpp +++ b/test/unittest/app_preloader_test/app_preloader_test.cpp @@ -16,6 +16,7 @@ #include #include "app_preloader.h" +#include "ability_manager_errors.h" #include "hilog_tag_wrapper.h" using namespace testing; @@ -293,5 +294,159 @@ HWTEST_F(AppPreloaderTest, AppPreloaderTest_PreCheck_0400, TestSize.Level2) auto ret = manager->PreCheck(bundleName, PreloadMode::PRESS_DOWN); EXPECT_EQ(ret, false); } + +/** + * @tc.number: AppPreloaderTest_GetLaunchWant_0100 + * @tc.desc: Test GetLaunchWant works + * @tc.type: FUNC + * @tc.Function: GetLaunchWant + * @tc.SubFunction: NA + * @tc.EnvConditions: NA + */ +HWTEST_F(AppPreloaderTest, AppPreloaderTest_GetLaunchWant_0100, TestSize.Level2) +{ + TAG_LOGD(AAFwkTag::TEST, "AppPreloaderTest_GetLaunchWant_0100 start."); + auto manager = std::make_shared(remoteClientManager_); + EXPECT_NE(manager, nullptr); + + std::string bundleName = "com.acts.preloadtest"; + int32_t userId = -2; + int32_t appIndex = 0; + PreloadRequest request; + auto ret = manager->GeneratePreloadRequest(bundleName, userId, appIndex, request); + EXPECT_EQ(ret, AAFwk::ERR_TARGET_BUNDLE_NOT_EXIST); +} + + /** + * @tc.number: AppPreloaderTest_GetLaunchAbilityInfo_0200 + * @tc.desc: Test GetLaunchAbilityInfo works + * @tc.type: FUNC + * @tc.Function: GetLaunchAbilityInfo + * @tc.SubFunction: NA + * @tc.EnvConditions: NA + */ +HWTEST_F(AppPreloaderTest, AppPreloaderTest_GetLaunchAbilityInfo_0200, TestSize.Level2) +{ + TAG_LOGD(AAFwkTag::TEST, "AppPreloaderTest_GetLaunchAbilityInfo_0200 start."); + auto manager = std::make_shared(remoteClientManager_); + EXPECT_NE(manager, nullptr); + + std::string bundleName = "com.acts.preloadtest"; + int32_t userId = -3; + int32_t appIndex = 0; + PreloadRequest request; + auto ret = manager->GeneratePreloadRequest(bundleName, userId, appIndex, request); + EXPECT_EQ(ret, AAFwk::ERR_GET_LAUNCH_ABILITY_INFO_FAILED); +} + +/** + * @tc.number: AppPreloaderTest_CheckPreloadConditions_0100 + * @tc.desc: Test CheckPreloadConditions works + * @tc.type: FUNC + * @tc.Function: CheckPreloadConditions + * @tc.SubFunction: NA + * @tc.EnvConditions: NA + */ +HWTEST_F(AppPreloaderTest, AppPreloaderTest_CheckPreloadConditions_0100, TestSize.Level2) +{ + TAG_LOGD(AAFwkTag::TEST, "AppPreloaderTest_CheckPreloadConditions_0100 start."); + auto manager = std::make_shared(remoteClientManager_); + EXPECT_NE(manager, nullptr); + + std::string bundleName = "com.acts.preloadtest"; + int32_t userId = -5; + int32_t appIndex = 0; + PreloadRequest request; + auto ret = manager->GeneratePreloadRequest(bundleName, userId, appIndex, request); + EXPECT_EQ(ret, AAFwk::ERR_CHECK_PRELOAD_CONDITIONS_FAILED); +} + +/** + * @tc.number: AppPreloaderTest_CheckPreloadConditions_0200 + * @tc.desc: Test CheckPreloadConditions works + * @tc.type: FUNC + * @tc.Function: CheckPreloadConditions + * @tc.SubFunction: NA + * @tc.EnvConditions: NA + */ +HWTEST_F(AppPreloaderTest, AppPreloaderTest_CheckPreloadConditions_0200, TestSize.Level2) +{ + TAG_LOGD(AAFwkTag::TEST, "AppPreloaderTest_CheckPreloadConditions_0200 start."); + auto manager = std::make_shared(remoteClientManager_); + EXPECT_NE(manager, nullptr); + + std::string bundleName = "com.acts.preloadtest"; + int32_t userId = -6; + int32_t appIndex = 0; + PreloadRequest request; + auto ret = manager->GeneratePreloadRequest(bundleName, userId, appIndex, request); + EXPECT_EQ(ret, AAFwk::ERR_CHECK_PRELOAD_CONDITIONS_FAILED); +} + +/** + * @tc.number: AppPreloaderTest_CheckPreloadConditions_0300 + * @tc.desc: Test CheckPreloadConditions works + * @tc.type: FUNC + * @tc.Function: CheckPreloadConditions + * @tc.SubFunction: NA + * @tc.EnvConditions: NA + */ +HWTEST_F(AppPreloaderTest, AppPreloaderTest_CheckPreloadConditions_0300, TestSize.Level2) +{ + TAG_LOGD(AAFwkTag::TEST, "AppPreloaderTest_CheckPreloadConditions_0300 start."); + auto manager = std::make_shared(remoteClientManager_); + EXPECT_NE(manager, nullptr); + + std::string bundleName = "com.acts.preloadtest"; + int32_t userId = -7; + int32_t appIndex = 0; + PreloadRequest request; + auto ret = manager->GeneratePreloadRequest(bundleName, userId, appIndex, request); + EXPECT_EQ(ret, AAFwk::ERR_CHECK_PRELOAD_CONDITIONS_FAILED); +} + + /** + * @tc.number: AppPreloaderTest_GetBundleAndHapInfo_0100 + * @tc.desc: Test GetBundleAndHapInfo works + * @tc.type: FUNC + * @tc.Function: GetBundleAndHapInfo + * @tc.SubFunction: NA + * @tc.EnvConditions: NA + */ +HWTEST_F(AppPreloaderTest, AppPreloaderTest_GetBundleAndHapInfo_0100, TestSize.Level2) +{ + TAG_LOGD(AAFwkTag::TEST, "AppPreloaderTest_GetBundleAndHapInfo_0100 start."); + auto manager = std::make_shared(remoteClientManager_); + EXPECT_NE(manager, nullptr); + + std::string bundleName = "com.acts.preloadtest"; + int32_t userId = -9; + int32_t appIndex = 0; + PreloadRequest request; + auto ret = manager->GeneratePreloadRequest(bundleName, userId, appIndex, request); + EXPECT_EQ(ret, AAFwk::GET_BUNDLE_INFO_FAILED); +} + + /** + * @tc.number: AppPreloaderTest_GetBundleAndHapInfo_0200 + * @tc.desc: Test GetBundleAndHapInfo works + * @tc.type: FUNC + * @tc.Function: GetBundleAndHapInfo + * @tc.SubFunction: NA + * @tc.EnvConditions: NA + */ +HWTEST_F(AppPreloaderTest, AppPreloaderTest_GetBundleAndHapInfo_0200, TestSize.Level2) +{ + TAG_LOGD(AAFwkTag::TEST, "AppPreloaderTest_GetBundleAndHapInfo_0200 start."); + auto manager = std::make_shared(remoteClientManager_); + EXPECT_NE(manager, nullptr); + + std::string bundleName = "com.acts.preloadtest"; + int32_t userId = -10; + int32_t appIndex = 0; + PreloadRequest request; + auto ret = manager->GeneratePreloadRequest(bundleName, userId, appIndex, request); + EXPECT_EQ(ret, AAFwk::GET_BUNDLE_INFO_FAILED); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_preloader_test/include/bundle_mgr_helper.h b/test/unittest/app_preloader_test/include/bundle_mgr_helper.h index e9cd13e107..86b808d8a5 100644 --- a/test/unittest/app_preloader_test/include/bundle_mgr_helper.h +++ b/test/unittest/app_preloader_test/include/bundle_mgr_helper.h @@ -18,9 +18,20 @@ #include "bundle_mgr_interface.h" #include "want.h" +#include "app_mgr_service_dump_error_code.h" namespace OHOS { namespace AppExecFwk { + enum UidCheckCode { + UID_CHECK_INVALID_NUM = -2, + UID_CHECK_FALSE = -3, + UID_CHECK_PRELOAD_SERVICE = -5, + UID_CHECK_PRELOAD_PAGE = -6, + UID_CHECK_PRELOAD_CONDITIONS = -7, + UID_CHECK_BUNDLE_INFO = -9, + UID_CHECK_BUNDLE_INFO_FAILED = -10, + }; + using Want = OHOS::AAFwk::Want; class BundleMgrHelper : public std::enable_shared_from_this { @@ -30,26 +41,65 @@ public: ErrCode GetLaunchWantForBundle(const std::string &bundleName, Want &want, int32_t userId) { + if (userId == UID_CHECK_INVALID_NUM) { + return ERR_INVALID_NUM_ARGS_ERROR; + } + return ERR_OK; } bool QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo) { - abilityInfo.type = AppExecFwk::AbilityType::PAGE; - abilityInfo.isStageBasedModel = true; - abilityInfo.name = "MainAbility"; - abilityInfo.applicationName = "com.acts.preloadtest"; - abilityInfo.applicationInfo.name = abilityInfo.applicationName; + switch (userId) { + case UID_CHECK_FALSE: + return false; + break; + case UID_CHECK_PRELOAD_SERVICE: + abilityInfo.type = AppExecFwk::AbilityType::SERVICE; + abilityInfo.isStageBasedModel = true; + abilityInfo.name = "MainAbility"; + abilityInfo.applicationName = "com.acts.preloadtest"; + abilityInfo.applicationInfo.name = abilityInfo.applicationName; + break; + case UID_CHECK_PRELOAD_PAGE: + abilityInfo.type = AppExecFwk::AbilityType::PAGE; + abilityInfo.isStageBasedModel = true; + abilityInfo.name = ""; + abilityInfo.applicationName = "com.acts.preloadtest"; + abilityInfo.applicationInfo.name = abilityInfo.applicationName; + break; + case UID_CHECK_PRELOAD_CONDITIONS: + abilityInfo.type = AppExecFwk::AbilityType::PAGE; + abilityInfo.isStageBasedModel = true; + abilityInfo.name = "MainAbility"; + abilityInfo.applicationName = "com.acts.preloadtest"; + abilityInfo.applicationInfo.name = "com.acts.preloadtest2"; + break; + default: + abilityInfo.type = AppExecFwk::AbilityType::PAGE; + abilityInfo.isStageBasedModel = true; + abilityInfo.name = "MainAbility"; + abilityInfo.applicationName = "com.acts.preloadtest"; + abilityInfo.applicationInfo.name = abilityInfo.applicationName; + } + return true; } bool GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId) { + if (userId == UID_CHECK_BUNDLE_INFO) { + return false; + } + return true; } bool GetHapModuleInfo(const AbilityInfo &abilityInfo, int32_t userId, HapModuleInfo &hapModuleInfo) { + if (userId == UID_CHECK_BUNDLE_INFO_FAILED) { + return false; + } return true; } };