diff --git a/av_transport/av_trans_engine/plugin/test/av_trans_input/daudio_input_test/daudio_input_test.cpp b/av_transport/av_trans_engine/plugin/test/av_trans_input/daudio_input_test/daudio_input_test.cpp index 23abf14a4fc6fc09b0ff6c79a45c27cedf395241..d3d8bf9986ed5eb35be92e67b9310e7786e1fb49 100644 --- a/av_transport/av_trans_engine/plugin/test/av_trans_input/daudio_input_test/daudio_input_test.cpp +++ b/av_transport/av_trans_engine/plugin/test/av_trans_input/daudio_input_test/daudio_input_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -32,8 +32,24 @@ namespace { const std::string KEY_TYPE = "type"; const std::string KEY_CONTENT = "content"; const std::string PLUGINNAME = "daudio_input"; + static const uint32_t BUFFER_MAX_CAPACITY = 104857600; + static const uint32_t NUM_16 = 16; } +class ChannelListenerTest : public ISoftbusChannelListener { + public: + void OnChannelEvent(const AVTransEvent &event) override + { + event_ = event; + } + void OnStreamReceived(const StreamData *data, const StreamData *ext) override + { + event_.type = EventType::EVENT_DATA_RECEIVED; + } + public: + AVTransEvent event_; +}; + void DaudioInputTest::SetUpTestCase() {} void DaudioInputTest::TearDownTestCase() {} @@ -409,6 +425,35 @@ HWTEST_F(DaudioInputTest, GetBufferData_001, TestSize.Level0) EXPECT_EQ(nullptr, ret); } +HWTEST_F(DaudioInputTest, CreateBufferData_001, TestSize.Level0) +{ + std::shared_ptr transBuffer = std::make_shared(); + auto ret = transBuffer->CreateBufferData(BUFFER_MAX_CAPACITY + 1); + EXPECT_EQ(nullptr, ret); + + ret = transBuffer->WrapBufferData(nullptr, BUFFER_MAX_CAPACITY + 1, 0); + EXPECT_EQ(nullptr, ret); + + ret = transBuffer->CreateBufferData(CAPACITY_MAX_LENGTH); + EXPECT_NE(nullptr, ret); + + ret = transBuffer->GetBufferData(0); + EXPECT_NE(nullptr, ret); + uint8_t data[] = { 0, 0, 0, 0}; + ret->Write(data, sizeof(data), 0); + uint8_t readData[NUM_16]; + int32_t readed = ret->Read(readData, 1); + EXPECT_EQ(readed, 1); + readed = ret->Read(readData, NUM_16); + EXPECT_EQ(readed, sizeof(data)); + + transBuffer->Reset(); + ret = transBuffer->GetBufferData(0); + EXPECT_NE(nullptr, ret); + EXPECT_EQ(transBuffer->GetDataCount(), 1); + EXPECT_EQ(ret->GetSize(), 0); +} + HWTEST_F(DaudioInputTest, MarshalMessage_001, TestSize.Level0) { auto avMessage = std::make_shared((uint32_t)AVTransTag::START_AV_SYNC, @@ -573,5 +618,64 @@ HWTEST_F(DaudioInputTest, GetPeerDevIdBySessId_001, TestSize.Level1) ret = SoftbusChannelAdapter::GetInstance().GetPeerDevIdBySessId(sessionId); EXPECT_EQ(EMPTY_STRING, ret); } + +HWTEST_F(DaudioInputTest, OnSoftbusTimeSyncResult_001, TestSize.Level1) +{ + ChannelListenerTest testListener; + SoftbusChannelAdapter::GetInstance().listenerMap_["sync1"] = &testListener; + TimeSyncResultInfo info; + SoftbusChannelAdapter::GetInstance().timeSyncSessNames_.emplace("sync1"); + SoftbusChannelAdapter::GetInstance().OnSoftbusTimeSyncResult(&info, 1); + SoftbusChannelAdapter::GetInstance().timeSyncSessNames_.clear(); + SoftbusChannelAdapter::GetInstance().listenerMap_.clear(); + EXPECT_EQ(testListener.event_.type, EventType::EVENT_TIME_SYNC_RESULT); +} + +HWTEST_F(DaudioInputTest, OnSoftbusStreamReceived_001, TestSize.Level1) +{ + ChannelListenerTest testListener; + SoftbusChannelAdapter::GetInstance().listenerMap_["sync1"] = &testListener; + StreamData data1 = {nullptr, 0}; + StreamData data2 = {nullptr, 0}; + int32_t sessionId = 1; + SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.clear(); + SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert(std::make_pair("sync1", sessionId)); + SoftbusChannelAdapter::GetInstance().OnSoftbusStreamReceived(sessionId, &data1, &data2, nullptr); + SoftbusChannelAdapter::GetInstance().listenerMap_.clear(); + SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.clear(); + EXPECT_EQ(testListener.event_.type, EventType::EVENT_DATA_RECEIVED); +} + +HWTEST_F(DaudioInputTest, OnSoftbusChannelOpened_001, TestSize.Level1) +{ + std::string peerSessionName = OWNER_NAME_D_SCREEN + "_" + SENDER_CONTROL_SESSION_NAME_SUFFIX; + std::string sessionName = OWNER_NAME_D_SCREEN + "_" + RECEIVER_CONTROL_SESSION_NAME_SUFFIX; + int32_t sessionId = 1; + SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert( + std::make_pair(sessionName + std::string("devid"), sessionId)); + int32_t ret = SoftbusChannelAdapter::GetInstance().OnSoftbusChannelOpened(peerSessionName, sessionId, "devid", 0); + EXPECT_EQ(ret, DH_AVT_SUCCESS); + SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.clear(); +} + +HWTEST_F(DaudioInputTest, SendStreamData_001, TestSize.Level1) +{ + std::string sessionName = OWNER_NAME_D_SCREEN + "_" + RECEIVER_CONTROL_SESSION_NAME_SUFFIX; + char bytes[] = {0, 0, 0, 0}; + StreamData data = {bytes, sizeof(bytes)}; + StreamData extdata = {bytes, sizeof(bytes)}; + int32_t ret = SoftbusChannelAdapter::GetInstance().SendStreamData(sessionName, "devid", &data, &extdata); + EXPECT_EQ(ret, ERR_DH_AVT_SEND_DATA_FAILED); +} + +HWTEST_F(DaudioInputTest, FindSessionName_001, TestSize.Level1) +{ + std::string peerSessionName = OWNER_NAME_D_SCREEN + "_" + SENDER_CONTROL_SESSION_NAME_SUFFIX; + std::string ret = SoftbusChannelAdapter::GetInstance().FindSessNameByPeerSessName(peerSessionName); + EXPECT_NE(ret.size(), 0); + ret = SoftbusChannelAdapter::GetInstance().FindSessNameByPeerSessName("name"); + EXPECT_EQ(ret.size(), 0); +} + } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/include/distributed_hardware_proxy_test.h b/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/include/distributed_hardware_proxy_test.h index ab51ec58305fe66ec04a81b908ff39ef350b7809..3d8a96fc8964aee1f583feddf946540b844c20cf 100644 --- a/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/include/distributed_hardware_proxy_test.h +++ b/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/include/distributed_hardware_proxy_test.h @@ -64,6 +64,12 @@ public: int32_t EnableSource(const std::string &networkId, const std::vector &descriptors); int32_t DisableSource(const std::string &networkId, const std::vector &descriptors); }; + + class TestDistributedHardwareStub2 : public TestDistributedHardwareStub { + public: + int32_t OnRemoteRequest(uint32_t code, + MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + }; }; class MockIPublisherListener : public IPublisherListener { diff --git a/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/src/distributed_hardware_proxy_test.cpp b/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/src/distributed_hardware_proxy_test.cpp index 283120adf30eeb602700fc1f384ddaab5d7c8693..0e79016bbe37ce80cf042c878713e2380189bd4a 100644 --- a/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/src/distributed_hardware_proxy_test.cpp +++ b/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/src/distributed_hardware_proxy_test.cpp @@ -14,6 +14,7 @@ */ #include "distributed_hardware_proxy_test.h" +#include "dhardware_ipc_interface_code.h" using namespace testing::ext; @@ -31,6 +32,19 @@ void DistributedHardwareProxyTest::SetUp() {} void DistributedHardwareProxyTest::TearDown() {} +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub2::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + if (code == static_cast(DHMsgInterfaceCode::GET_DISTRIBUTED_HARDWARE)) { + std::vector descriptors; + descriptors.emplace_back(DHDescriptor()); + descriptors.emplace_back(DHDescriptor()); + OHOS::DistributedHardware::DistributedHardwareStub::WriteDescriptors(reply, descriptors); + return DH_FWK_SUCCESS; + } + return OHOS::DistributedHardware::DistributedHardwareStub::OnRemoteRequest(code, data, reply, option); +} + int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::RegisterPublisherListener(const DHTopic topic, const sptr listener) { @@ -529,6 +543,19 @@ HWTEST_F(DistributedHardwareProxyTest, GetDistributedHardware_001, TestSize.Leve std::vector descriptors; auto ret = dhProxy.GetDistributedHardware(networkId, descriptors); EXPECT_EQ(ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL, ret); + ret = dhProxy.GetDistributedHardware(std::string(), descriptors); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(DistributedHardwareProxyTest, GetDistributedHardware_002, TestSize.Level1) +{ + std::string networkId = "123456"; + sptr dhStubPtr(new TestDistributedHardwareStub2()); + ASSERT_TRUE(dhStubPtr != nullptr); + DistributedHardwareProxy dhProxy(dhStubPtr); + std::vector descriptors; + auto ret = dhProxy.GetDistributedHardware(networkId, descriptors); + EXPECT_EQ(DH_FWK_SUCCESS, ret); } HWTEST_F(DistributedHardwareProxyTest, RegisterDHStatusListener_Source_001, TestSize.Level1) @@ -540,6 +567,8 @@ HWTEST_F(DistributedHardwareProxyTest, RegisterDHStatusListener_Source_001, Test sptr listener(new MockHDSourceStatusListenerStub()); auto ret = dhProxy.RegisterDHStatusListener(networkId, listener); EXPECT_EQ(ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL, ret); + ret = dhProxy.RegisterDHStatusListener(std::string(), listener); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } HWTEST_F(DistributedHardwareProxyTest, UnregisterDHStatusListener_Source_001, TestSize.Level1) @@ -551,6 +580,8 @@ HWTEST_F(DistributedHardwareProxyTest, UnregisterDHStatusListener_Source_001, Te sptr listener(new MockHDSourceStatusListenerStub()); auto ret = dhProxy.UnregisterDHStatusListener(networkId, listener); EXPECT_EQ(ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL, ret); + ret = dhProxy.UnregisterDHStatusListener(std::string(), listener); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } HWTEST_F(DistributedHardwareProxyTest, RegisterDHStatusListener_Sink_001, TestSize.Level1) diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn index f75a462e1580f7ae84595d7f84e8d509672416ad..e310400320bfbb8fdd9304d014c6d586394458ee 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn @@ -29,6 +29,7 @@ config("module_private_config") { "${common_path}/log/include", "${common_path}/utils/include", "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/componentloader", "${services_path}/distributedhardwarefwkservice/include/task", "${services_path}/distributedhardwarefwkservice/include/utils", ] diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp index aef9c77ec9d698f78b55a7cef9836d38603f153b..07b27d021b4e202b74879b55b960b11e2066001b 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp @@ -21,6 +21,7 @@ #include "constants.h" #include "cJSON.h" +#include "component_loader.h" #include "dh_context.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -43,6 +44,10 @@ namespace { const std::u16string ARGS_E = u"-e"; const std::u16string ARGS_T = u"-t"; const std::u16string ARGS_C = u"-c"; + + constexpr int32_t TEST_COMP_SINK_SA_ID = 4804; + constexpr int32_t TEST_SINK_SA_ID = 12345; + constexpr int32_t TEST_SOURCE_SA_ID = 12345; } void DistributedHardwareServiceTest::SetUpTestCase(void) {} @@ -52,6 +57,42 @@ void DistributedHardwareServiceTest::SetUp() {} void DistributedHardwareServiceTest::TearDown() {} +void SetUpComponentLoaderConfig() +{ + if (ComponentLoader::GetInstance().compHandlerMap_.find(DHType::AUDIO) + == ComponentLoader::GetInstance().compHandlerMap_.end()) { + CompHandler handler; + handler.compConfig.name = "distributed_audio"; + handler.compConfig.type = DHType::AUDIO; + handler.compConfig.compHandlerLoc = "libdistributed_camera_handler.z.so"; + handler.compConfig.compHandlerVersion = "1.0"; + handler.compConfig.compSourceLoc = "libdistributed_camera_source_sdk.z.so"; + handler.compConfig.compSourceVersion = "1.0"; + handler.compConfig.compSinkLoc = "libdistributed_camera_sink_sdk.z.so"; + handler.compConfig.compSinkVersion = "2.0"; + handler.compConfig.compSinkSaId = TEST_COMP_SINK_SA_ID; + handler.compConfig.haveFeature = false; + handler.hardwareHandler = nullptr; + handler.sourceHandler = nullptr; + handler.sinkHandler = nullptr; + handler.type = DHType::AUDIO; + handler.sinkSaId = TEST_SINK_SA_ID; + handler.sourceSaId = TEST_SOURCE_SA_ID; + ComponentLoader::GetInstance().compHandlerMap_[DHType::AUDIO] = handler; + } +} + +void SetDownComponentLoaderConfig() +{ + auto itHandler = ComponentLoader::GetInstance().compHandlerMap_.find(DHType::AUDIO); + if (itHandler != ComponentLoader::GetInstance().compHandlerMap_.end()) { + CompHandler &handler = itHandler->second; + if (handler.sinkSaId == TEST_SINK_SA_ID && handler.sourceSaId == TEST_SOURCE_SA_ID) { + ComponentLoader::GetInstance().compHandlerMap_.erase(itHandler); + } + } +} + /** * @tc.name: register_publisher_listener_001 * @tc.desc: Verify the RegisterPublisherListener function @@ -262,6 +303,10 @@ HWTEST_F(DistributedHardwareServiceTest, GetDistributedHardware_001, TestSize.Le networkId = "local"; ret = service.GetDistributedHardware(networkId, descriptors); EXPECT_EQ(ret, DH_FWK_SUCCESS); + + networkId = ""; + ret = service.GetDistributedHardware(networkId, descriptors); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); } /** @@ -275,8 +320,12 @@ HWTEST_F(DistributedHardwareServiceTest, RegisterDHStatusListener_001, TestSize. DistributedHardwareService service(ASID, true); sptr listener = nullptr; + SetUpComponentLoaderConfig(); auto ret = service.RegisterDHStatusListener(listener); EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.RegisterDHStatusListener(listener); + SetDownComponentLoaderConfig(); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_REPEAT_CALL); } /** @@ -290,9 +339,17 @@ HWTEST_F(DistributedHardwareServiceTest, RegisterDHStatusListener_002, TestSize. DistributedHardwareService service(ASID, true); sptr listener = nullptr; + SetUpComponentLoaderConfig(); std::string networkId = "111"; auto ret = service.RegisterDHStatusListener(networkId, listener); EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.RegisterDHStatusListener(networkId, listener); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_REPEAT_CALL); + SetDownComponentLoaderConfig(); + + networkId = ""; + ret = service.RegisterDHStatusListener(networkId, listener); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); } /** @@ -306,8 +363,12 @@ HWTEST_F(DistributedHardwareServiceTest, UnregisterDHStatusListener_001, TestSiz DistributedHardwareService service(ASID, true); sptr listener = nullptr; + SetUpComponentLoaderConfig(); auto ret = service.UnregisterDHStatusListener(listener); EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.UnregisterDHStatusListener(listener); + SetDownComponentLoaderConfig(); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_REPEAT_CALL); } /** @@ -321,9 +382,17 @@ HWTEST_F(DistributedHardwareServiceTest, UnregisterDHStatusListener_002, TestSiz DistributedHardwareService service(ASID, true); sptr listener = nullptr; + SetUpComponentLoaderConfig(); std::string networkId = "111"; auto ret = service.UnregisterDHStatusListener(networkId, listener); EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.UnregisterDHStatusListener(networkId, listener); + EXPECT_EQ(ret, ERR_DH_FWK_COMPONENT_REPEAT_CALL); + SetDownComponentLoaderConfig(); + + networkId = ""; + ret = service.UnregisterDHStatusListener(networkId, listener); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); } /** @@ -370,6 +439,10 @@ HWTEST_F(DistributedHardwareServiceTest, EnableSource_001, TestSize.Level1) auto ret = service.EnableSource(networkId, descriptors); EXPECT_EQ(ret, DH_FWK_SUCCESS); + + networkId = ""; + ret = service.EnableSource(networkId, descriptors); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); } /** @@ -386,6 +459,10 @@ HWTEST_F(DistributedHardwareServiceTest, DisableSource_001, TestSize.Level1) auto ret = service.DisableSource(networkId, descriptors); EXPECT_EQ(ret, DH_FWK_SUCCESS); + + networkId = ""; + ret = service.DisableSource(networkId, descriptors); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); } HWTEST_F(DistributedHardwareServiceTest, ResumeDistributedHardware_002, TestSize.Level1) diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn index 4a30a9122226f09f16aa490e9e38d39cf805ad5a..a51bbf7ad7a2858dab2d45c222da094794ee47df 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn @@ -20,17 +20,114 @@ module_out_path = unittest_output_path config("module_private_config") { visibility = [ ":*" ] include_dirs = [ + "${av_trans_path}/common/include", "include", + "${innerkits_path}/include", "${utils_path}/include", "${utils_path}/include/log", "${services_path}/distributedhardwarefwkservice/include", - "${services_path}/distributedhardwarefwkservice/include/task", + "${services_path}/distributedhardwarefwkservice/include/componentloader", + "${services_path}/distributedhardwarefwkservice/include/componentmanager", + "${services_path}/distributedhardwarefwkservice/include/lowlatency", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", "${services_path}/distributedhardwarefwkservice/include/utils", + "${services_path}/distributedhardwarefwkservice/include/task", + "${services_path}/distributedhardwarefwkservice/include/transport", "${common_path}/utils/include", "${common_path}/log/include", ] } +## UnitTest dh_disable_task_test +ohos_unittest("DHDisableTaskTest") { + module_out_path = module_out_path + + sources = [ + "src/disable_task_test.cpp", + "src/mock_component_manager.cpp", + "src/mock_dh_context.cpp", + ] + + configs = [ ":module_private_config" ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + external_deps = [ + "googletest:gmock", + "googletest:gmock_main", + "cJSON:cjson", + "c_utils:utils", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "kv_store:distributeddata_inner", + "samgr:samgr_proxy", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"TaskTest\"", + "LOG_DOMAIN=0xD004100", + ] +} + +## UnitTest dh_enable_task_test +ohos_unittest("DHEnableTaskTest") { + module_out_path = module_out_path + + sources = [ + "src/enable_task_test.cpp", + "src/mock_component_manager.cpp", + "src/mock_dh_context.cpp", + ] + + configs = [ ":module_private_config" ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + external_deps = [ + "googletest:gmock", + "googletest:gmock_main", + "cJSON:cjson", + "c_utils:utils", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "kv_store:distributeddata_inner", + "samgr:samgr_proxy", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"TaskTest\"", + "LOG_DOMAIN=0xD004100", + ] +} + ## UnitTest dh_task_test ohos_unittest("DHTaskTest") { module_out_path = module_out_path @@ -75,5 +172,9 @@ ohos_unittest("DHTaskTest") { group("dh_task_test") { testonly = true - deps = [ ":DHTaskTest" ] + deps = [ + ":DHDisableTaskTest" , + ":DHEnableTaskTest" , + ":DHTaskTest" , + ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/include/disable_task_test.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/disable_task_test.h new file mode 100644 index 0000000000000000000000000000000000000000..8885c9dbc02021b6645a008c99ba9761251e54b9 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/include/disable_task_test.h @@ -0,0 +1,40 @@ +/* + * 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_DISABLE_TASK_TEST_H +#define OHOS_DISTRIBUTED_DISABLE_TASK_TEST_H + +#include +#include "task.h" +#include "disable_task.h" + +#include "mock_component_manager.h" +#include "mock_dh_context.h" + +namespace OHOS { +namespace DistributedHardware { +class DisableTaskTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + std::shared_ptr componentManager_; + std::shared_ptr dhContext_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/include/enable_task_test.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/enable_task_test.h new file mode 100644 index 0000000000000000000000000000000000000000..bd51e76d8614f59b66b3384c15776e2b86448540 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/include/enable_task_test.h @@ -0,0 +1,37 @@ +/* + * 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_ENABLE_TASK_TEST_H +#define OHOS_DISTRIBUTED_ENABLE_TASK_TEST_H + +#include +#include "task.h" +#include "enable_task.h" + +#include "mock_component_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class EnableTaskTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + std::shared_ptr componentManager_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_component_manager.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_component_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..dc95fa36d20049a72178dc2bbbb1e5fc3a9784df --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_component_manager.h @@ -0,0 +1,58 @@ +/* + * 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_MANAGER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_COMPONENT_MANAGER_H + +#include +#include + +#include "component_manager.h" +#include "component_loader.h" +namespace OHOS { +namespace DistributedHardware { +class IComponentManager { +public: + virtual int32_t CheckDemandStart(const std::string &uuid, const DHType dhType, bool &enableSource) = 0; + virtual int32_t ForceDisableSink(const DHDescriptor &dhDescriptor) = 0; + virtual int32_t ForceDisableSource(const std::string &networkId, const DHDescriptor &dhDescriptor) = 0; + virtual int32_t EnableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) = 0; + virtual int32_t EnableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) = 0; + virtual int32_t DisableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) = 0; + virtual int32_t DisableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) = 0; + + static std::shared_ptr GetOrCtreateInstance(); + static void ReleaseInstance(); +public: + static std::shared_ptr componentManager_; +}; + +class MockComponentManager : public IComponentManager { +public: + virtual ~MockComponentManager() = default; + MOCK_METHOD(int32_t, CheckDemandStart, (const std::string&, const DHType, (bool&))); + MOCK_METHOD(int32_t, ForceDisableSink, (const DHDescriptor&)); + MOCK_METHOD(int32_t, ForceDisableSource, (const std::string&, (const DHDescriptor&))); + MOCK_METHOD(int32_t, EnableSink, (const DHDescriptor&, int32_t, int32_t)); + MOCK_METHOD(int32_t, EnableSource, (const std::string&, const DHDescriptor&, int32_t, int32_t)); + MOCK_METHOD(int32_t, DisableSink, (const DHDescriptor&, int32_t, int32_t)); + MOCK_METHOD(int32_t, DisableSource, (const std::string&, const DHDescriptor&, int32_t, int32_t)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_COMPONENT_MANAGER_H + \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_dh_context.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_dh_context.h new file mode 100644 index 0000000000000000000000000000000000000000..066668d0ca6359d8fc71efdaa5b79bad670d6f7f --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_dh_context.h @@ -0,0 +1,45 @@ +/* + * 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 + +#include "dh_context.h" + +namespace OHOS { +namespace DistributedHardware { +class IDHContext { +public: + virtual size_t GetRealTimeOnlineDeviceCount() = 0; + virtual uint32_t GetIsomerismConnectCount() = 0; + static std::shared_ptr GetOrCreateInstance(); + static void ReleaseInstance(); +public: + static std::shared_ptr dhContext_; +}; + +class MockDHContext : public IDHContext { +public: + virtual ~MockDHContext() = default; + MOCK_METHOD(size_t, GetRealTimeOnlineDeviceCount, ()); + MOCK_METHOD(uint32_t, GetIsomerismConnectCount, ()); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_MOCK_COMPONENT_MANAGER_H + \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/src/disable_task_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/disable_task_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e30abb88c11470941c7170c6fdaa18e8237995d --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/src/disable_task_test.cpp @@ -0,0 +1,208 @@ +/* + * 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 "disable_task_test.h" + +#include "ffrt.h" + +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +namespace { + +const std::string DEV_ID_1 = "bb536a637105409e904d4da83790a4a8"; +const std::string DEV_NETWORK_ID_1 = "nt36a637105409e904d4da83790a4a8"; +const std::string DEV_DID_1 = "2144a637105409e904d4da83790a4a8"; + +const TaskParam TASK_PARAM_1 = { + .networkId = DEV_NETWORK_ID_1, + .uuid = DEV_ID_1, + .udid = DEV_DID_1, + .dhId = "", + .dhType = DHType::UNKNOWN +}; + +} + +void DisableTaskTest::SetUpTestCase() +{} + +void DisableTaskTest::TearDownTestCase() +{} + +void DisableTaskTest::SetUp() +{ + auto componentManager = IComponentManager::GetOrCtreateInstance(); + componentManager_ = std::static_pointer_cast(componentManager); + auto dhContext = IDHContext::GetOrCreateInstance(); + dhContext_ = std::static_pointer_cast(dhContext); +} + +void DisableTaskTest::TearDown() +{ + IComponentManager::ReleaseInstance(); + componentManager_ = nullptr; + IDHContext::ReleaseInstance(); + dhContext_ = nullptr; +} + +/** + * @tc.name: UnRegisterHardware_001 + * @tc.desc: Verify the UnRegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DisableTaskTest, UnRegisterHardware_001, TestSize.Level0) +{ + auto disableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*componentManager_, ForceDisableSource(_, _)).Times(1).WillRepeatedly(Return(0)); + ASSERT_EQ(disableTask->UnRegisterHardware(), DH_FWK_SUCCESS); +} + +/** + * @tc.name: UnRegisterHardware_002 + * @tc.desc: Verify the UnRegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DisableTaskTest, UnRegisterHardware_002, TestSize.Level0) +{ + auto disableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*componentManager_, ForceDisableSource(_, _)).Times(1).WillRepeatedly(Return(1)); + ASSERT_EQ(disableTask->UnRegisterHardware(), 1); +} + +/** + * @tc.name: UnRegisterHardware_003 + * @tc.desc: Verify the UnRegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DisableTaskTest, UnRegisterHardware_003, TestSize.Level0) +{ + auto disableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, GetLocalUdid(), + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*dhContext_, GetRealTimeOnlineDeviceCount()).Times(1).WillRepeatedly(Return(0)); + EXPECT_CALL(*dhContext_, GetIsomerismConnectCount()).Times(1).WillRepeatedly(Return(0)); + EXPECT_CALL(*componentManager_, ForceDisableSink(_)).Times(1).WillRepeatedly(Return(0)); + ASSERT_EQ(disableTask->UnRegisterHardware(), DH_FWK_SUCCESS); +} + +/** + * @tc.name: UnRegisterHardware_004 + * @tc.desc: Verify the UnRegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DisableTaskTest, UnRegisterHardware_004, TestSize.Level0) +{ + auto disableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, GetLocalUdid(), + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*dhContext_, GetRealTimeOnlineDeviceCount()).Times(1).WillRepeatedly(Return(0)); + EXPECT_CALL(*dhContext_, GetIsomerismConnectCount()).Times(1).WillRepeatedly(Return(0)); + EXPECT_CALL(*componentManager_, ForceDisableSink(_)).Times(1).WillRepeatedly(Return(1)); + ASSERT_EQ(disableTask->UnRegisterHardware(), 1); +} + +/** + * @tc.name: UnRegisterHardware_005 + * @tc.desc: Verify the UnRegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DisableTaskTest, UnRegisterHardware_005, TestSize.Level0) +{ + auto disableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, GetLocalUdid(), + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*dhContext_, GetRealTimeOnlineDeviceCount()).Times(1).WillRepeatedly(Return(1)); + ASSERT_EQ(disableTask->UnRegisterHardware(), DH_FWK_SUCCESS); +} + +/** + * @tc.name: UnRegisterHardware_006 + * @tc.desc: Verify the UnRegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DisableTaskTest, UnRegisterHardware_006, TestSize.Level0) +{ + auto disableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, GetLocalUdid(), + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*dhContext_, GetRealTimeOnlineDeviceCount()).Times(1).WillRepeatedly(Return(0)); + EXPECT_CALL(*dhContext_, GetIsomerismConnectCount()).Times(1).WillRepeatedly(Return(1)); + ASSERT_EQ(disableTask->UnRegisterHardware(), DH_FWK_SUCCESS); +} + +/** + * @tc.name: UnRegisterHardware_007 + * @tc.desc: Verify the UnRegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DisableTaskTest, UnRegisterHardware_007, TestSize.Level0) +{ + auto disableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + disableTask->callingUid_ = 1; + disableTask->callingPid_ = 1; + disableTask->effectSink_ = true; + disableTask->effectSource_ = true; + EXPECT_CALL(*componentManager_, DisableSink(_, _, _)).Times(1).WillRepeatedly(Return(1)); + EXPECT_CALL(*componentManager_, DisableSource(_, _, _, _)).Times(1).WillRepeatedly(Return(1)); + ASSERT_EQ(disableTask->UnRegisterHardware(), 1); +} + +/** + * @tc.name: UnRegisterHardware_008 + * @tc.desc: Verify the UnRegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DisableTaskTest, UnRegisterHardware_008, TestSize.Level0) +{ + auto disableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + disableTask->callingUid_ = 1; + disableTask->callingPid_ = 1; + disableTask->effectSink_ = true; + disableTask->effectSource_ = true; + EXPECT_CALL(*componentManager_, DisableSink(_, _, _)).Times(1).WillRepeatedly(Return(0)); + EXPECT_CALL(*componentManager_, DisableSource(_, _, _, _)).Times(1).WillRepeatedly(Return(0)); + ASSERT_EQ(disableTask->UnRegisterHardware(), DH_FWK_SUCCESS); +} + +/** + * @tc.name: UnRegisterHardware_009 + * @tc.desc: Verify the UnRegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DisableTaskTest, UnRegisterHardware_009, TestSize.Level0) +{ + auto disableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + disableTask->callingUid_ = 1; + disableTask->callingPid_ = 1; + ASSERT_EQ(disableTask->UnRegisterHardware(), DH_FWK_SUCCESS); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/src/enable_task_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/enable_task_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b36e6dbe4b04d0ef39b570cd4ed2ceaed932ff8 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/src/enable_task_test.cpp @@ -0,0 +1,204 @@ +/* + * 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 "enable_task_test.h" + +#include "ffrt.h" + +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +namespace { + +const std::string DEV_ID_1 = "bb536a637105409e904d4da83790a4a8"; +const std::string DEV_NETWORK_ID_1 = "nt36a637105409e904d4da83790a4a8"; +const std::string DEV_DID_1 = "2144a637105409e904d4da83790a4a8"; + +const TaskParam TASK_PARAM_1 = { + .networkId = DEV_NETWORK_ID_1, + .uuid = DEV_ID_1, + .udid = DEV_DID_1, + .dhId = "", + .dhType = DHType::UNKNOWN +}; + +} + +void EnableTaskTest::SetUpTestCase() +{} + +void EnableTaskTest::TearDownTestCase() +{} + +void EnableTaskTest::SetUp() +{ + auto componentManager = IComponentManager::GetOrCtreateInstance(); + componentManager_ = std::static_pointer_cast(componentManager); +} + +void EnableTaskTest::TearDown() +{ + IComponentManager::ReleaseInstance(); + componentManager_ = nullptr; +} + +/** + * @tc.name: RegisterHardware_001 + * @tc.desc: Verify the RegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(EnableTaskTest, RegisterHardware_001, TestSize.Level0) +{ + auto enableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*componentManager_, CheckDemandStart(_, _, _)).Times(1).WillRepeatedly( + DoAll(SetArgReferee<2>(false), Return(0))); + ASSERT_EQ(enableTask->RegisterHardware(), ERR_DH_FWK_COMPONENT_LIMIT_DEMAND_START); +} + +/** + * @tc.name: RegisterHardware_002 + * @tc.desc: Verify the RegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(EnableTaskTest, RegisterHardware_002, TestSize.Level0) +{ + auto enableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*componentManager_, CheckDemandStart(_, _, _)).Times(1).WillRepeatedly( + DoAll(SetArgReferee<2>(true), Return(0))); + EXPECT_CALL(*componentManager_, EnableSource(_, _, _, _)).Times(1).WillRepeatedly(Return(0)); + ASSERT_EQ(enableTask->RegisterHardware(), DH_FWK_SUCCESS); +} + +/** + * @tc.name: RegisterHardware_003 + * @tc.desc: Verify the RegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(EnableTaskTest, RegisterHardware_003, TestSize.Level0) +{ + auto enableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*componentManager_, CheckDemandStart(_, _, _)).Times(1).WillRepeatedly( + DoAll(SetArgReferee<2>(true), Return(0))); + EXPECT_CALL(*componentManager_, EnableSource(_, _, _, _)).Times(1).WillRepeatedly(Return(1)); + ASSERT_EQ(enableTask->RegisterHardware(), 1); +} + +/** + * @tc.name: RegisterHardware_004 + * @tc.desc: Verify the RegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(EnableTaskTest, RegisterHardware_004, TestSize.Level0) +{ + auto enableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*componentManager_, CheckDemandStart(_, _, _)).Times(1).WillRepeatedly(Return(1)); + ASSERT_EQ(enableTask->RegisterHardware(), 1); +} + +/** + * @tc.name: RegisterHardware_005 + * @tc.desc: Verify the RegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(EnableTaskTest, RegisterHardware_005, TestSize.Level0) +{ + auto enableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, GetLocalUdid(), + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*componentManager_, EnableSink(_, _, _)).Times(1).WillRepeatedly(Return(0)); + ASSERT_EQ(enableTask->RegisterHardware(), 0); +} + +/** + * @tc.name: RegisterHardware_006 + * @tc.desc: Verify the RegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(EnableTaskTest, RegisterHardware_006, TestSize.Level0) +{ + auto enableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, GetLocalUdid(), + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + EXPECT_CALL(*componentManager_, EnableSink(_, _, _)).Times(1).WillRepeatedly(Return(1)); + ASSERT_EQ(enableTask->RegisterHardware(), 1); +} + +/** + * @tc.name: RegisterHardware_007 + * @tc.desc: Verify the RegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(EnableTaskTest, RegisterHardware_007, TestSize.Level0) +{ + auto enableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + enableTask->callingUid_ = 1; + enableTask->callingPid_ = 1; + enableTask->effectSink_ = true; + enableTask->effectSource_ = true; + EXPECT_CALL(*componentManager_, EnableSink(_, _, _)).Times(1).WillRepeatedly(Return(0)); + EXPECT_CALL(*componentManager_, EnableSource(_, _, _, _)).Times(1).WillRepeatedly(Return(0)); + ASSERT_EQ(enableTask->RegisterHardware(), 0); +} + +/** + * @tc.name: RegisterHardware_008 + * @tc.desc: Verify the RegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(EnableTaskTest, RegisterHardware_008, TestSize.Level0) +{ + auto enableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + enableTask->callingUid_ = 1; + enableTask->callingPid_ = 1; + enableTask->effectSink_ = true; + enableTask->effectSource_ = true; + EXPECT_CALL(*componentManager_, EnableSink(_, _, _)).Times(1).WillRepeatedly(Return(1)); + EXPECT_CALL(*componentManager_, EnableSource(_, _, _, _)).Times(1).WillRepeatedly(Return(1)); + ASSERT_EQ(enableTask->RegisterHardware(), 1); +} + +/** + * @tc.name: RegisterHardware_009 + * @tc.desc: Verify the RegisterHardware function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(EnableTaskTest, RegisterHardware_009, TestSize.Level0) +{ + auto enableTask = std::make_shared(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid, + TASK_PARAM_1.dhId, TASK_PARAM_1.dhType); + enableTask->callingUid_ = 1; + enableTask->callingPid_ = 1; + ASSERT_EQ(enableTask->RegisterHardware(), DH_FWK_SUCCESS); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_component_manager.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_component_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba35cf186b44f5a560486f21e7da2c0af7b10f37 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_component_manager.cpp @@ -0,0 +1,78 @@ +/* + * 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_manager.h" + +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr IComponentManager::componentManager_; + +std::shared_ptr IComponentManager::GetOrCtreateInstance() +{ + if (!componentManager_) { + componentManager_ = std::make_shared(); + } + + return componentManager_; +} + +void IComponentManager::ReleaseInstance() +{ + componentManager_.reset(); + componentManager_ = nullptr; +} + +int32_t ComponentManager::CheckDemandStart(const std::string &uuid, const DHType dhType, bool &enableSource) +{ + enableSource = true; + return IComponentManager::GetOrCtreateInstance()->CheckDemandStart(uuid, dhType, enableSource); +} + +int32_t ComponentManager::ForceDisableSink(const DHDescriptor &dhDescriptor) +{ + return IComponentManager::GetOrCtreateInstance()->ForceDisableSink(dhDescriptor); +} + +int32_t ComponentManager::ForceDisableSource(const std::string &networkId, const DHDescriptor &dhDescriptor) +{ + return IComponentManager::GetOrCtreateInstance()->ForceDisableSource(networkId, dhDescriptor); +} + +int32_t ComponentManager::EnableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + return IComponentManager::GetOrCtreateInstance()->EnableSink(dhDescriptor, callingUid, callingPid); +} + +int32_t ComponentManager::EnableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + return IComponentManager::GetOrCtreateInstance()->EnableSource(networkId, dhDescriptor, callingUid, callingPid); +} + +int32_t ComponentManager::DisableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + return IComponentManager::GetOrCtreateInstance()->DisableSink(dhDescriptor, callingUid, callingPid); +} + +int32_t ComponentManager::DisableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + return IComponentManager::GetOrCtreateInstance()->DisableSource(networkId, dhDescriptor, callingUid, callingPid); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_dh_context.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_dh_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a7637cc941e4d07d45521597b3ebb7789998492 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_dh_context.cpp @@ -0,0 +1,50 @@ +/* + * 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" + +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr IDHContext::dhContext_; + +std::shared_ptr IDHContext::GetOrCreateInstance() +{ + if (!dhContext_) { + dhContext_ = std::make_shared(); + } + return dhContext_; +} + +void IDHContext::ReleaseInstance() +{ + dhContext_.reset(); + dhContext_ = nullptr; +} + +size_t DHContext::GetRealTimeOnlineDeviceCount() +{ + return IDHContext::GetOrCreateInstance()->GetRealTimeOnlineDeviceCount(); +} + +uint32_t DHContext::GetIsomerismConnectCount() +{ + return IDHContext::dhContext_->GetIsomerismConnectCount(); +} +} // namespace DistributedHardware +} // namespace OHOS + \ No newline at end of file