diff --git a/bundle.json b/bundle.json index f304daecff4295370995cd97f5230709ded8f052..f940eb45037f581d30de7a2f74bd097eb1fe9d32 100755 --- a/bundle.json +++ b/bundle.json @@ -97,7 +97,8 @@ "//foundation/distributedhardware/distributed_input/utils/test:test", "//foundation/distributedhardware/distributed_input/dfx_utils/test:test", "//foundation/distributedhardware/distributed_input/interfaces/ipc/test:test", - "//foundation/distributedhardware/distributed_input/inputdevicehandler/test:test" + "//foundation/distributedhardware/distributed_input/inputdevicehandler/test:test", + "//foundation/distributedhardware/distributed_input/services/state/test:test" ] } } diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index ef403e11f80231779dbe397e70dcf4feefc45d4a..f6da77957bb95cc402f88be9c5c867ca8b678641 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -143,7 +143,8 @@ size_t InputHub::StartCollectInputEvents(RawEvent *buffer, size_t bufferSize) break; } - if (RefreshEpollItem(false) < 0) { + bool flag = DInputSinkState::GetInstance().IsInThroughtOut(); + if (RefreshEpollItem(!flag) < 0) { break; } } @@ -475,6 +476,7 @@ int32_t InputHub::RefreshEpollItem(bool isSleep) } else { // Some events occurred. pendingEventCount_ = pollResult; + DHLOGD("Number of events is: %d", pollResult); } if (isSleep) { usleep(SLEEP_TIME_US); diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index 2a4e51df0c9a1fddd6727601318def6d49288694..c5126bd4392703c9f6c202e5a56561c03093078b 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -133,16 +133,6 @@ void DistributedInputSinkManager::DInputSinkListener::OnPrepareRemoteInput( nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ONPREPARE; std::string smsg = ""; - int32_t ret = DistributedInputCollector::GetInstance().StartCollectionThread( - DistributedInputSinkTransport::GetInstance().GetEventHandler()); - if (ret != DH_SUCCESS) { - DHLOGE("DInputSinkListener init InputCollector error."); - jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = false; - jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; - smsg = jsonStr.dump(); - DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(sessionId, smsg); - return; - } DistributedInputSinkSwitch::GetInstance().AddSession(sessionId); sinkManagerObj_->QueryLocalWhiteList(jsonStr); @@ -173,16 +163,6 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayPrepareRemoteInput( jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_PREPARE; jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId; std::string smsg = ""; - int ret = DistributedInputCollector::GetInstance().StartCollectionThread( - DistributedInputSinkTransport::GetInstance().GetEventHandler()); - if (ret != DH_SUCCESS) { - DHLOGE("DInputSinkListener init InputCollector error."); - jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = false; - jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; - smsg = jsonStr.dump(); - DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(toSinkSessionId, smsg); - return; - } DistributedInputSinkSwitch::GetInstance().AddSession(toSinkSessionId); sinkManagerObj_->QueryLocalWhiteList(jsonStr); @@ -636,6 +616,13 @@ int32_t DistributedInputSinkManager::Init() dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_PHY_DEV_PLUGIN, pluginStartListener_); DistributedInputCollector::GetInstance().PreInit(); + DHLOGI("init InputCollector."); + int result = DistributedInputCollector::GetInstance().StartCollectionThread( + DistributedInputSinkTransport::GetInstance().GetEventHandler()); + if (result != DH_SUCCESS) { + DHLOGE("init InputCollector error."); + } + return DH_SUCCESS; } diff --git a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp index 43f2c162413c90fc6dd16341325ce064668848ef..9a0b741645668837baf20ebf8eba1f6a371d5882 100644 --- a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp +++ b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -68,6 +68,13 @@ void DistributedInputSourceInjectTest::TestInputNodeListener::OnNodeOffLine(cons (void)sinkNodeId; } +void DistributedInputSourceInjectTest::TestRegisterSessionStateCallbackStub::OnResult(const std::string &devId, + const uint32_t status) +{ + (void)devId; + (void)status; +} + HWTEST_F(DistributedInputSourceInjectTest, RegisterDistributedHardware01, testing::ext::TestSize.Level1) { InputDevice pBuffer; @@ -405,6 +412,53 @@ HWTEST_F(DistributedInputSourceInjectTest, OpenDevicesNode_002, testing::ext::Te ret = DistributedInputInject::GetInstance().inputNodeManager_->OpenDevicesNode(devId, dhIdtest, param); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL, ret); } + +HWTEST_F(DistributedInputSourceInjectTest, RegisterInjectEventCb_001, testing::ext::TestSize.Level1) +{ + sptr callback = nullptr; + auto ret = DistributedInputInject::GetInstance().RegisterInjectEventCb(callback); + EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INJECT_EVENT_CB_IS_NULL, ret); +} + +HWTEST_F(DistributedInputSourceInjectTest, RegisterInjectEventCb_002, testing::ext::TestSize.Level1) +{ + sptr callback(new TestRegisterSessionStateCallbackStub()); + auto ret = DistributedInputInject::GetInstance().RegisterInjectEventCb(callback); + EXPECT_EQ(DH_SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceInjectTest, GetVirtualKeyboardPaths_001, testing::ext::TestSize.Level1) +{ + std::string devId; + std::vector dhIds; + std::vector virKeyboardPaths; + DistributedInputInject::GetInstance().inputNodeManager_ = nullptr; + DistributedInputInject::GetInstance().GetVirtualKeyboardPaths(devId, dhIds, virKeyboardPaths); + + std::string dhId; + DistributedInputInject::GetInstance().NotifyNodeMgrScanVirNode(devId, dhId); + + DistributedInputInject::GetInstance().inputNodeManager_ = std::make_unique(); + DistributedInputInject::GetInstance().NotifyNodeMgrScanVirNode(devId, dhId); + + auto ret = DistributedInputInject::GetInstance().UnregisterInjectEventCb(); + EXPECT_EQ(DH_SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceInjectTest, MatchAndSavePhysicalPath_001, testing::ext::TestSize.Level1) +{ + std::string devicePath = ""; + std::string devId = ""; + std::string dhId = ""; + auto ret = + DistributedInputInject::GetInstance().inputNodeManager_->MatchAndSavePhysicalPath(devicePath, devId, dhId); + EXPECT_EQ(false, ret); + + DistributedInputInject::GetInstance().inputNodeManager_->isInjectThreadCreated_.store(true); + DistributedInputInject::GetInstance().inputNodeManager_->StartInjectThread(); + DistributedInputInject::GetInstance().inputNodeManager_->isInjectThreadCreated_.store(false); + DistributedInputInject::GetInstance().inputNodeManager_->StopInjectThread(); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.h b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.h index bfe0adb560f40e0491f6b8a1d0508f9178e1d105..c97fab483a88e4a8a53392c2544eac1bcdcef7c9 100644 --- a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.h +++ b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -27,6 +27,7 @@ #include "distributed_input_inject.h" #include "distributed_input_node_manager.h" #include "input_node_listener_stub.h" +#include "register_session_state_callback_stub.h" namespace OHOS { namespace DistributedHardware { @@ -45,6 +46,14 @@ public: const std::string sinkNodeDesc); void OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId); }; + + class TestRegisterSessionStateCallbackStub : + public OHOS::DistributedHardware::DistributedInput::RegisterSessionStateCallbackStub { + public: + TestRegisterSessionStateCallbackStub() = default; + virtual ~TestRegisterSessionStateCallbackStub() = default; + void OnResult(const std::string &devId, const uint32_t status); + }; private: sptr inputNodelistener_; }; diff --git a/services/state/include/dinput_sink_state.h b/services/state/include/dinput_sink_state.h index 5844b1ff46b21db0c2c9309850a4e9e73fb838d7..aa41ec9ee7de29a5fa66dab313f5492307f798bf 100644 --- a/services/state/include/dinput_sink_state.h +++ b/services/state/include/dinput_sink_state.h @@ -48,6 +48,9 @@ public: int32_t RemoveDhIds(const std::vector &dhIds); DhIdState GetStateByDhid(const std::string &dhId); + /* Is some dhid in through out to the other side */ + bool IsInThroughtOut(); + void AddKeyDownState(struct RawEvent event); void RemoveKeyDownState(struct RawEvent event); /** diff --git a/services/state/src/dinput_sink_state.cpp b/services/state/src/dinput_sink_state.cpp index 5311a2b12d7f0dfdda3bc1de6496e90c2320eb4f..6bb32653f994c8f38bd48f6514f433fae8550224 100644 --- a/services/state/src/dinput_sink_state.cpp +++ b/services/state/src/dinput_sink_state.cpp @@ -242,6 +242,20 @@ void DInputSinkState::ClearDeviceStates() std::lock_guard mapLock(keyDownStateMapMtx_); keyDownStateMap_.clear(); } + +bool DInputSinkState::IsInThroughtOut() +{ + std::lock_guard mapLock(operationMutex_); + bool flag = false; + for (auto it : dhIdStateMap_) { + if (it.second == DhIdState::THROUGH_OUT) { + flag = true; + break; + } + } + DHLOGD("IsInThroughtOut: %d", flag); + return flag; +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOSs \ No newline at end of file diff --git a/services/state/test/BUILD.gn b/services/state/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..054a0a9e5ae5eba003a57f315a7a3c4f126aa334 --- /dev/null +++ b/services/state/test/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2024 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. + +group("test") { + testonly = true + + deps = [ "dinpusinkstatetest:dinpusinkstatetest" ] +} diff --git a/services/state/test/dinpusinkstatetest/BUILD.gn b/services/state/test/dinpusinkstatetest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..325142293aa969663f70143409fd530de8827438 --- /dev/null +++ b/services/state/test/dinpusinkstatetest/BUILD.gn @@ -0,0 +1,59 @@ +# Copyright (c) 2024 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. + +import("//build/test.gni") +import("../../../../distributedinput.gni") + +module_out_path = "distributed_input/state" + +group("dinpusinkstatetest") { + testonly = true + + deps = [ ":dinput_sink_state_test" ] +} + +## UnitTest dinput_sink_state_test {{{ +ohos_unittest("dinput_sink_state_test") { + module_out_path = module_out_path + + include_dirs = [ + "${common_path}/include", + "${fwk_common_path}/utils/include", + "${services_state_path}/include", + ] + + sources = [ "dinput_sink_state_test.cpp" ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"distributedinpututtest\"", + "LOG_DOMAIN=0xD004120", + ] + + deps = [ + "${services_state_path}:libdinput_sink_state", + "//third_party/libevdev:libevdev", + ] + + external_deps = [ "c_utils:utils" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] +} +## UnitTest dinput_sink_state_test }}} diff --git a/services/state/test/dinpusinkstatetest/dinput_sink_state_test.cpp b/services/state/test/dinpusinkstatetest/dinput_sink_state_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05008ee92829abaa4206343af49bd9ccb0068d30 --- /dev/null +++ b/services/state/test/dinpusinkstatetest/dinput_sink_state_test.cpp @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "constants_dinput.h" +#include "dinput_sink_state.h" +#include "dinput_errcode.h" +#include "touchpad_event_fragment_mgr.h" + +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +using namespace std; +namespace { +const std::string DHID_1 = "input_159753qazplm"; +RawEvent EVENT_1 = { + .when = 0, + .type = EV_KEY, + .code = KEY_D, + .value = 1, + .descriptor = DHID_1 +}; +RawEvent EVENT_2 = { + .when = 1, + .type = EV_ABS, + .code = KEY_D, + .value = 0, + .descriptor = DHID_1 +}; +RawEvent EVENT_3 = { + .when = 1, + .type = EV_ABS, + .code = ABS_X, + .value = 0, + .descriptor = DHID_1 +}; + +} +class DinputSinkStateTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void DinputSinkStateTest::SetUpTestCase() +{ +} + +void DinputSinkStateTest::TearDownTestCase() +{ +} + +void DinputSinkStateTest::SetUp() +{ +} + +void DinputSinkStateTest::TearDown() +{ +} + +HWTEST_F(DinputSinkStateTest, RecordDhIds_001, testing::ext::TestSize.Level0) +{ + std::vector dhIds; + dhIds.push_back(DHID_1); + DhIdState state = DhIdState::THROUGH_IN; + int32_t sessionId = 1; + int32_t ret = DInputSinkState::GetInstance().RecordDhIds(dhIds, state, sessionId); + EXPECT_EQ(DH_SUCCESS, ret); +} + +HWTEST_F(DinputSinkStateTest, RecordDhIds_002, testing::ext::TestSize.Level0) +{ + std::vector dhIds; + dhIds.push_back(DHID_1); + DhIdState state = DhIdState::THROUGH_OUT; + int32_t sessionId = -1; + DInputSinkState::GetInstance().dhIdStateMap_.clear(); + int32_t ret = DInputSinkState::GetInstance().RecordDhIds(dhIds, state, sessionId); + EXPECT_EQ(DH_SUCCESS, ret); +} + +HWTEST_F(DinputSinkStateTest, RemoveDhIds_001, testing::ext::TestSize.Level0) +{ + std::vector dhIds; + dhIds.push_back(DHID_1); + int32_t ret = DInputSinkState::GetInstance().RemoveDhIds(dhIds); + EXPECT_EQ(DH_SUCCESS, ret); +} + +HWTEST_F(DinputSinkStateTest, GetStateByDhid_001, testing::ext::TestSize.Level0) +{ + DInputSinkState::GetInstance().dhIdStateMap_[DHID_1] = DhIdState::THROUGH_OUT; + DhIdState ret = DInputSinkState::GetInstance().GetStateByDhid(DHID_1); + EXPECT_EQ(DhIdState::THROUGH_OUT, ret); +} + +HWTEST_F(DinputSinkStateTest, GetStateByDhid_002, testing::ext::TestSize.Level0) +{ + std::string dhId = "dhId_test"; + DhIdState ret = DInputSinkState::GetInstance().GetStateByDhid(dhId); + EXPECT_EQ(DhIdState::THROUGH_IN, ret); +} + +HWTEST_F(DinputSinkStateTest, SimulateEventInjectToSrc_001, testing::ext::TestSize.Level0) +{ + int32_t sessionId = 1; + std::string dhId = "dhId_test"; + std::vector dhIds; + dhIds.push_back(dhId); + DInputSinkState::GetInstance().Init(); + DInputSinkState::GetInstance().AddKeyDownState(EVENT_1); + DInputSinkState::GetInstance().SimulateEventInjectToSrc(sessionId, dhIds); + + dhIds.clear(); + dhIds.push_back(DHID_1); + DInputSinkState::GetInstance().SimulateEventInjectToSrc(sessionId, dhIds); + EXPECT_EQ(DInputSinkState::GetInstance().keyDownStateMap_.size(), 0); +} + +HWTEST_F(DinputSinkStateTest, RemoveKeyDownState_001, testing::ext::TestSize.Level0) +{ + DInputSinkState::GetInstance().keyDownStateMap_.clear(); + std::string dhId = "dhId_test"; + std::vector dhIds; + dhIds.push_back(dhId); + DInputSinkState::GetInstance().AddKeyDownState(EVENT_1); + DInputSinkState::GetInstance().RemoveKeyDownState(EVENT_1); + + dhIds.clear(); + dhIds.push_back(DHID_1); + DInputSinkState::GetInstance().RemoveKeyDownState(EVENT_1); + EXPECT_EQ(DInputSinkState::GetInstance().keyDownStateMap_.size(), 0); +} + +HWTEST_F(DinputSinkStateTest, IsPositionEvent_001, testing::ext::TestSize.Level0) +{ + DInputSinkState::GetInstance().Init(); + auto ret = DInputSinkState::GetInstance().GetTouchPadEventFragMgr()->IsPositionEvent(EVENT_2); + EXPECT_EQ(false, ret); +} + +HWTEST_F(DinputSinkStateTest, PushEvent_001, testing::ext::TestSize.Level0) +{ + auto ret = DInputSinkState::GetInstance().GetTouchPadEventFragMgr()->PushEvent(DHID_1, EVENT_1); + EXPECT_EQ(false, ret.first); + + ret = DInputSinkState::GetInstance().GetTouchPadEventFragMgr()->PushEvent(DHID_1, EVENT_3); + EXPECT_EQ(false, ret.first); +} + +HWTEST_F(DinputSinkStateTest, GetAndClearEvents_001, testing::ext::TestSize.Level0) +{ + std::string dhId = "dhId_test"; + auto ret = DInputSinkState::GetInstance().GetTouchPadEventFragMgr()->GetAndClearEvents(dhId); + EXPECT_EQ(0, ret.size()); +} +} +} +} \ No newline at end of file diff --git a/services/transportbase/test/transbaseunittest/BUILD.gn b/services/transportbase/test/transbaseunittest/BUILD.gn index cb99469f14ded394ffc0c9bd7b664d0ab413b511..98685993c88271a3828a00e3902ee92d55df43f5 100644 --- a/services/transportbase/test/transbaseunittest/BUILD.gn +++ b/services/transportbase/test/transbaseunittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022-2023 Huawei Device Co., Ltd. +# Copyright (c) 2022-2024 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 @@ -45,6 +45,7 @@ ohos_unittest("distributed_input_transbase_test") { "${distributedinput_path}/inputdevicehandler/include", "${distributedinput_path}/services/state/include", "${common_path}/test/mock", + "${ipc_path}/include", ] sources = [ @@ -73,6 +74,7 @@ ohos_unittest("distributed_input_transbase_test") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", + "${innerkits_path}:libdinput_sdk", "${services_source_path}/inputinject:libdinput_inject", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", @@ -84,6 +86,7 @@ ohos_unittest("distributed_input_transbase_test") { "eventhandler:libeventhandler", "hicollie:libhicollie", "hitrace:hitrace_meter", + "ipc:ipc_core", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/services/transportbase/test/transbaseunittest/distributed_input_transbase_test.cpp b/services/transportbase/test/transbaseunittest/distributed_input_transbase_test.cpp index 0c14b471f6b88c5fd9dbfca800d11634f0fb20ed..b892ac9eaf959448b29c0e4552bc613945ce40dc 100644 --- a/services/transportbase/test/transbaseunittest/distributed_input_transbase_test.cpp +++ b/services/transportbase/test/transbaseunittest/distributed_input_transbase_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 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 @@ -55,6 +55,13 @@ void DistributedInputTransbaseTest::TearDownTestCase() { } +void DistributedInputTransbaseTest::TestRegisterSessionStateCallbackStub::OnResult(const std::string &devId, + const uint32_t status) +{ + (void)devId; + (void)status; +} + HWTEST_F(DistributedInputTransbaseTest, Init01, testing::ext::TestSize.Level0) { int32_t ret = DistributedInputTransportBase::GetInstance().Init(); @@ -177,6 +184,49 @@ HWTEST_F(DistributedInputTransbaseTest, Release01, testing::ext::TestSize.Level1 EXPECT_EQ(0, DistributedInputTransportBase::GetInstance().channelStatusMap_.size()); } +HWTEST_F(DistributedInputTransbaseTest, StopSession_001, testing::ext::TestSize.Level1) +{ + DistributedInputTransportBase::GetInstance().remoteDevSessionMap_.clear(); + std::string remoteDevId = "remoteDevId_test"; + DistributedInputTransportBase::GetInstance().StopSession(remoteDevId); + EXPECT_EQ(0, DistributedInputTransportBase::GetInstance().remoteDevSessionMap_.size()); +} + +HWTEST_F(DistributedInputTransbaseTest, RunSessionStateCallback_001, testing::ext::TestSize.Level1) +{ + std::string remoteDevId = "remoteDevId_test"; + uint32_t sessionState = 1; + sptr callback(new TestRegisterSessionStateCallbackStub()); + DistributedInputTransportBase::GetInstance().RegisterSessionStateCb(callback); + DistributedInputTransportBase::GetInstance().RunSessionStateCallback(remoteDevId, sessionState); + DistributedInputTransportBase::GetInstance().UnregisterSessionStateCb(); + auto ret = DistributedInputTransportBase::GetInstance().CountSession(remoteDevId); + EXPECT_EQ(DH_SUCCESS, ret); + +} + +HWTEST_F(DistributedInputTransbaseTest, OnSessionClosed_001, testing::ext::TestSize.Level1) +{ + int32_t sessionId = 1; + std::string devId = "devId_741258"; + ShutdownReason reason = ShutdownReason::SHUTDOWN_REASON_UNKNOWN; + DistributedInputTransportBase::GetInstance().OnSessionClosed(sessionId, reason); + + DistributedInputTransportBase::GetInstance().remoteDevSessionMap_[devId] = sessionId; + DistributedInputTransportBase::GetInstance().OnSessionClosed(sessionId, reason); + + DistributedInputTransportBase::GetInstance().sinkCallback_ = std::shared_ptr(); + DistributedInputTransportBase::GetInstance().OnSessionClosed(sessionId, reason); + + DistributedInputTransportBase::GetInstance().srcCallback_ = std::shared_ptr(); + DistributedInputTransportBase::GetInstance().OnSessionClosed(sessionId, reason); + + DistributedInputTransportBase::GetInstance().srcMgrCallback_ = std::shared_ptr(); + DistributedInputTransportBase::GetInstance().OnSessionClosed(sessionId, reason); + auto ret = DistributedInputTransportBase::GetInstance().CountSession(devId); + EXPECT_EQ(DH_SUCCESS, ret); +} + } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/services/transportbase/test/transbaseunittest/distributed_input_transbase_test.h b/services/transportbase/test/transbaseunittest/distributed_input_transbase_test.h index 5392378885cb4e0b5edab243a348d14dee357e47..56bb08943146b29b1e8c723cf5cc270c2bfb3fc9 100644 --- a/services/transportbase/test/transbaseunittest/distributed_input_transbase_test.h +++ b/services/transportbase/test/transbaseunittest/distributed_input_transbase_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 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 @@ -29,6 +29,7 @@ #include "distributed_input_transport_base.h" #include "dinput_transbase_source_callback.h" #include "dinput_transbase_sink_callback.h" +#include "register_session_state_callback_stub.h" namespace OHOS { namespace DistributedHardware { @@ -40,6 +41,14 @@ public: void SetUp() override; void TearDown() override; + class TestRegisterSessionStateCallbackStub : + public OHOS::DistributedHardware::DistributedInput::RegisterSessionStateCallbackStub { + public: + TestRegisterSessionStateCallbackStub() = default; + virtual ~TestRegisterSessionStateCallbackStub() = default; + void OnResult(const std::string &devId, const uint32_t status); + }; + private: DistributedInputSourceTransport* sourceTransport_; std::shared_ptr statusSourceListener_;