From e49d38012c0001915f3c4b63f2f822f91a5867bf Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Thu, 20 Mar 2025 15:18:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=88=86=E5=B8=83=E5=BC=8F?= =?UTF-8?q?=E7=A1=AC=E4=BB=B6=E6=A1=86=E6=9E=B6enable=E4=B8=8Edisable?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=9C=BA=E6=99=AF=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 --- .../component_manager/BUILD.gn | 51 +- .../include/component_manager_test_ext.h | 46 ++ .../include/mock_capability_info_manager.h | 49 ++ .../include/mock_component_loader.h | 59 ++ .../include/mock_dh_context.h | 46 ++ .../include/mock_dh_utils_tool.h | 42 ++ .../include/mock_hardware_handler.h | 39 ++ .../include/mock_hardware_status_listener.h | 60 ++ .../include/mock_idistributed_hardware_sink.h | 43 +- .../mock_idistributed_hardware_source.h | 52 +- .../include/mock_meta_info_manager.h | 43 ++ .../include/mock_version_manager.h | 42 ++ .../src/component_manager_test.cpp | 2 - .../src/component_manager_test_ext.cpp | 548 ++++++++++++++++++ .../src/mock_capability_info_manager.cpp | 48 ++ .../src/mock_component_disable.cpp | 33 ++ .../src/mock_component_enable.cpp | 33 ++ .../src/mock_component_loader.cpp | 81 +++ .../component_manager/src/mock_dh_context.cpp | 51 ++ .../src/mock_dh_utils_tool.cpp | 41 ++ .../src/mock_meta_info_manager.cpp | 41 ++ .../src/mock_version_manager.cpp | 41 ++ 22 files changed, 1417 insertions(+), 74 deletions(-) create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test_ext.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_capability_info_manager.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_component_loader.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_dh_context.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_dh_utils_tool.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_hardware_handler.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_hardware_status_listener.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_meta_info_manager.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_version_manager.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_capability_info_manager.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_disable.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_enable.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_loader.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_dh_context.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_dh_utils_tool.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_meta_info_manager.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_version_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 f9944a67..ef959afd 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn @@ -84,7 +84,56 @@ ohos_unittest("ComponentManagerTest") { } } +ohos_unittest("ComponentManagerTestExt") { + module_out_path = module_out_path + + sources = [ + "src/component_manager_test_ext.cpp", + "src/mock_capability_info_manager.cpp", + "src/mock_component_disable.cpp", + "src/mock_component_enable.cpp", + "src/mock_component_loader.cpp", + "src/mock_dh_context.cpp", + "src/mock_dh_utils_tool.cpp", + "src/mock_meta_info_manager.cpp", + "src/mock_version_manager.cpp", + ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "cJSON:cjson", + "c_utils:utils", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "googletest:gmock", + "googletest:gmock_main", + "hilog:libhilog", + "ipc:ipc_single", + "kv_store:distributeddata_inner", + "samgr:samgr_proxy", + ] +} + group("component_manager_test") { testonly = true - deps = [ ":ComponentManagerTest" ] + deps = [ + ":ComponentManagerTest", + ":ComponentManagerTestExt", + ] } 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 new file mode 100644 index 00000000..6c39e7bf --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test_ext.h @@ -0,0 +1,46 @@ +/* + * 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_COMPONENT_MANAGER_TEST_EXT_H +#define OHOS_DISTRIBUTED_HARDWARE_COMPONENT_MANAGER_TEST_EXT_H + +#include "gtest/gtest.h" +#include "component_manager.h" +#include "mock_capability_info_manager.h" +#include "mock_component_loader.h" +#include "mock_dh_context.h" +#include "mock_dh_utils_tool.h" +#include "mock_meta_info_manager.h" +#include "mock_version_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class ComponentManagerTestExt : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +private: + std::shared_ptr capabilityInfoManager_; + std::shared_ptr componentLoader_; + std::shared_ptr dhContext_; + std::shared_ptr utilTool_; + std::shared_ptr metaInfoManager_; + std::shared_ptr versionManager_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_COMPONENT_MANAGER_TEST_EXT_H diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_capability_info_manager.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_capability_info_manager.h new file mode 100644 index 00000000..425466ea --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_capability_info_manager.h @@ -0,0 +1,49 @@ +/* + * 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_CAPABILITY_INFO_MANAGER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_CAPABILITY_INFO_MANAGER_H + +#include + +#include "capability_info_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class ICapabilityInfoManager { +public: + virtual ~ICapabilityInfoManager() = default; + + virtual int32_t GetCapability(const std::string &deviceId, const std::string &dhId, + std::shared_ptr &capPtr) = 0; + virtual void GetCapabilitiesByDeviceId(const std::string &deviceId, + std::vector> &resInfos); + static std::shared_ptr GetOrCtreateInstance(); + static void ReleaseInstance(); + +private: + static std::shared_ptr CapabilityInfoManagerInstance_; +}; + +class MockCapabilityInfoManager : public ICapabilityInfoManager { +public: + MOCK_METHOD(int32_t, GetCapability, (const std::string &, const std::string &, + std::shared_ptr &)); + MOCK_METHOD(void, GetCapabilitiesByDeviceId, (const std::string &, + std::vector> &)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_CAPABILITY_INFO_MANAGER_H diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_component_loader.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_component_loader.h new file mode 100644 index 00000000..5452c42c --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_component_loader.h @@ -0,0 +1,59 @@ +/* + * 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_COMPONENT_LOADER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_COMPONENT_LOADER_H + +#include + +#include "component_loader.h" + +namespace OHOS { +namespace DistributedHardware { +class IComponentLoader { +public: + virtual ~IComponentLoader() = default; + + virtual bool IsDHTypeSupport(DHType dhType) = 0; + virtual int32_t GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr) = 0; + virtual int32_t GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr) = 0; + virtual int32_t ReleaseSink(const DHType dhType) = 0; + virtual std::vector GetAllCompTypes() = 0; + virtual int32_t GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr) = 0; + virtual int32_t GetSourceSaId(const DHType dhType) = 0; + virtual int32_t ReleaseSource(const DHType dhType) = 0; + virtual int32_t GetLocalDHVersion(DHVersion &dhVersion) = 0; + + static std::shared_ptr GetOrCtreateInstance(); + static void ReleaseInstance(); +private: + static std::shared_ptr componentLoaderInstance_; +}; + +class MockComponentLoader : public IComponentLoader { +public: + MOCK_METHOD(bool, IsDHTypeSupport, (DHType)); + MOCK_METHOD(int32_t, GetSink, (const DHType, IDistributedHardwareSink *&)); + MOCK_METHOD(int32_t, GetHardwareHandler, (const DHType, IHardwareHandler *&)); + MOCK_METHOD(int32_t, ReleaseSink, (const DHType)); + MOCK_METHOD((std::vector), GetAllCompTypes, ()); + MOCK_METHOD(int32_t, GetSource, (const DHType, IDistributedHardwareSource *&)); + MOCK_METHOD(int32_t, GetSourceSaId, (const DHType)); + MOCK_METHOD(int32_t, ReleaseSource, (const DHType)); + MOCK_METHOD(int32_t, GetLocalDHVersion, (DHVersion &)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_COMPONENT_LOADER_H diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_dh_context.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_dh_context.h new file mode 100644 index 00000000..90548c78 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_dh_context.h @@ -0,0 +1,46 @@ +/* + * 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_DH_CONTEXT_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_DH_CONTEXT_H + +#include + +#include "dh_context.h" + +namespace OHOS { +namespace DistributedHardware { +class IDHContext { +public: + virtual ~IDHContext() = default; + + virtual const DeviceInfo& GetDeviceInfo() = 0; + virtual std::string GetUUIDByNetworkId(const std::string &networkId) = 0; + virtual std::string GetUUIDByDeviceId(const std::string &deviceId) = 0; + static std::shared_ptr GetOrCtreateInstance(); + static void ReleaseInstance(); +private: + static std::shared_ptr dHContextInstance_; +}; + +class MockDHContext : public IDHContext { +public: + MOCK_METHOD(const DeviceInfo&, GetDeviceInfo, ()); + MOCK_METHOD(std::string, GetUUIDByNetworkId, (const std::string &)); + MOCK_METHOD(std::string, GetUUIDByDeviceId, (const std::string &)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_DH_CONTEXT_H diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_dh_utils_tool.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_dh_utils_tool.h new file mode 100644 index 00000000..b7b742be --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_dh_utils_tool.h @@ -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. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_MOCK_DH_UTILS_TOOL_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_DH_UTILS_TOOL_H + +#include + +#include "dh_utils_tool.h" + +namespace OHOS { +namespace DistributedHardware { +class IDHUtilTool { +public: + virtual ~IDHUtilTool() = default; + + virtual DeviceInfo GetLocalDeviceInfo() = 0; + static std::shared_ptr GetOrCtreateInstance(); + static void ReleaseInstance(); +private: + static std::shared_ptr UtilToolInstance_; +}; + +class MockDHUtilTool : public IDHUtilTool { +public: + MOCK_METHOD(DeviceInfo, GetLocalDeviceInfo, ()); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_DH_UTILS_TOOL_H diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_hardware_handler.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_hardware_handler.h new file mode 100644 index 00000000..9b06fbc5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_hardware_handler.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_DISTRIBUTED_HARDWARE_MOCK_HARDWARE_HANDLER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_HARDWARE_HANDLER_H + +#include + +#include "ihardware_handler.h" + +namespace OHOS { +namespace DistributedHardware { +class MockHardwareHandler : public IHardwareHandler { +public: + virtual ~MockHardwareHandler() = default; + + MOCK_METHOD(int32_t, Initialize, ()); + MOCK_METHOD((std::vector), QueryMeta, ()); + MOCK_METHOD((std::vector), Query, ()); + MOCK_METHOD((std::map), QueryExtraInfo, ()); + MOCK_METHOD(bool, IsSupportPlugin, ()); + MOCK_METHOD(void, RegisterPluginListener, (std::shared_ptr)); + MOCK_METHOD(void, UnRegisterPluginListener, ()); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_HARDWARE_HANDLER_H diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_hardware_status_listener.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_hardware_status_listener.h new file mode 100644 index 00000000..864a9107 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_hardware_status_listener.h @@ -0,0 +1,60 @@ +/* + * 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_HARDWARE_STATUS_LISTENER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_HARDWARE_STATUS_LISTENER_H + +#include + +#include "ihardware_status_listener.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class MockHDSinkStatusListenerProxy : public IRemoteProxy { +public: + virtual ~MockHDSinkStatusListenerProxy() = default; + + MOCK_METHOD(void, OnEnable, (const DHDescriptor &)); + MOCK_METHOD(void, OnDisable, (const DHDescriptor &)); +}; + +class MockHDSourceStatusListenerProxy : public IRemoteProxy { +public: + virtual ~MockHDSourceStatusListenerProxy() = default; + + MOCK_METHOD(void, OnEnable, (const std::string &, const DHDescriptor &)); + MOCK_METHOD(void, OnDisable, (const std::string &, const DHDescriptor &)); +}; + +class MockHDSinkStatusListenerStub : public IRemoteStub { +public: + virtual ~MockHDSinkStatusListenerStub() = default; + + MOCK_METHOD(void, OnEnable, (const DHDescriptor &)); + MOCK_METHOD(void, OnDisable, (const DHDescriptor &)); +}; + +class MockHDSourceStatusListenerStub : public IRemoteStub { +public: + virtual ~MockHDSourceStatusListenerStub() = default; + + MOCK_METHOD(void, OnEnable, (const std::string &, const DHDescriptor &)); + MOCK_METHOD(void, OnDisable, (const std::string &, const DHDescriptor &)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_HARDWARE_STATUS_LISTENER_H diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_idistributed_hardware_sink.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_idistributed_hardware_sink.h index ddb1439a..ce8d7648 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_idistributed_hardware_sink.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_idistributed_hardware_sink.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -16,6 +16,7 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_MOCK_IDISTRIBUTED_HARDWARE_SINK_H #define OHOS_DISTRIBUTED_HARDWARE_MOCK_IDISTRIBUTED_HARDWARE_SINK_H +#include #include #include "idistributed_hardware_sink.h" @@ -25,38 +26,14 @@ namespace DistributedHardware { class MockIDistributedHardwareSink : public IDistributedHardwareSink { public: virtual ~MockIDistributedHardwareSink() {} - int32_t InitSink(const std::string ¶ms) - { - return 0; - } - int32_t ReleaseSink() - { - return 0; - } - int32_t SubscribeLocalHardware(const std::string &dhId, const std::string ¶meter) - { - return 0; - } - int32_t UnsubscribeLocalHardware(const std::string &dhId) - { - return 0; - } - int32_t RegisterPrivacyResources(std::shared_ptr listener) - { - return 0; - } - int32_t PauseDistributedHardware(const std::string &networkId) - { - return 0; - } - int32_t ResumeDistributedHardware(const std::string &networkId) - { - return 0; - } - int32_t StopDistributedHardware(const std::string &networkId) - { - return 0; - } + MOCK_METHOD(int32_t, InitSink, (const std::string &)); + MOCK_METHOD(int32_t, ReleaseSink, ()); + MOCK_METHOD(int32_t, SubscribeLocalHardware, (const std::string &, const std::string &)); + MOCK_METHOD(int32_t, UnsubscribeLocalHardware, (const std::string &)); + MOCK_METHOD(int32_t, RegisterPrivacyResources, (std::shared_ptr)); + MOCK_METHOD(int32_t, PauseDistributedHardware, (const std::string &)); + MOCK_METHOD(int32_t, ResumeDistributedHardware, (const std::string &)); + MOCK_METHOD(int32_t, StopDistributedHardware, (const std::string &)); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_idistributed_hardware_source.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_idistributed_hardware_source.h index a187059e..5d8ca265 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_idistributed_hardware_source.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_idistributed_hardware_source.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -16,6 +16,7 @@ #ifndef OHOS_MOCK_IDISTRIBUTED_HARDWARE_SOURCE_H #define OHOS_MOCK_IDISTRIBUTED_HARDWARE_SOURCE_H +#include #include #include "idistributed_hardware_source.h" @@ -24,43 +25,18 @@ namespace DistributedHardware { class MockIDistributedHardwareSource : public IDistributedHardwareSource { public: virtual ~MockIDistributedHardwareSource() {} - int32_t InitSource(const std::string ¶ms) - { - return 0; - } - int32_t ReleaseSource() - { - return 0; - } - int32_t RegisterDistributedHardware(const std::string &uuid, const std::string &dhId, - const EnableParam ¶meters, std::shared_ptr callback) - { - return 0; - } - int32_t UnregisterDistributedHardware(const std::string &uuid, const std::string &dhId, - std::shared_ptr callback) - { - return 0; - } - int32_t ConfigDistributedHardware(const std::string &uuid, const std::string &dhId, const std::string &key, - const std::string &value) - { - return 0; - } - void RegisterDistributedHardwareStateListener(std::shared_ptr listener) - { - (void)listener; - } - void UnregisterDistributedHardwareStateListener() - { - } - void RegisterDataSyncTriggerListener(std::shared_ptr listener) - { - (void)listener; - } - void UnregisterDataSyncTriggerListener() - { - } + MOCK_METHOD(int32_t, InitSource, (const std::string &)); + MOCK_METHOD(int32_t, ReleaseSource, ()); + MOCK_METHOD(int32_t, RegisterDistributedHardware, (const std::string &, const std::string &, + const EnableParam &, std::shared_ptr)); + MOCK_METHOD(int32_t, UnregisterDistributedHardware, (const std::string &, const std::string &, + std::shared_ptr)); + MOCK_METHOD(int32_t, ConfigDistributedHardware, (const std::string &, const std::string &, const std::string &, + const std::string &)); + MOCK_METHOD(void, RegisterDistributedHardwareStateListener, (std::shared_ptr)); + MOCK_METHOD(void, UnregisterDistributedHardwareStateListener, ()); + MOCK_METHOD(void, RegisterDataSyncTriggerListener, (std::shared_ptr)); + MOCK_METHOD(void, UnregisterDataSyncTriggerListener, ()); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_meta_info_manager.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_meta_info_manager.h new file mode 100644 index 00000000..57e04e46 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_meta_info_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_META_INFO_MANAGER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_META_INFO_MANAGER_H + +#include + +#include "meta_info_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class IMetaInfoManager { +public: + virtual ~IMetaInfoManager() = default; + + virtual int32_t GetMetaDataByDHType(const DHType dhType, MetaCapInfoMap &metaInfoMap); + + static std::shared_ptr GetOrCtreateInstance(); + static void ReleaseInstance(); +private: + static std::shared_ptr metaInfoManagerInstance_; +}; + +class MockMetaInfoManager : public IMetaInfoManager { +public: + MOCK_METHOD(int32_t, GetMetaDataByDHType, (const DHType, MetaCapInfoMap &)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_META_INFO_MANAGER_H diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_version_manager.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_version_manager.h new file mode 100644 index 00000000..843b071c --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/mock_version_manager.h @@ -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. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_MOCK_VERSION_MANAGER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_VERSION_MANAGER_H + +#include + +#include "version_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class IVersionManager { +public: + virtual ~IVersionManager() = default; + virtual int32_t GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion) = 0; + + static std::shared_ptr GetOrCtreateInstance(); + static void ReleaseInstance(); +private: + static std::shared_ptr versionManagerInstance_; +}; + +class MockVersionManager : public IVersionManager { +public: + MOCK_METHOD(int32_t, GetCompVersion, (const std::string &, const DHType, CompVersion &)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_VERSION_MANAGER_H diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp index 4cee1319..ea7516b3 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp @@ -35,8 +35,6 @@ #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "local_capability_info_manager.h" -#include "mock_idistributed_hardware_sink.h" -#include "mock_idistributed_hardware_source.h" #include "meta_capability_info.h" #include "meta_info_manager.h" #include "nativetoken_kit.h" 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 new file mode 100644 index 00000000..7ebfdf7d --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp @@ -0,0 +1,548 @@ +/* + * 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 "component_manager_test_ext.h" +#include "distributed_hardware_errno.h" +#include "mock_hardware_handler.h" +#include "mock_hardware_status_listener.h" +#include "mock_idistributed_hardware_sink.h" +#include "mock_idistributed_hardware_source.h" + +using namespace testing; + +namespace OHOS { +namespace DistributedHardware { +namespace { +const DeviceInfo VALUABLE_DEVICE_INFO( + "networkId-1", + "uuid-1", + "deviceId-1", + "udid-1", + "udidHash-1", + "deviceName: com.ohos.helloworld", + 1 +); +constexpr int32_t CURRENT_DEVICE_UID = 10010; +constexpr int32_t CAMERA_UID = CURRENT_DEVICE_UID; +constexpr int32_t CAMERA_PID = 4083; +constexpr int32_t AUDIO_UID = CURRENT_DEVICE_UID; +constexpr int32_t AUDIO_PID = 4085; +const CompVersion VERSION = { .sinkVersion = "1.0", .sourceVersion = "1.0" }; +const DHDescriptor CAMERA_DESCRIPTOR = { .id = "camera_1", .dhType = DHType::CAMERA }; +const DHDescriptor AUDIO_DESCRIPTOR = { .id = "audio_1", .dhType = DHType::AUDIO }; + +std::shared_ptr CreateDHSinkPtrWithSetExpectation() +{ + auto sinkPtr = std::make_shared(); + EXPECT_CALL(*sinkPtr, InitSink(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + EXPECT_CALL(*sinkPtr, ReleaseSink()).WillRepeatedly(Return(DH_FWK_SUCCESS)); + EXPECT_CALL(*sinkPtr, RegisterPrivacyResources(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + return sinkPtr; +} + +std::shared_ptr CreateDHSourcePtrWithSetExpectation() +{ + auto sourcePtr = std::make_shared(); + EXPECT_CALL(*sourcePtr, InitSource(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + EXPECT_CALL(*sourcePtr, ReleaseSource()).WillRepeatedly(Return(DH_FWK_SUCCESS)); + EXPECT_CALL(*sourcePtr, RegisterDistributedHardwareStateListener(_)).Times(AtLeast(1)); + EXPECT_CALL(*sourcePtr, RegisterDataSyncTriggerListener(_)).Times(AtLeast(1)); + EXPECT_CALL(*sourcePtr, UnregisterDistributedHardwareStateListener()).Times(AtLeast(1)); + EXPECT_CALL(*sourcePtr, UnregisterDataSyncTriggerListener()).Times(AtLeast(1)); + return sourcePtr; +} +} // namespace + +void ComponentManagerTestExt::SetUpTestCase(void) +{} + +void ComponentManagerTestExt::TearDownTestCase(void) +{} + +void ComponentManagerTestExt::SetUp() +{ + auto capabilityInfoManager = ICapabilityInfoManager::GetOrCtreateInstance(); + capabilityInfoManager_ = std::static_pointer_cast(capabilityInfoManager); + auto componentLoader = IComponentLoader::GetOrCtreateInstance(); + componentLoader_ = std::static_pointer_cast(componentLoader); + auto dhContext = IDHContext::GetOrCtreateInstance(); + dhContext_ = std::static_pointer_cast(dhContext); + auto utilTool = IDHUtilTool::GetOrCtreateInstance(); + utilTool_ = std::static_pointer_cast(utilTool); + auto metaInfoManager = IMetaInfoManager::GetOrCtreateInstance(); + metaInfoManager_ = std::static_pointer_cast(metaInfoManager); + auto versionManager = IVersionManager::GetOrCtreateInstance(); + versionManager_ = std::static_pointer_cast(versionManager); +} + +void ComponentManagerTestExt::TearDown() +{ + ICapabilityInfoManager::ReleaseInstance(); + IComponentLoader::ReleaseInstance(); + IDHContext::ReleaseInstance(); + IDHUtilTool::ReleaseInstance(); + IMetaInfoManager::ReleaseInstance(); + IVersionManager::ReleaseInstance(); + capabilityInfoManager_ = nullptr; + componentLoader_ = nullptr; + dhContext_ = nullptr; + metaInfoManager_ = nullptr; + versionManager_ = nullptr; + utilTool_ = nullptr; +} + +HWTEST_F(ComponentManagerTestExt, EnableSinkAndDisableSink_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + ASSERT_TRUE(dhContext_ != nullptr); + ASSERT_TRUE(versionManager_ != nullptr); + + auto sinkPtr = CreateDHSinkPtrWithSetExpectation(); + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(true)); + EXPECT_CALL(*componentLoader_, GetSink(_, _)) + .WillRepeatedly(DoAll(SetArgReferee<1>(sinkPtr.get()), Return(DH_FWK_SUCCESS))); + + DeviceInfo emptyInfo("", "", "", "", "", "", 0); + EXPECT_CALL(*dhContext_, GetDeviceInfo()).WillRepeatedly(ReturnRef(emptyInfo)); + EXPECT_CALL(*versionManager_, GetCompVersion(_, _, _)).Times(AtLeast(1)); + + std::vector types = { DHType::CAMERA }; + auto sinkListener = sptr(new (std::nothrow) MockHDSinkStatusListenerStub()); + EXPECT_CALL(*sinkListener, OnEnable(_)).Times(AtLeast(1)); + EXPECT_CALL(*sinkListener, OnDisable(_)).Times(AtLeast(1)); + + EXPECT_CALL(*componentLoader_, GetAllCompTypes()).WillRepeatedly(Return(types)); + ComponentManager::GetInstance().RegisterDHStatusListener(sinkListener, CAMERA_UID, CAMERA_PID); + + auto ret = ComponentManager::GetInstance().EnableSink(CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = ComponentManager::GetInstance().EnableSink(CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_REPEAT_CALL); + + auto handler = std::make_shared(); + EXPECT_CALL(*componentLoader_, GetHardwareHandler(_, _)) + .WillRepeatedly(DoAll(SetArgReferee<1>(handler.get()), Return(DH_FWK_SUCCESS))); + EXPECT_CALL(*componentLoader_, ReleaseSink(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + EXPECT_CALL(*handler, UnRegisterPluginListener()).Times(AtLeast(1)); + + ret = ComponentManager::GetInstance().DisableSink(CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = ComponentManager::GetInstance().DisableSink(CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_REPEAT_CALL); + ret = ComponentManager::GetInstance().UnregisterDHStatusListener(sinkListener, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +HWTEST_F(ComponentManagerTestExt, EnableSinkAndDisableSink_002, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + ASSERT_TRUE(dhContext_ != nullptr); + ASSERT_TRUE(versionManager_ != nullptr); + + DeviceInfo emptyInfo("", "", "", "", "", "", 0); + EXPECT_CALL(*dhContext_, GetDeviceInfo()).WillRepeatedly(ReturnRef(emptyInfo)); + EXPECT_CALL(*versionManager_, GetCompVersion(_, _, _)).Times(AtLeast(1)); + + auto sinkPtr = CreateDHSinkPtrWithSetExpectation(); + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(true)); + EXPECT_CALL(*componentLoader_, GetSink(_, _)) + .WillRepeatedly(DoAll(SetArgReferee<1>(sinkPtr.get()), Return(DH_FWK_SUCCESS))); + + auto ret = ComponentManager::GetInstance().EnableSink(AUDIO_DESCRIPTOR, AUDIO_UID, AUDIO_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + + DHDescriptor newAudio = { .id = "aduio_2", .dhType = DHType::AUDIO }; + int32_t newAudioPid = AUDIO_PID + 1; + ComponentManager::GetInstance().EnableSink(newAudio, AUDIO_UID, newAudioPid); + + auto handler = std::make_shared(); + EXPECT_CALL(*componentLoader_, GetHardwareHandler(_, _)) + .WillRepeatedly(DoAll(SetArgReferee<1>(handler.get()), Return(DH_FWK_SUCCESS))); + EXPECT_CALL(*componentLoader_, ReleaseSink(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + EXPECT_CALL(*handler, UnRegisterPluginListener()).Times(AtLeast(1)); + + ComponentManager::GetInstance().DisableSink(newAudio, AUDIO_UID, newAudioPid); + ret = ComponentManager::GetInstance().DisableSink(AUDIO_DESCRIPTOR, AUDIO_UID, AUDIO_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +HWTEST_F(ComponentManagerTestExt, EnableSink_failed_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + size_t isDHTypeSupportCallCount = 0; + size_t getSinkCallCount = 0; + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)) + .WillRepeatedly(Invoke([&isDHTypeSupportCallCount](DHType) { + return (isDHTypeSupportCallCount++ > 0) ? true : false; + })); + EXPECT_CALL(*componentLoader_, GetSink(_, _)) + .WillRepeatedly(Invoke([&getSinkCallCount](const DHType dhType, IDistributedHardwareSink *&sinkPtr) { + sinkPtr = nullptr; + return (getSinkCallCount++ > 0) ? DH_FWK_SUCCESS : ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + })); + for (size_t i = 0; i < 3; ++i) { + auto ret = ComponentManager::GetInstance().EnableSink(CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + if (isDHTypeSupportCallCount > 0 && getSinkCallCount == 0) { + EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST); + } else { + EXPECT_EQ(ret, ERR_DH_FWK_LOADER_HANDLER_IS_NULL); + } + } +} + +HWTEST_F(ComponentManagerTestExt, DisableSink_failed_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(false)); + auto ret = ComponentManager::GetInstance().DisableSink(CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST); +} + +HWTEST_F(ComponentManagerTestExt, EnableSourceAndDisableSource_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(capabilityInfoManager_ != nullptr); + ASSERT_TRUE(componentLoader_ != nullptr); + ASSERT_TRUE(dhContext_ != nullptr); + ASSERT_TRUE(versionManager_ != nullptr); + ASSERT_TRUE(utilTool_ != nullptr); + + std::vector types = { DHType::CAMERA }; + auto sourceListener = sptr(new (std::nothrow) MockHDSourceStatusListenerStub()); + EXPECT_CALL(*sourceListener, OnEnable(_, _)).Times(AtLeast(1)); + EXPECT_CALL(*sourceListener, OnDisable(_, _)).Times(AtLeast(1)); + EXPECT_CALL(*componentLoader_, GetAllCompTypes()).WillRepeatedly(Return(types)); + ComponentManager::GetInstance().RegisterDHStatusListener(VALUABLE_DEVICE_INFO.networkId, + sourceListener, CAMERA_UID, CAMERA_PID); + + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(true)); + auto sourcePtr = CreateDHSourcePtrWithSetExpectation(); + EXPECT_CALL(*componentLoader_, GetSource(_, _)) + .WillRepeatedly(DoAll(SetArgReferee<1>(sourcePtr.get()), Return(DH_FWK_SUCCESS))); + EXPECT_CALL(*componentLoader_, GetSourceSaId(_)).WillRepeatedly(Return(CAMERA_PID)); + + auto capabilityInfo = std::make_shared(); + 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)); + + auto ret = ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, + CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, + CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_REPEAT_CALL); + + EXPECT_CALL(*componentLoader_, ReleaseSource(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + ret = ComponentManager::GetInstance().DisableSource(VALUABLE_DEVICE_INFO.networkId, + CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = ComponentManager::GetInstance().DisableSource(VALUABLE_DEVICE_INFO.networkId, + CAMERA_DESCRIPTOR, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_REPEAT_CALL); + + ret = ComponentManager::GetInstance().UnregisterDHStatusListener(VALUABLE_DEVICE_INFO.networkId, + sourceListener, CAMERA_UID, CAMERA_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +HWTEST_F(ComponentManagerTestExt, EnableSourceAndDisableSource_002, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(capabilityInfoManager_ != nullptr); + ASSERT_TRUE(componentLoader_ != nullptr); + ASSERT_TRUE(dhContext_ != nullptr); + ASSERT_TRUE(versionManager_ != nullptr); + ASSERT_TRUE(utilTool_ != nullptr); + + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(true)); + auto sourcePtr = CreateDHSourcePtrWithSetExpectation(); + EXPECT_CALL(*componentLoader_, GetSource(_, _)) + .WillRepeatedly(DoAll(SetArgReferee<1>(sourcePtr.get()), Return(DH_FWK_SUCCESS))); + + EXPECT_CALL(*componentLoader_, GetSourceSaId(_)).WillRepeatedly(Return(CAMERA_PID)); + + auto capabilityInfo = std::make_shared(); + 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)); + + auto ret = ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, + AUDIO_DESCRIPTOR, AUDIO_UID, AUDIO_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + + DHDescriptor newAudio = { .id = "aduio_2", .dhType = DHType::AUDIO }; + int32_t newAudioPid = AUDIO_PID + 1; + ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, newAudio, AUDIO_UID, newAudioPid); + + EXPECT_CALL(*componentLoader_, ReleaseSource(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + ComponentManager::GetInstance().DisableSource(VALUABLE_DEVICE_INFO.networkId, newAudio, AUDIO_UID, newAudioPid); + ret = ComponentManager::GetInstance().DisableSource(VALUABLE_DEVICE_INFO.networkId, + AUDIO_DESCRIPTOR, AUDIO_UID, AUDIO_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +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); + } + } +} + +HWTEST_F(ComponentManagerTestExt, DisableSource_failed_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(false)); + auto ret = ComponentManager::GetInstance().DisableSource(VALUABLE_DEVICE_INFO.networkId, + AUDIO_DESCRIPTOR, AUDIO_UID, AUDIO_PID); + EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST); +} + +HWTEST_F(ComponentManagerTestExt, EnableSinkAndForceDisableSink_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + ASSERT_TRUE(dhContext_ != nullptr); + ASSERT_TRUE(versionManager_ != nullptr); + + auto sinkPtr = CreateDHSinkPtrWithSetExpectation(); + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(true)); + EXPECT_CALL(*componentLoader_, GetSink(_, _)) + .WillRepeatedly(DoAll(SetArgReferee<1>(sinkPtr.get()), Return(DH_FWK_SUCCESS))); + + DeviceInfo emptyInfo("", "", "", "", "", "", 0); + EXPECT_CALL(*dhContext_, GetDeviceInfo()).WillRepeatedly(ReturnRef(emptyInfo)); + EXPECT_CALL(*versionManager_, GetCompVersion(_, _, _)).Times(AtLeast(1)); + + std::vector types = { DHType::AUDIO }; + auto sinkListener = sptr(new (std::nothrow) MockHDSinkStatusListenerStub()); + EXPECT_CALL(*sinkListener, OnEnable(_)).Times(AtLeast(1)); + EXPECT_CALL(*sinkListener, OnDisable(_)).Times(AtLeast(1)); + + EXPECT_CALL(*componentLoader_, GetAllCompTypes()).WillRepeatedly(Return(types)); + ComponentManager::GetInstance().RegisterDHStatusListener(sinkListener, AUDIO_UID, AUDIO_PID); + + ComponentManager::GetInstance().EnableSink(AUDIO_DESCRIPTOR, AUDIO_UID, AUDIO_PID); + DHDescriptor newAudio = { .id = "aduio_2", .dhType = DHType::AUDIO }; + int32_t newAudioPid = AUDIO_PID + 1; + ComponentManager::GetInstance().EnableSink(newAudio, AUDIO_UID, newAudioPid); + + auto handler = std::make_shared(); + EXPECT_CALL(*componentLoader_, GetHardwareHandler(_, _)) + .WillRepeatedly(DoAll(SetArgReferee<1>(handler.get()), Return(DH_FWK_SUCCESS))); + EXPECT_CALL(*componentLoader_, ReleaseSink(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + EXPECT_CALL(*handler, UnRegisterPluginListener()).Times(AtLeast(1)); + + auto ret = ComponentManager::GetInstance().ForceDisableSink(newAudio); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = ComponentManager::GetInstance().ForceDisableSink(AUDIO_DESCRIPTOR); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = ComponentManager::GetInstance().UnregisterDHStatusListener(sinkListener, AUDIO_UID, AUDIO_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +HWTEST_F(ComponentManagerTestExt, ForceDisableSink_failed_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(false)); + auto ret = ComponentManager::GetInstance().ForceDisableSink(CAMERA_DESCRIPTOR); + EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST); +} + +HWTEST_F(ComponentManagerTestExt, EnableSourceAndForceDisableSource_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(capabilityInfoManager_ != nullptr); + ASSERT_TRUE(componentLoader_ != nullptr); + ASSERT_TRUE(dhContext_ != nullptr); + ASSERT_TRUE(versionManager_ != nullptr); + ASSERT_TRUE(utilTool_ != nullptr); + + std::vector types = { DHType::AUDIO }; + auto sourceListener = sptr(new (std::nothrow) MockHDSourceStatusListenerStub()); + EXPECT_CALL(*sourceListener, OnEnable(_, _)).Times(AtLeast(1)); + EXPECT_CALL(*sourceListener, OnDisable(_, _)).Times(AtLeast(1)); + EXPECT_CALL(*componentLoader_, GetAllCompTypes()).WillRepeatedly(Return(types)); + ComponentManager::GetInstance().RegisterDHStatusListener(VALUABLE_DEVICE_INFO.networkId, + sourceListener, AUDIO_UID, AUDIO_PID); + + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(true)); + auto sourcePtr = CreateDHSourcePtrWithSetExpectation(); + EXPECT_CALL(*componentLoader_, GetSource(_, _)) + .WillRepeatedly(DoAll(SetArgReferee<1>(sourcePtr.get()), Return(DH_FWK_SUCCESS))); + EXPECT_CALL(*componentLoader_, GetSourceSaId(_)).WillRepeatedly(Return(CAMERA_PID)); + auto capabilityInfo = std::make_shared(); + 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)); + + ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, + AUDIO_DESCRIPTOR, AUDIO_UID, AUDIO_PID); + DHDescriptor newAudio = { .id = "aduio_2", .dhType = DHType::AUDIO }; + int32_t newAudioPid = AUDIO_PID + 1; + ComponentManager::GetInstance().EnableSource(VALUABLE_DEVICE_INFO.networkId, newAudio, AUDIO_UID, newAudioPid); + + EXPECT_CALL(*componentLoader_, ReleaseSource(_)).WillRepeatedly(Return(DH_FWK_SUCCESS)); + auto ret = ComponentManager::GetInstance().ForceDisableSource(VALUABLE_DEVICE_INFO.networkId, newAudio); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = ComponentManager::GetInstance().ForceDisableSource(VALUABLE_DEVICE_INFO.networkId, AUDIO_DESCRIPTOR); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = ComponentManager::GetInstance().UnregisterDHStatusListener(VALUABLE_DEVICE_INFO.networkId, + sourceListener, AUDIO_UID, AUDIO_PID); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +HWTEST_F(ComponentManagerTestExt, ForceDisableSource_failed_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + EXPECT_CALL(*componentLoader_, IsDHTypeSupport(_)).WillRepeatedly(Return(false)); + auto ret = ComponentManager::GetInstance().ForceDisableSource(VALUABLE_DEVICE_INFO.networkId, CAMERA_DESCRIPTOR); + EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST); +} + +HWTEST_F(ComponentManagerTestExt, CheckDemandStart_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(componentLoader_ != nullptr); + ASSERT_TRUE(dhContext_ != nullptr); + ASSERT_TRUE(metaInfoManager_ != nullptr); + + DHType targetType = DHType::CAMERA; + CompVersion localVersion = { + .sourceVersion = "1.0", + .sinkVersion = "1.0", + .haveFeature = true, + .sourceFeatureFilters = { "yes" }, + .sinkSupportedFeatures = { "yes" } + }; + CompVersion remoteVersion = { + .sourceVersion = "1.0", + .sinkVersion = "1.0", + .haveFeature = true, + .sourceFeatureFilters = { "yes" }, + .sinkSupportedFeatures = { "yes" } + }; + DHVersion dhVersion = { + .uuid = VALUABLE_DEVICE_INFO.uuid, + .dhVersion = "1.0", + .compVersions = {{ targetType, localVersion }} + }; + auto metaCapabilityInfo = std::make_shared(); + metaCapabilityInfo->SetCompVersion(remoteVersion); + 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(DoAll(SetArgReferee<0>(dhVersion), Return(DH_FWK_SUCCESS))); + + bool isEnableSink = false; + bool isEnableSource = false; + auto ret = ComponentManager::GetInstance().CheckDemandStart( + VALUABLE_DEVICE_INFO.uuid, targetType, isEnableSink, isEnableSource); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + EXPECT_TRUE(isEnableSink); + EXPECT_TRUE(isEnableSource); +} + +HWTEST_F(ComponentManagerTestExt, CheckDemandStart_002, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(metaInfoManager_ != nullptr); + + size_t getMetaDataByDHTypeCallCount = 0; + EXPECT_CALL(*metaInfoManager_, GetMetaDataByDHType(_, _)) + .WillRepeatedly(Invoke([&getMetaDataByDHTypeCallCount](const DHType dhType, MetaCapInfoMap &metaInfoMap) { + MetaCapInfoMap emptyInfo; + metaInfoMap = emptyInfo; + return (getMetaDataByDHTypeCallCount++ > 0) ? DH_FWK_SUCCESS : ERR_DH_FWK_COMPONENT_COMPVERSION_NOT_FOUND; + })); + + for (size_t i = 0; i < 2; ++i) { + bool isEnableSink = false; + bool isEnableSource = false; + auto ret = ComponentManager::GetInstance().CheckDemandStart( + VALUABLE_DEVICE_INFO.uuid, DHType::CAMERA, isEnableSink, isEnableSource); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_COMPVERSION_NOT_FOUND); + } +} + +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 isEnableSink = false; + bool isEnableSource = false; + auto ret = ComponentManager::GetInstance().CheckDemandStart( + VALUABLE_DEVICE_INFO.uuid, DHType::CAMERA, isEnableSink, isEnableSource); + EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST); + } +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_capability_info_manager.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_capability_info_manager.cpp new file mode 100644 index 00000000..8096d3b2 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_capability_info_manager.cpp @@ -0,0 +1,48 @@ +/* + * 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_capability_info_manager.h" + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr ICapabilityInfoManager::CapabilityInfoManagerInstance_; + +std::shared_ptr ICapabilityInfoManager::GetOrCtreateInstance() +{ + if (!CapabilityInfoManagerInstance_) { + CapabilityInfoManagerInstance_ = std::make_shared(); + } + return CapabilityInfoManagerInstance_; +} + +void ICapabilityInfoManager::ReleaseInstance() +{ + CapabilityInfoManagerInstance_.reset(); + CapabilityInfoManagerInstance_ = nullptr; +} + +int32_t CapabilityInfoManager::GetCapability(const std::string &deviceId, const std::string &dhId, + std::shared_ptr &capPtr) +{ + return ICapabilityInfoManager::GetOrCtreateInstance()->GetCapability(deviceId, dhId, capPtr); +} + +void CapabilityInfoManager::GetCapabilitiesByDeviceId(const std::string &deviceId, + std::vector> &resInfos) +{ + ICapabilityInfoManager::GetOrCtreateInstance()->GetCapabilitiesByDeviceId(deviceId, resInfos); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_disable.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_disable.cpp new file mode 100644 index 00000000..b383c546 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_disable.cpp @@ -0,0 +1,33 @@ +/* + * 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 "component_disable.h" +#include "distributed_hardware_errno.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t ComponentDisable::Disable(const std::string &networkId, const std::string &dhId, + IDistributedHardwareSource *handler) +{ + return DH_FWK_SUCCESS; +} + +int32_t ComponentDisable::OnUnregisterResult(const std::string &networkId, const std::string &dhId, + int32_t status, const std::string &data) +{ + return DH_FWK_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_enable.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_enable.cpp new file mode 100644 index 00000000..21728b39 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_enable.cpp @@ -0,0 +1,33 @@ +/* + * 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 "component_enable.h" +#include "distributed_hardware_errno.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t ComponentEnable::Enable(const std::string &networkId, const std::string &dhId, const EnableParam ¶m, + IDistributedHardwareSource *handler) +{ + return DH_FWK_SUCCESS; +} + +int32_t ComponentEnable::OnRegisterResult(const std::string &networkId, const std::string &dhId, int32_t status, + const std::string &data) +{ + return DH_FWK_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_loader.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_loader.cpp new file mode 100644 index 00000000..afb5e45e --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_component_loader.cpp @@ -0,0 +1,81 @@ +/* + * 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_component_loader.h" + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr IComponentLoader::componentLoaderInstance_; + +std::shared_ptr IComponentLoader::GetOrCtreateInstance() +{ + if (!componentLoaderInstance_) { + componentLoaderInstance_ = std::make_shared(); + } + return componentLoaderInstance_; +} + +void IComponentLoader::ReleaseInstance() +{ + componentLoaderInstance_.reset(); + componentLoaderInstance_ = nullptr; +} + +bool ComponentLoader::IsDHTypeSupport(DHType dhType) +{ + return IComponentLoader::GetOrCtreateInstance()->IsDHTypeSupport(dhType); +} + +int32_t ComponentLoader::GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr) +{ + return IComponentLoader::GetOrCtreateInstance()->GetSink(dhType, sinkPtr); +} + +int32_t ComponentLoader::GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr) +{ + return IComponentLoader::GetOrCtreateInstance()->GetHardwareHandler(dhType, hardwareHandlerPtr); +} + +int32_t ComponentLoader::ReleaseSink(const DHType dhType) +{ + return IComponentLoader::GetOrCtreateInstance()->ReleaseSink(dhType); +} + +std::vector ComponentLoader::GetAllCompTypes() +{ + return IComponentLoader::GetOrCtreateInstance()->GetAllCompTypes(); +} + +int32_t ComponentLoader::GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr) +{ + return IComponentLoader::GetOrCtreateInstance()->GetSource(dhType, sourcePtr); +} + +int32_t ComponentLoader::GetSourceSaId(const DHType dhType) +{ + return IComponentLoader::GetOrCtreateInstance()->GetSourceSaId(dhType); +} + +int32_t ComponentLoader::ReleaseSource(const DHType dhType) +{ + return IComponentLoader::GetOrCtreateInstance()->ReleaseSource(dhType); +} + +int32_t ComponentLoader::GetLocalDHVersion(DHVersion &dhVersion) +{ + return IComponentLoader::GetOrCtreateInstance()->GetLocalDHVersion(dhVersion); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_dh_context.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_dh_context.cpp new file mode 100644 index 00000000..ad6e3123 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_dh_context.cpp @@ -0,0 +1,51 @@ +/* + * 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_dh_context.h" + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr IDHContext::dHContextInstance_; + +std::shared_ptr IDHContext::GetOrCtreateInstance() +{ + if (!dHContextInstance_) { + dHContextInstance_ = std::make_shared(); + } + return dHContextInstance_; +} + +void IDHContext::ReleaseInstance() +{ + dHContextInstance_.reset(); + dHContextInstance_ = nullptr; +} + +const DeviceInfo& DHContext::GetDeviceInfo() +{ + return IDHContext::GetOrCtreateInstance()->GetDeviceInfo(); +} + +std::string DHContext::GetUUIDByNetworkId(const std::string &networkId) +{ + return IDHContext::GetOrCtreateInstance()->GetUUIDByNetworkId(networkId); +} + +std::string DHContext::GetUUIDByDeviceId(const std::string &deviceId) +{ + return IDHContext::GetOrCtreateInstance()->GetUUIDByDeviceId(deviceId); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_dh_utils_tool.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_dh_utils_tool.cpp new file mode 100644 index 00000000..db65ddc3 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_dh_utils_tool.cpp @@ -0,0 +1,41 @@ +/* + * 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_dh_utils_tool.h" + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr IDHUtilTool::UtilToolInstance_; + +std::shared_ptr IDHUtilTool::GetOrCtreateInstance() +{ + if (!UtilToolInstance_) { + UtilToolInstance_ = std::make_shared(); + } + return UtilToolInstance_; +} + +void IDHUtilTool::ReleaseInstance() +{ + UtilToolInstance_.reset(); + UtilToolInstance_ = nullptr; +} + +DeviceInfo GetLocalDeviceInfo() +{ + return IDHUtilTool::GetOrCtreateInstance()->GetLocalDeviceInfo(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_meta_info_manager.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_meta_info_manager.cpp new file mode 100644 index 00000000..a906eb28 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_meta_info_manager.cpp @@ -0,0 +1,41 @@ +/* + * 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_meta_info_manager.h" + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr IMetaInfoManager::metaInfoManagerInstance_; + +std::shared_ptr IMetaInfoManager::GetOrCtreateInstance() +{ + if (!metaInfoManagerInstance_) { + metaInfoManagerInstance_ = std::make_shared(); + } + return metaInfoManagerInstance_; +} + +void IMetaInfoManager::ReleaseInstance() +{ + metaInfoManagerInstance_.reset(); + metaInfoManagerInstance_ = nullptr; +} + +int32_t MetaInfoManager::GetMetaDataByDHType(const DHType dhType, MetaCapInfoMap &metaInfoMap) +{ + return IMetaInfoManager::GetOrCtreateInstance()->GetMetaDataByDHType(dhType, metaInfoMap); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_version_manager.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_version_manager.cpp new file mode 100644 index 00000000..37e17bd2 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_version_manager.cpp @@ -0,0 +1,41 @@ +/* + * 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_version_manager.h" + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr IVersionManager::versionManagerInstance_; + +std::shared_ptr IVersionManager::GetOrCtreateInstance() +{ + if (!versionManagerInstance_) { + versionManagerInstance_ = std::make_shared(); + } + return versionManagerInstance_; +} + +void IVersionManager::ReleaseInstance() +{ + versionManagerInstance_.reset(); + versionManagerInstance_ = nullptr; +} + +int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion) +{ + return IVersionManager::GetOrCtreateInstance()->GetCompVersion(uuid, dhType, compVersion); +} +} // namespace DistributedHardware +} // namespace OHOS -- Gitee