From 58ec25a6704ec42128db9bd45b56c2acade9d28d Mon Sep 17 00:00:00 2001 From: "yang.yang" Date: Thu, 12 Jun 2025 22:02:14 +0800 Subject: [PATCH] opt dfx Signed-off-by: yang.yang Change-Id: I3dcf8c9edc67042419eeefef5d7107bb51e92ce9 --- services/abilitymgr/src/connection_record.cpp | 3 +- .../ability_connect_manager_test.cpp | 144 ++++++++++++++++++ 2 files changed, 146 insertions(+), 1 deletion(-) diff --git a/services/abilitymgr/src/connection_record.cpp b/services/abilitymgr/src/connection_record.cpp index 6c8122777f1..8f7601b8372 100644 --- a/services/abilitymgr/src/connection_record.cpp +++ b/services/abilitymgr/src/connection_record.cpp @@ -130,7 +130,8 @@ int ConnectionRecord::DisconnectAbility() TAG_LOGI(AAFwkTag::CONNECTION, "Disconnect UIServiceExtension ability, set correct want"); targetService_->DisconnectAbilityWithWant(GetConnectWant()); } else { - TAG_LOGI(AAFwkTag::CONNECTION, "DisconnectAbility called"); + TAG_LOGI(AAFwkTag::CONNECTION, "DisconnectAbility %{public}s called", + targetService_->GetAbilityInfo().name.c_str()); targetService_->DisconnectAbility(); } } else { diff --git a/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp b/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp index 72de2ade6be..88755eedca9 100644 --- a/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp +++ b/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp @@ -1053,6 +1053,95 @@ HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Disconnect_002, TestSize.Level1) } } +/* + * Feature: AbilityConnectManager + * Function: DisconnectAbilityLocked + * SubFunction: + * FunctionPoints: DisconnectAbilityLocked and ConnectAbilityLocked + * EnvConditions:NA + * CaseDescription:Verify the following: + * 1. Disconnect ability a nonexistent connect, disconnect failed + * 2. If the current connect ability state is not connected, disconnect fails + * 3. Verify the success of disconnect ability + */ +HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Disconnect_003, TestSize.Level1) +{ + ConnectManager()->SetTaskHandler(TaskHandler()); + ConnectManager()->SetEventHandler(EventHandler()); + + auto callback = new AbilityConnectCallback(); + auto result = ConnectManager()->DisconnectAbilityLocked(callback); + EXPECT_EQ(result, OHOS::AAFwk::CONNECTION_NOT_EXIST); + + auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr); + EXPECT_EQ(0, result1); + + auto result2 = ConnectManager()->DisconnectAbilityLocked(callbackA_); + EXPECT_EQ(result2, OHOS::AAFwk::INVALID_CONNECTION_STATE); + + auto list = ConnectManager()->GetConnectRecordListByCallback(callbackA_); + EXPECT_EQ(static_cast(list.size()), 1); + + for (auto& it : list) { + it->SetConnectState(ConnectionState::CONNECTED); + } + + auto result3 = ConnectManager()->DisconnectAbilityLocked(callbackA_); + EXPECT_EQ(result3, OHOS::ERR_OK); +} + +/* + * Feature: AbilityConnectManager + * Function: DisconnectAbilityLocked + * SubFunction: + * FunctionPoints: DisconnectAbilityLocked and ConnectAbilityLocked + * EnvConditions:NA + * CaseDescription: Results after verifying the disconnect ability + */ +HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Disconnect_004, TestSize.Level1) +{ + ConnectManager()->SetTaskHandler(TaskHandler()); + ConnectManager()->SetEventHandler(EventHandler()); + + auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr); + EXPECT_EQ(0, result); + + auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr); + EXPECT_EQ(0, result1); + + auto result2 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackA_, nullptr); + EXPECT_EQ(0, result2); + + auto result3 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackB_, nullptr); + EXPECT_EQ(0, result3); + + auto listA = ConnectManager()->GetConnectRecordListByCallback(callbackA_); + EXPECT_EQ(static_cast(listA.size()), 2); + + for (auto& it : listA) { + it->SetConnectState(ConnectionState::CONNECTED); + } + + auto listB = ConnectManager()->GetConnectRecordListByCallback(callbackB_); + EXPECT_EQ(static_cast(listB.size()), 2); + + for (auto& it : listB) { + it->SetConnectState(ConnectionState::CONNECTED); + } + + auto result5 = ConnectManager()->DisconnectAbilityLocked(callbackA_); + WaitUntilTaskDone(TaskHandler()); + EXPECT_EQ(result5, OHOS::ERR_OK); + auto serviceMap = ConnectManager()->GetServiceMap(); + EXPECT_EQ(static_cast(serviceMap.size()), 2); + + auto connectMap = ConnectManager()->connectMap_; + EXPECT_EQ(static_cast(connectMap.size()), 1); + for (auto& it : connectMap) { + EXPECT_EQ(static_cast(it.second.size()), 2); + } +} + /* * Feature: AbilityConnectManager * Function: AbilityTransitionDone @@ -1588,6 +1677,61 @@ HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAppStateChanged_002, TestS connectManager->OnAppStateChanged(info); } +/* + * Feature: AbilityConnectManager + * Function: OnAppStateChanged + * SubFunction: OnAppStateChanged + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Verify AbilityConnectManager OnAppStateChanged + */ +HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAppStateChanged_003, TestSize.Level1) +{ + std::shared_ptr connectManager = std::make_shared(0); + ASSERT_NE(connectManager, nullptr); + std::shared_ptr abilityRecord = serviceRecord_; + AppInfo info; + std::string bundleName = "bundleName"; + std::string name = "name"; + int32_t uid = 0; + info.processName = bundleName; + abilityRecord->abilityInfo_.applicationInfo.bundleName = bundleName; + abilityRecord->abilityInfo_.applicationInfo.name = name; + abilityRecord->abilityInfo_.uid = uid; + info.appData.push_back({name, uid}); + connectManager->serviceMap_.emplace("first", abilityRecord); + connectManager->serviceMap_.emplace("second", nullptr); + connectManager->OnAppStateChanged(info); +} + + /* + * Feature: AbilityConnectManager + * Function: OnAppStateChanged + * SubFunction: OnAppStateChanged + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Verify AbilityConnectManager OnAppStateChanged + */ +HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAppStateChanged_004, TestSize.Level1) +{ + std::shared_ptr connectManager = std::make_shared(0); + ASSERT_NE(connectManager, nullptr); + std::shared_ptr abilityRecord = serviceRecord_; + AppInfo info; + std::string bundleName = "bundleName"; + std::string name = "name"; + int32_t uid = 0; + info.processName = ""; + abilityRecord->abilityInfo_.applicationInfo.bundleName = bundleName; + abilityRecord->abilityInfo_.applicationInfo.name = name; + abilityRecord->abilityInfo_.uid = uid; + info.appData.push_back({name, uid}); + connectManager->serviceMap_.emplace("first", abilityRecord); + connectManager->serviceMap_.emplace("first", nullptr); + connectManager->serviceMap_.emplace("second", abilityRecord); + connectManager->OnAppStateChanged(info); +} + /* * Feature: AbilityConnectManager * Function: AbilityTransitionDone -- Gitee