From 6fe48e9f638cb1917ea0ae2a27898afaafd6ec8f Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Tue, 15 Apr 2025 10:19:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=86=E5=B8=83=E5=BC=8F?= =?UTF-8?q?=E7=A1=AC=E4=BB=B6=E6=A1=86=E6=9E=B6=E7=94=A8=E4=BE=8B=E9=BB=84?= =?UTF-8?q?=E5=8C=BA=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98=E5=8F=8A=E8=93=9D?= =?UTF-8?q?=E9=BB=84=E4=B8=8D=E4=B8=80=E8=87=B4=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuzhihui7 --- .../component_manager/BUILD.gn | 16 +++ .../include/component_manager_test_ext.h | 2 + .../include/mock_device_manager.h | 43 ++++++ .../src/component_manager_test_ext.cpp | 136 ++++++++++-------- .../src/mock_device_manager.cpp | 42 ++++++ 5 files changed, 179 insertions(+), 60 deletions(-) create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_device_manager.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_device_manager.cpp diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn index ef959afd..3e6b866f 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn @@ -81,6 +81,8 @@ ohos_unittest("ComponentManagerTest") { if (!distributed_hardware_fwk_low_latency) { defines += [ "DHARDWARE_CLOSE_UT" ] + } else { + defines += [ "DHARDWARE_CHECK_RESOURCE" ] } } @@ -93,6 +95,7 @@ ohos_unittest("ComponentManagerTestExt") { "src/mock_component_disable.cpp", "src/mock_component_enable.cpp", "src/mock_component_loader.cpp", + "src/mock_device_manager.cpp", "src/mock_dh_context.cpp", "src/mock_dh_utils_tool.cpp", "src/mock_meta_info_manager.cpp", @@ -119,6 +122,7 @@ ohos_unittest("ComponentManagerTestExt") { "access_token:libtoken_setproc", "cJSON:cjson", "c_utils:utils", + "device_manager:devicemanagersdk", "dsoftbus:softbus_client", "eventhandler:libeventhandler", "googletest:gmock", @@ -128,6 +132,18 @@ ohos_unittest("ComponentManagerTestExt") { "kv_store:distributeddata_inner", "samgr:samgr_proxy", ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"ComponentManagerTestExt\"", + "LOG_DOMAIN=0xD004100", + ] + + if (!distributed_hardware_fwk_low_latency) { + defines += [ "DHARDWARE_CLOSE_UT" ] + } else { + defines += [ "DHARDWARE_CHECK_RESOURCE" ] + } } group("component_manager_test") { diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test_ext.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test_ext.h index 6c39e7bf..de50c3b3 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test_ext.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test_ext.h @@ -20,6 +20,7 @@ #include "component_manager.h" #include "mock_capability_info_manager.h" #include "mock_component_loader.h" +#include "mock_device_manager.h" #include "mock_dh_context.h" #include "mock_dh_utils_tool.h" #include "mock_meta_info_manager.h" @@ -40,6 +41,7 @@ private: std::shared_ptr utilTool_; std::shared_ptr metaInfoManager_; std::shared_ptr versionManager_; + std::shared_ptr deviceManager_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_device_manager.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_device_manager.h new file mode 100644 index 00000000..9539b2ee --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_device_manager.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_MOCK_DEVICE_MANAGER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_DEVICE_MANAGER_H + +#include + +#include "device_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class IDeviceManager { +public: + virtual ~IDeviceManager() = default; + virtual int32_t GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList) = 0; + static std::shared_ptr GetOrCtreateInstance(); + static void ReleaseInstance(); +private: + static std::shared_ptr deviceManagerInstance_; +}; + +class MockDeviceManager : public IDeviceManager { +public: + MOCK_METHOD(int32_t, GetTrustedDeviceList, + (const std::string &, const std::string &, std::vector &)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_DEVICE_MANAGER_H \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp index 55bc6955..227475c6 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp @@ -85,6 +85,8 @@ void ComponentManagerTestExt::SetUp() metaInfoManager_ = std::static_pointer_cast(metaInfoManager); auto versionManager = IVersionManager::GetOrCtreateInstance(); versionManager_ = std::static_pointer_cast(versionManager); + auto deviceManager = IDeviceManager::GetOrCtreateInstance(); + deviceManager_ = std::static_pointer_cast(deviceManager); } void ComponentManagerTestExt::TearDown() @@ -95,12 +97,14 @@ void ComponentManagerTestExt::TearDown() IDHUtilTool::ReleaseInstance(); IMetaInfoManager::ReleaseInstance(); IVersionManager::ReleaseInstance(); + IDeviceManager::ReleaseInstance(); capabilityInfoManager_ = nullptr; componentLoader_ = nullptr; dhContext_ = nullptr; metaInfoManager_ = nullptr; versionManager_ = nullptr; utilTool_ = nullptr; + deviceManager_ = nullptr; } HWTEST_F(ComponentManagerTestExt, EnableSinkAndDisableSink_001, testing::ext::TestSize.Level1) @@ -355,6 +359,7 @@ HWTEST_F(ComponentManagerTestExt, EnableSourceAndDisableSource_003, testing::ext EXPECT_EQ(ret, DH_FWK_SUCCESS); } +#ifdef DHARDWARE_CHECK_RESOURCE HWTEST_F(ComponentManagerTestExt, EnableSourceAndDisableSource_004, testing::ext::TestSize.Level1) { ASSERT_TRUE(capabilityInfoManager_ != nullptr); @@ -370,7 +375,8 @@ HWTEST_F(ComponentManagerTestExt, EnableSourceAndDisableSource_004, testing::ext EXPECT_CALL(*componentLoader_, GetSourceSaId(_)).WillRepeatedly(Return(CAMERA_PID)); - auto capabilityInfo = std::make_shared(); + auto capabilityInfo = std::make_shared( + "dhid", "devId", "devName", 1, DHType::AUDIO, "dhAttrs", "mic"); EXPECT_CALL(*capabilityInfoManager_, GetCapability(_, _, _)) .WillRepeatedly(DoAll(SetArgReferee<2>(capabilityInfo), Return(DH_FWK_SUCCESS))); EXPECT_CALL(*capabilityInfoManager_, GetCapabilitiesByDeviceId(_, _)) @@ -383,13 +389,48 @@ HWTEST_F(ComponentManagerTestExt, EnableSourceAndDisableSource_004, testing::ext EXPECT_CALL(*utilTool_, GetLocalDeviceInfo()).WillRepeatedly(Return(VALUABLE_DEVICE_INFO)); EXPECT_CALL(*componentLoader_, ReleaseSource(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + std::map resDescMap; + resDescMap["mic"] = false; + EXPECT_CALL(*componentLoader_, GetCompResourceDesc()).WillRepeatedly(Return(resDescMap)); + auto ret = ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, AUDIO_DESCRIPTOR, 0, 0); EXPECT_EQ(ret, DH_FWK_SUCCESS); - ret = ComponentManager::GetInstance().DisableSource(VALUABLE_DEVICE_INFO.networkId, AUDIO_DESCRIPTOR, 0, 0); + ret = ComponentManager::GetInstance().DisableSource(VALUABLE_DEVICE_INFO.networkId, + AUDIO_DESCRIPTOR, 0, 0); EXPECT_EQ(ret, DH_FWK_SUCCESS); } +#endif -HWTEST_F(ComponentManagerTestExt, EnableSourceAndDisableSource_005, testing::ext::TestSize.Level1) +HWTEST_F(ComponentManagerTestExt, EnableSource_failed_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + ASSERT_TRUE(dhContext_ != nullptr); + + size_t isDHTypeSupportCallCount = 0; + size_t getSourceCallCount = 0; + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)) + .WillRepeatedly(Invoke([&isDHTypeSupportCallCount](DHType) { + return (isDHTypeSupportCallCount++ > 0) ? true : false; + })); + EXPECT_CALL(*dhContext_, GetUUIDByNetworkId(_)).Times(AtLeast(1)); + EXPECT_CALL(*componentLoader_, GetSource(_, _)) + .WillRepeatedly(Invoke([&getSourceCallCount](const DHType dhType, IDistributedHardwareSource *&sourcePtr) { + sourcePtr = nullptr; + return (getSourceCallCount++ > 0) ? DH_FWK_SUCCESS : ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + })); + for (size_t i = 0; i < 3; ++i) { + auto ret = ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, + AUDIO_DESCRIPTOR, AUDIO_UID, AUDIO_PID); + if (isDHTypeSupportCallCount > 0 && getSourceCallCount == 0) { + EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST); + } else { + EXPECT_EQ(ret, ERR_DH_FWK_LOADER_HANDLER_IS_NULL); + } + } +} + +#ifdef DHARDWARE_CHECK_RESOURCE +HWTEST_F(ComponentManagerTestExt, EnableSource_failed_002, testing::ext::TestSize.Level1) { ASSERT_TRUE(capabilityInfoManager_ != nullptr); ASSERT_TRUE(componentLoader_ != nullptr); @@ -417,43 +458,47 @@ HWTEST_F(ComponentManagerTestExt, EnableSourceAndDisableSource_005, testing::ext EXPECT_CALL(*utilTool_, GetLocalDeviceInfo()).WillRepeatedly(Return(VALUABLE_DEVICE_INFO)); EXPECT_CALL(*componentLoader_, ReleaseSource(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); - std::map resDescMap; - resDescMap["speaker"] = false; - EXPECT_CALL(*componentLoader_, GetCompResourceDesc()).WillRepeatedly(Return(resDescMap)); - auto ret = ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, AUDIO_DESCRIPTOR, 0, 0); - EXPECT_EQ(ret, DH_FWK_SUCCESS); - ret = ComponentManager::GetInstance().DisableSource(VALUABLE_DEVICE_INFO.networkId, AUDIO_DESCRIPTOR, 0, 0); - EXPECT_EQ(ret, DH_FWK_SUCCESS); + EXPECT_EQ(ret, ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY); } -HWTEST_F(ComponentManagerTestExt, EnableSource_failed_001, testing::ext::TestSize.Level2) +HWTEST_F(ComponentManagerTestExt, EnableSource_failed_003, testing::ext::TestSize.Level1) { + ASSERT_TRUE(capabilityInfoManager_ != nullptr); ASSERT_TRUE(componentLoader_ != nullptr); ASSERT_TRUE(dhContext_ != nullptr); + ASSERT_TRUE(versionManager_ != nullptr); + ASSERT_TRUE(utilTool_ != nullptr); - size_t isDHTypeSupportCallCount = 0; - size_t getSourceCallCount = 0; - EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)) - .WillRepeatedly(Invoke([&isDHTypeSupportCallCount](DHType) { - return (isDHTypeSupportCallCount++ > 0) ? true : false; - })); - EXPECT_CALL(*dhContext_, GetUUIDByNetworkId(_)).Times(AtLeast(1)); + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(true)); + auto sourcePtr = CreateDHSourcePtrWithSetExpectation(); EXPECT_CALL(*componentLoader_, GetSource(_, _)) - .WillRepeatedly(Invoke([&getSourceCallCount](const DHType dhType, IDistributedHardwareSource *&sourcePtr) { - sourcePtr = nullptr; - return (getSourceCallCount++ > 0) ? DH_FWK_SUCCESS : ERR_DH_FWK_LOADER_HANDLER_IS_NULL; - })); - for (size_t i = 0; i < 3; ++i) { - auto ret = ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, - AUDIO_DESCRIPTOR, AUDIO_UID, AUDIO_PID); - if (isDHTypeSupportCallCount > 0 && getSourceCallCount == 0) { - EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST); - } else { - EXPECT_EQ(ret, ERR_DH_FWK_LOADER_HANDLER_IS_NULL); - } - } + .WillRepeatedly(DoAll(SetArgReferee<1>(sourcePtr.get()), Return(DH_FWK_SUCCESS))); + + EXPECT_CALL(*componentLoader_, GetSourceSaId(_)).WillRepeatedly(Return(CAMERA_PID)); + + auto capabilityInfo = std::make_shared( + "dhid", "devId", "devName", 1, DHType::AUDIO, "dhAttrs", "mic"); + EXPECT_CALL(*capabilityInfoManager_, GetCapability(_, _, _)) + .WillRepeatedly(DoAll(SetArgReferee<2>(capabilityInfo), Return(DH_FWK_SUCCESS))); + EXPECT_CALL(*capabilityInfoManager_, GetCapabilitiesByDeviceId(_, _)) + .Times(AtLeast(1)); + DeviceInfo emptyInfo("", "", "", "", "", "", 0); + EXPECT_CALL(*dhContext_, GetDeviceInfo()).WillRepeatedly(ReturnRef(emptyInfo)); + EXPECT_CALL(*dhContext_, GetUUIDByNetworkId(_)).WillRepeatedly(Return(VALUABLE_DEVICE_INFO.uuid)); + EXPECT_CALL(*versionManager_, GetCompVersion(_, _, _)) + .WillRepeatedly(DoAll(SetArgReferee<2>(VERSION), Return(DH_FWK_SUCCESS))); + EXPECT_CALL(*utilTool_, GetLocalDeviceInfo()).WillRepeatedly(Return(VALUABLE_DEVICE_INFO)); + EXPECT_CALL(*componentLoader_, ReleaseSource(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + + std::map resDescMap; + resDescMap["mic"] = true; + EXPECT_CALL(*componentLoader_, GetCompResourceDesc()).WillRepeatedly(Return(resDescMap)); + + auto ret = ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, AUDIO_DESCRIPTOR, 0, 0); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_ENABLE_FAILED); } +#endif HWTEST_F(ComponentManagerTestExt, DisableSource_failed_001, testing::ext::TestSize.Level2) { @@ -631,35 +676,6 @@ HWTEST_F(ComponentManagerTestExt, CheckDemandStart_002, testing::ext::TestSize.L } } -HWTEST_F(ComponentManagerTestExt, CheckDemandStart_003, testing::ext::TestSize.Level2) -{ - ASSERT_TRUE(componentLoader_ != nullptr); - ASSERT_TRUE(dhContext_ != nullptr); - ASSERT_TRUE(metaInfoManager_ != nullptr); - - size_t getLocalDHVersionCallCount = 0; - - auto metaCapabilityInfo = std::make_shared(); - metaCapabilityInfo->SetCompVersion(VERSION); - MetaCapInfoMap infoMap = {{ VALUABLE_DEVICE_INFO.uuid, metaCapabilityInfo }}; - EXPECT_CALL(*metaInfoManager_, GetMetaDataByDHType(_, _)) - .WillRepeatedly(DoAll(SetArgReferee<1>(infoMap), Return(DH_FWK_SUCCESS))); - EXPECT_CALL(*dhContext_, GetUUIDByDeviceId(_)).WillRepeatedly(Return(VALUABLE_DEVICE_INFO.uuid)); - EXPECT_CALL(*componentLoader_, GetLocalDHVersion(_)) - .WillRepeatedly(Invoke([&getLocalDHVersionCallCount](DHVersion &dhVersion) { - DHVersion emptyVersion; - dhVersion = emptyVersion; - return (getLocalDHVersionCallCount++ > 0) ? DH_FWK_SUCCESS : ERR_DH_FWK_TYPE_NOT_EXIST; - })); - - for (size_t i = 0; i < 2; ++i) { - bool isEnableSource = false; - auto ret = ComponentManager::GetInstance().CheckDemandStart( - VALUABLE_DEVICE_INFO.uuid, DHType::CAMERA, isEnableSource); - EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST); - } -} - HWTEST_F(ComponentManagerTestExt, CheckDemandStart_004, testing::ext::TestSize.Level1) { ASSERT_TRUE(componentLoader_ != nullptr); diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_device_manager.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_device_manager.cpp new file mode 100644 index 00000000..fc425da4 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_device_manager.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mock_device_manager.h" + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr IDeviceManager::deviceManagerInstance_; + +std::shared_ptr IDeviceManager::GetOrCtreateInstance() +{ + if (!deviceManagerInstance_) { + deviceManagerInstance_ = std::make_shared(); + } + return deviceManagerInstance_; +} + +void IDeviceManager::ReleaseInstance() +{ + deviceManagerInstance_.reset(); + deviceManagerInstance_ = nullptr; +} + +int32_t DeviceManager::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList) +{ + return IDeviceManager::GetOrCtreateInstance()->GetTrustedDeviceList(pkgName, extra, deviceList); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file -- Gitee