From 8479c05fdc5ba86a28938129342c906622ad6091 Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Fri, 7 Mar 2025 19:37:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E7=9B=B8=E6=9C=BAal?= =?UTF-8?q?lconnect=E8=BF=BD=E5=8A=A0tdd=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuzhihui7 --- .../test/unittest/common/channel/BUILD.gn | 4 + .../dcamera_allconnect_manager_test.cpp | 200 ++++++++++++++++++ .../test/unittest/common/channel/dlfcn_mock.h | 39 ++++ .../common/channel/lib_function_mock.cpp | 63 ++++++ 4 files changed, 306 insertions(+) create mode 100644 services/channel/test/unittest/common/channel/dcamera_allconnect_manager_test.cpp create mode 100644 services/channel/test/unittest/common/channel/dlfcn_mock.h create mode 100644 services/channel/test/unittest/common/channel/lib_function_mock.cpp diff --git a/services/channel/test/unittest/common/channel/BUILD.gn b/services/channel/test/unittest/common/channel/BUILD.gn index 64a9eccf..f24a7681 100644 --- a/services/channel/test/unittest/common/channel/BUILD.gn +++ b/services/channel/test/unittest/common/channel/BUILD.gn @@ -60,11 +60,13 @@ ohos_unittest("DCameraChannelTest") { "${services_path}/channel/src/dcamera_softbus_adapter.cpp", "${services_path}/channel/src/dcamera_softbus_latency.cpp", "${services_path}/channel/src/dcamera_softbus_session.cpp", + "dcamera_allconnect_manager_test.cpp", "dcamera_channel_sink_impl_test.cpp", "dcamera_channel_source_impl_test.cpp", "dcamera_softbus_adapter_test.cpp", "dcamera_softbus_latency_test.cpp", "dcamera_softbus_session_test.cpp", + "lib_function_mock.cpp", "session_bus_center.cpp", "session_mock.cpp", ] @@ -85,6 +87,8 @@ ohos_unittest("DCameraChannelTest") { "drivers_interface_distributed_camera:libdistributed_camera_provider_proxy_1.1", "dsoftbus:softbus_client", "eventhandler:libeventhandler", + "googletest:gmock", + "googletest:gmock_main", "graphic_surface:surface", "hdf_core:libhdi", "hilog:libhilog", diff --git a/services/channel/test/unittest/common/channel/dcamera_allconnect_manager_test.cpp b/services/channel/test/unittest/common/channel/dcamera_allconnect_manager_test.cpp new file mode 100644 index 00000000..c8a9d00b --- /dev/null +++ b/services/channel/test/unittest/common/channel/dcamera_allconnect_manager_test.cpp @@ -0,0 +1,200 @@ +/* + * 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 +#include + +#include "distributed_camera_allconnect_manager.h" +#include "dlfcn_mock.h" + +using namespace testing; + +namespace OHOS { +namespace DistributedHardware { +namespace { +constexpr int32_t VALUABLE_SESSION_ID = 10; +constexpr int32_t UNVALUABLE_SESSION_ID = -1; +constexpr const char* DL_HANDLE = "dlhandle success"; +const std::string PEER_NETWORK_ID = "peerNetworkId"; + +int32_t CameraCollaborationPublishServiceStateFunc(const char *peerNetworkId, const char *serviceName, + const char *extraInfo, DCameraCollaborationBussinessStatus state) +{ + if (peerNetworkId == nullptr || strlen(peerNetworkId) == 0) { + return -1; + } + return 0; +} + +int32_t CameraCollaborationApplyAdvancedResourceFunc(const char *peerNetworkId, const char *serviceName, + DCameraCollaborationResourceRequestInfoSets *resourceRequest, + DCameraCollaborationCallback *callback) +{ + if (resourceRequest == nullptr || callback == nullptr || callback->applyResult == nullptr) { + return -1; + } + if (peerNetworkId == nullptr || strlen(peerNetworkId) == 0) { + callback->applyResult(DCAMERA_ERR_APPLY_RESULT, REJECT, "failed"); + } else { + callback->applyResult(DCAMERA_OK, PASS, "success"); + if (callback->onStop) { + callback->onStop(peerNetworkId); + } + } + return 0; +} + +int32_t CameraCollaborationRegisterLifecycleCallbackFunc(const char *serviceName, + DCameraCollaborationCallback *callback) +{ + return 0; +} + +int32_t CameraCollaborationUnRegisterLifecycleCallbackFunc(const char *serviceName) +{ + return 0; +} + +int32_t DCameraCollaborationExport(DCameraCollaborationApi *exportApi) +{ + if (!exportApi) { + return -1; + } + exportApi->dCameraCollaborationPublishServiceState = CameraCollaborationPublishServiceStateFunc; + exportApi->dCameraCollaborationApplyAdvancedResource = CameraCollaborationApplyAdvancedResourceFunc; + exportApi->dCameraCollaborationRegisterLifecycleCallback = CameraCollaborationRegisterLifecycleCallbackFunc; + exportApi->dCameraCollaborationUnRegisterLifecycleCallback = CameraCollaborationUnRegisterLifecycleCallbackFunc; + return 0; +} +} // namespace +class DCameraAllConnectManagerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + std::shared_ptr dlfcnInstance_; +}; + +void DCameraAllConnectManagerTest::SetUp() +{ + dlfcnInstance_ = DlfcnMock::GetOrCreateInstance(); + ASSERT_TRUE(dlfcnInstance_ != nullptr); + + EXPECT_CALL(*dlfcnInstance_, DlopenMock(_, _)).WillOnce(Return((void *)DL_HANDLE)); + EXPECT_CALL(*dlfcnInstance_, DlsymMock(_, _)).WillOnce(Return((void *)DCameraCollaborationExport)); + DCameraAllConnectManager::GetInstance().InitDCameraAllConnectManager(); +} + +void DCameraAllConnectManagerTest::TearDown() +{ + if (DCameraAllConnectManager::IsInited()) { + ASSERT_TRUE(dlfcnInstance_ != nullptr); + EXPECT_CALL(*dlfcnInstance_, DlcloseMock(_)).Times(AtLeast(1)); + DCameraAllConnectManager::GetInstance().UnInitDCameraAllConnectManager(); + } + DlfcnMock::ReleaseInstance(); + dlfcnInstance_ = nullptr; +} + +void DCameraAllConnectManagerTest::SetUpTestCase(void) +{} + +void DCameraAllConnectManagerTest::TearDownTestCase(void) +{} + +HWTEST_F(DCameraAllConnectManagerTest, PublishServiceState_001, testing::ext::TestSize.Level1) +{ + auto ret = DCameraAllConnectManager::GetInstance().PublishServiceState(PEER_NETWORK_ID, + DCameraCollaborationBussinessStatus::SCM_CONNECTING); + EXPECT_EQ(ret, DistributedCameraErrno::DCAMERA_OK); +} + +HWTEST_F(DCameraAllConnectManagerTest, PublishServiceState_002, testing::ext::TestSize.Level2) +{ + std::string emptyPeerNetworkId; + auto ret = DCameraAllConnectManager::GetInstance().PublishServiceState(emptyPeerNetworkId, + DCameraCollaborationBussinessStatus::SCM_CONNECTING); + EXPECT_EQ(ret, DistributedCameraErrno::DCAMERA_ERR_PUBLISH_STATE); +} + +HWTEST_F(DCameraAllConnectManagerTest, ApplyAdvancedResource_001, testing::ext::TestSize.Level1) +{ + DCameraCollaborationResourceRequestInfoSets resultCallback; + auto ret = DCameraAllConnectManager::GetInstance().ApplyAdvancedResource(PEER_NETWORK_ID, &resultCallback); + EXPECT_EQ(ret, DistributedCameraErrno::DCAMERA_OK); +} + +HWTEST_F(DCameraAllConnectManagerTest, ApplyAdvancedResource_002, testing::ext::TestSize.Level2) +{ + auto ret = DCameraAllConnectManager::GetInstance().ApplyAdvancedResource(PEER_NETWORK_ID, nullptr); + EXPECT_EQ(ret, DistributedCameraErrno::DCAMERA_ERR_APPLY_RESULT); + + std::string emptyPeerNetworkId; + DCameraCollaborationResourceRequestInfoSets resultCallback; + ret = DCameraAllConnectManager::GetInstance().ApplyAdvancedResource(emptyPeerNetworkId, &resultCallback); + EXPECT_EQ(ret, DistributedCameraErrno::DCAMERA_ERR_APPLY_RESULT); +} + +HWTEST_F(DCameraAllConnectManagerTest, RegisterLifecycleCallback_001, testing::ext::TestSize.Level1) +{ + auto ret = DCameraAllConnectManager::GetInstance().RegisterLifecycleCallback(); + EXPECT_EQ(ret, DistributedCameraErrno::DCAMERA_OK); + ret = DCameraAllConnectManager::GetInstance().UnRegisterLifecycleCallback(); + EXPECT_EQ(ret, DistributedCameraErrno::DCAMERA_OK); +} + +HWTEST_F(DCameraAllConnectManagerTest, BuildResourceRequest_001, testing::ext::TestSize.Level1) +{ + auto ret = DCameraAllConnectManager::GetInstance().BuildResourceRequest(); + EXPECT_TRUE(ret != nullptr); +} + +HWTEST_F(DCameraAllConnectManagerTest, SinkSideScenario_001, testing::ext::TestSize.Level1) +{ + DCameraAllConnectManager::SetSinkNetWorkId(PEER_NETWORK_ID, VALUABLE_SESSION_ID); + auto targetNetworkId = DCameraAllConnectManager::GetSinkDevIdBySocket(VALUABLE_SESSION_ID); + EXPECT_STREQ(targetNetworkId.c_str(), PEER_NETWORK_ID.c_str()); + DCameraAllConnectManager::RemoveSinkNetworkId(VALUABLE_SESSION_ID); +} + +HWTEST_F(DCameraAllConnectManagerTest, SinkSideScenario_002, testing::ext::TestSize.Level2) +{ + DCameraAllConnectManager::SetSinkNetWorkId(std::string(), UNVALUABLE_SESSION_ID); + DCameraAllConnectManager::SetSinkNetWorkId(PEER_NETWORK_ID, UNVALUABLE_SESSION_ID); + auto targetNetworkId = DCameraAllConnectManager::GetSinkDevIdBySocket(UNVALUABLE_SESSION_ID); + EXPECT_TRUE(targetNetworkId.empty()); + DCameraAllConnectManager::RemoveSinkNetworkId(VALUABLE_SESSION_ID); +} + +HWTEST_F(DCameraAllConnectManagerTest, SourceSideScenario_001, testing::ext::TestSize.Level1) +{ + DCameraAllConnectManager::SetSourceNetworkId(PEER_NETWORK_ID, VALUABLE_SESSION_ID); + auto targetSessionId = DCameraAllConnectManager::GetSourceSocketByNetworkId(PEER_NETWORK_ID); + EXPECT_EQ(targetSessionId, VALUABLE_SESSION_ID); + DCameraAllConnectManager::RemoveSourceNetworkId(VALUABLE_SESSION_ID); +} + +HWTEST_F(DCameraAllConnectManagerTest, SourceSideScenario_002, testing::ext::TestSize.Level2) +{ + DCameraAllConnectManager::SetSourceNetworkId(std::string(), UNVALUABLE_SESSION_ID); + DCameraAllConnectManager::SetSourceNetworkId(PEER_NETWORK_ID, UNVALUABLE_SESSION_ID); + auto targetSessionId = DCameraAllConnectManager::GetSourceSocketByNetworkId(PEER_NETWORK_ID); + EXPECT_EQ(targetSessionId, UNVALUABLE_SESSION_ID); + DCameraAllConnectManager::RemoveSourceNetworkId(UNVALUABLE_SESSION_ID); +} +} // namespace DistributedHardware +} // OHOS diff --git a/services/channel/test/unittest/common/channel/dlfcn_mock.h b/services/channel/test/unittest/common/channel/dlfcn_mock.h new file mode 100644 index 00000000..135c38db --- /dev/null +++ b/services/channel/test/unittest/common/channel/dlfcn_mock.h @@ -0,0 +1,39 @@ +/* + * 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_DCAMERA_DLFCN_MOCK_H +#define OHOS_DCAMERA_DLFCN_MOCK_H + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +class DlfcnMock { +public: + DlfcnMock() = default; + virtual ~DlfcnMock() = default; + + static std::shared_ptr GetOrCreateInstance(); + static void ReleaseInstance(); + MOCK_METHOD(void *, DlopenMock, (const char *, int)); + MOCK_METHOD(int, DlcloseMock, (void *)); + MOCK_METHOD(void *, DlsymMock, (void *__restrict, const char *__restrict)); +private: + static std::shared_ptr instance_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DCAMERA_DLFCN_MOCK_H diff --git a/services/channel/test/unittest/common/channel/lib_function_mock.cpp b/services/channel/test/unittest/common/channel/lib_function_mock.cpp new file mode 100644 index 00000000..68b00485 --- /dev/null +++ b/services/channel/test/unittest/common/channel/lib_function_mock.cpp @@ -0,0 +1,63 @@ +/* + * 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 +#include +#include + +#include "dlfcn_mock.h" + +char *realpath(const char *path, char *resolvedPath) +{ + return resolvedPath; +} + +void *dlopen(const char *file, int mode) +{ + auto instance = OHOS::DistributedHardware::DlfcnMock::GetOrCreateInstance(); + return instance->DlopenMock(file, mode); +} + +int dlclose(void *handle) +{ + auto instance = OHOS::DistributedHardware::DlfcnMock::GetOrCreateInstance(); + return instance->DlcloseMock(handle); +} + +void *dlsym(void *__restrict handle, const char *__restrict name) +{ + auto instance = OHOS::DistributedHardware::DlfcnMock::GetOrCreateInstance(); + return instance->DlsymMock(handle, name); +} + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr DlfcnMock::instance_; + +std::shared_ptr DlfcnMock::GetOrCreateInstance() +{ + if (!instance_) { + instance_ = std::make_shared(); + } + return instance_; +} + +void DlfcnMock::ReleaseInstance() +{ + instance_.reset(); + instance_ = nullptr; +} +} // namespace DistributedHardware +} // namespace OHOS -- Gitee