diff --git a/sourcehandler/BUILD.gn b/sourcehandler/BUILD.gn new file mode 100644 index 0000000..9433d90 --- /dev/null +++ b/sourcehandler/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2021-2022 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/ohos.gni") +import("//foundation/distributedhardware/distributedinput/distributedinput.gni") + +ohos_shared_library("libdinput_source_handler") { + include_dirs = [ + "include", + "${innerkits_path}/include", + "${innerkits_path}/src", + "${ipc_path}/include", + "${ipc_path}/src", + "${services_source_path}/sourcemanager/include", + "${frameworks_path}/include", + "${service_common}/include", + "${common_path}/include", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${fwk_common_path}/log/include", + "${fwk_common_path}/utils/include", + "${fwk_utils_path}/include/log", + "${fwk_utils_path}/include", + ] + + sources = [ + "src/distributed_input_source_handler.cpp", + "src/load_d_input_source_callback.cpp" + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"distributedinputsourcehandler\"", + "LOG_DOMAIN=0xD004100", + ] + + deps = [ + "//foundation/bundlemanager/bundle_framework/interfaces/innerkits/libeventhandler:libeventhandler", + "//utils/native/base:utils", + "${fwk_utils_path}:distributedhardwareutils", + "${innerkits_path}:libdinput_sdk", + "${services_source_path}/sourcemanager:libdinput_source", + ] + + external_deps = [ + "ipc:ipc_core", + "safwk:system_ability_fwk", + "hiviewdfx_hilog_native:libhilog", + "samgr_standard:samgr_proxy", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_input" + +} diff --git a/sourcehandler/include/distributed_input_source_handler.h b/sourcehandler/include/distributed_input_source_handler.h new file mode 100644 index 0000000..4b904ab --- /dev/null +++ b/sourcehandler/include/distributed_input_source_handler.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2021-2022 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 DISTRIBUTED_INPUT_SOURCE_HANDLER_H +#define DISTRIBUTED_INPUT_SOURCE_HANDLER_H + +#include +#include +#include + +#include "idistributed_hardware_source.h" +#include "iservice_registry.h" +#include "single_instance.h" +#include "system_ability_load_callback_stub.h" +#include "system_ability_definition.h" + +#include "distributed_input_client.h" +#include "distributed_input_source_manager.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class DistributedInputSourceHandler : public IDistributedHardwareSource { +DECLARE_SINGLE_INSTANCE_BASE(DistributedInputSourceHandler); +public: + int32_t InitSource(const std::string& params) override; + int32_t ReleaseSource() override; + int32_t RegisterDistributedHardware(const std::string& devId, const std::string& dhId, + const EnableParam& param, std::shared_ptr callback) override; + int32_t UnregisterDistributedHardware(const std::string& devId, const std::string& dhId, + std::shared_ptr callback) override; + int32_t ConfigDistributedHardware(const std::string& devId, const std::string& dhId, const std::string& key, + const std::string& value) override; + void FinishStartSA(const std::string ¶ms, const sptr &remoteObject); + +public: + + class SALoadSourceCb : public OHOS::SystemAbilityLoadCallbackStub { + public: + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const OHOS::sptr& remoteObject) override; + + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; + + int32_t GetSystemAbilityId() const + { + return currSystemAbilityId; + } + + OHOS::sptr GetRemoteObject() const + { + return currRemoteObject; + } + private: + int32_t currSystemAbilityId = 0; + OHOS::sptr currRemoteObject; + }; +private: + DistributedInputSourceHandler() = default; + ~DistributedInputSourceHandler(); + OHOS::sptr sysSourceCallback = nullptr; + + std::mutex proxyMutex_; + std::condition_variable proxyConVar_; +}; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus +__attribute__((visibility("default"))) IDistributedHardwareSource *GetSourceHardwareHandler(); +#ifdef __cplusplus +} +#endif // __cplusplus +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS + +#endif // DISTRIBUTED_INPUT_SOURCE_HANDLER_H diff --git a/sourcehandler/include/load_d_input_source_callback.h b/sourcehandler/include/load_d_input_source_callback.h new file mode 100644 index 0000000..b14f791 --- /dev/null +++ b/sourcehandler/include/load_d_input_source_callback.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 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 LOAD_D_INPUT_SOURCE_CALLBACK_H +#define LOAD_D_INPUT_SOURCE_CALLBACK_H + +#include "system_ability_load_callback_stub.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class LoadDInputSourceCallback : public SystemAbilityLoadCallbackStub { +public: + explicit LoadDInputSourceCallback(const std::string ¶ms); + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr &remoteObject) override; + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; +private: + std::string params_; +}; +} +} +} +#endif \ No newline at end of file diff --git a/sourcehandler/src/distributed_input_source_handler.cpp b/sourcehandler/src/distributed_input_source_handler.cpp new file mode 100644 index 0000000..0e0e27e --- /dev/null +++ b/sourcehandler/src/distributed_input_source_handler.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2021-2022 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 "distributed_input_source_handler.h" + +#include "distributed_hardware_log.h" + +#include "dinput_errcode.h" +#include "i_distributed_source_input.h" +#include "load_d_input_source_callback.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +IMPLEMENT_SINGLE_INSTANCE(DistributedInputSourceHandler); + +DistributedInputSourceHandler::~DistributedInputSourceHandler() +{ +} + +int32_t DistributedInputSourceHandler::InitSource(const std::string ¶ms) +{ + DHLOGD("InitSource"); + std::unique_lock lock(proxyMutex_); + if (!DistributedInputClient::GetInstance().HasDInputSourceProxy()) { + sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!samgr) { + DHLOGE("Failed to get system ability mgr."); + return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL; + } + sptr loadCallback = new LoadDInputSourceCallback(params); + int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, loadCallback); + if (ret != ERR_OK) { + DHLOGE("Failed to Load systemAbility, systemAbilityId:%d, ret code:%d", + DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, ret); + return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL; + } + } + + auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(INPUT_LOADSA_TIMEOUT_MS), + [this]() { return (DistributedInputClient::GetInstance().HasDInputSourceProxy()); }); + if (!waitStatus) { + DHLOGE("dinput load sa timeout."); + return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL; + } + + return DH_SUCCESS; +} + +void DistributedInputSourceHandler::FinishStartSA(const std::string ¶ms, const sptr &remoteObject) +{ + DHLOGD("FinishStartSA"); + std::unique_lock lock(proxyMutex_); + DistributedInputClient::GetInstance().SetDInputSourceProxy(remoteObject); + DistributedInputClient::GetInstance().InitSource(); + proxyConVar_.notify_all(); +} + +int32_t DistributedInputSourceHandler::ReleaseSource() +{ + return DistributedInputClient::GetInstance().ReleaseSource(); +} + +int32_t DistributedInputSourceHandler::RegisterDistributedHardware(const std::string &devId, + const std::string &dhId, const EnableParam ¶m, std::shared_ptr callback) +{ + return DistributedInputClient::GetInstance().RegisterDistributedHardware(devId, dhId, param.attrs, callback); +} + +int32_t DistributedInputSourceHandler::UnregisterDistributedHardware(const std::string &devId, + const std::string &dhId, std::shared_ptr callback) +{ + return DistributedInputClient::GetInstance().UnregisterDistributedHardware(devId, dhId, callback); +} + +int32_t DistributedInputSourceHandler::ConfigDistributedHardware(const std::string &devId, + const std::string &dhId, const std::string &key, const std::string &value) +{ + return DH_SUCCESS; +} + +void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const OHOS::sptr& remoteObject) +{ + currSystemAbilityId = systemAbilityId; + currRemoteObject = remoteObject; + DHLOGI("DistributedInputSourceHandler OnLoadSystemAbilitySuccess. systemAbilityId=%d", systemAbilityId); +} + +void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + currSystemAbilityId = systemAbilityId; + DHLOGE("DistributedInputSourceHandler OnLoadSystemAbilityFail. systemAbilityId=%d", systemAbilityId); +} + +IDistributedHardwareSource *GetSourceHardwareHandler() +{ + return &DistributedInputSourceHandler::GetInstance(); +} +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS diff --git a/sourcehandler/src/load_d_input_source_callback.cpp b/sourcehandler/src/load_d_input_source_callback.cpp new file mode 100644 index 0000000..815f4da --- /dev/null +++ b/sourcehandler/src/load_d_input_source_callback.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 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 "load_d_input_source_callback.h" + +#include "distributed_hardware_log.h" + +#include "distributed_input_source_handler.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +LoadDInputSourceCallback::LoadDInputSourceCallback(const std::string ¶ms) : params_(params) {} + +void LoadDInputSourceCallback::OnLoadSystemAbilitySuccess( + int32_t systemAbilityId, const sptr &remoteObject) +{ + DHLOGI("load dinput SA success, systemAbilityId:%d, remoteObject result:%s", + systemAbilityId, (remoteObject != nullptr) ? "true" : "false"); + if (remoteObject == nullptr) { + DHLOGE("remoteObject is nullptr"); + return; + } + DistributedInputSourceHandler::GetInstance().FinishStartSA(params_, remoteObject); +} + +void LoadDInputSourceCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + DHLOGE("load dinput SA failed, systemAbilityId:%d", systemAbilityId); +} +} +} +} diff --git a/sourcehandler/test/BUILD.gn b/sourcehandler/test/BUILD.gn new file mode 100644 index 0000000..e75f084 --- /dev/null +++ b/sourcehandler/test/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2021-2022 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 = [ "unittest:unittest" ] +} diff --git a/sourcehandler/test/unittest/BUILD.gn b/sourcehandler/test/unittest/BUILD.gn new file mode 100644 index 0000000..4168bd4 --- /dev/null +++ b/sourcehandler/test/unittest/BUILD.gn @@ -0,0 +1,86 @@ +# Copyright (c) 2021-2022 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("//foundation/distributedhardware/distributedinput/distributedinput.gni") +module_out_path = "distributedinput_stander/source_handler" + +group("unittest") { + testonly = true + + deps = [ ":distributed_input_source_handler_test" ] +} + +## UnitTest distributed_input_manager_service_test {{{ +ohos_unittest("distributed_input_source_handler_test") { + module_out_path = module_out_path + + include_dirs = [ + "${distributedinput_path}/sourcehandler/include", + "${innerkits_path}/include", + "${innerkits_path}/src", + "${ipc_path}/include", + "${ipc_path}/src", + "${services_source_path}/sourcemanager/include", + "${frameworks_path}/include", + "${service_common}/include", + "${common_path}/include", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${fwk_common_path}/log/include", + "${fwk_common_path}/utils/include", + "${fwk_utils_path}/include/log", + "${fwk_utils_path}/include", + "//third_party/json/include", + ] + + sources = [ + "//foundation/distributedhardware/distributedinput/sourcehandler/src/distributed_input_source_handler.cpp", + "//foundation/distributedhardware/distributedinput/sourcehandler/src/load_d_input_source_callback.cpp", + "distributed_input_sourcehandler_test.cpp", + "//foundation/distributedhardware/distributedinput/sourcehandler/test/unittest/mock/mock_distributed_input_client.cpp", + ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + "-Dprotected=public", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"distributedinpututtest\"", + "LOG_DOMAIN=0xD004100", + ] + + deps = [ + "//foundation/bundlemanager/bundle_framework/interfaces/innerkits/libeventhandler:libeventhandler", + "//utils/native/base:utils", + "${fwk_utils_path}:distributedhardwareutils", + "${innerkits_path}:libdinput_sdk", + "${services_source_path}/sourcemanager:libdinput_source", + ] + + external_deps = [ + "ipc:ipc_core", + "safwk:system_ability_fwk", + "hiviewdfx_hilog_native:libhilog", + "samgr_standard:samgr_proxy", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] +} + +## UnitTest distributed_input_manager_service_test }}} diff --git a/sourcehandler/test/unittest/distributed_input_sourcehandler_test.cpp b/sourcehandler/test/unittest/distributed_input_sourcehandler_test.cpp new file mode 100644 index 0000000..81bcf97 --- /dev/null +++ b/sourcehandler/test/unittest/distributed_input_sourcehandler_test.cpp @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2021-2022 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 "distributed_input_sourcehandler_test.h" + +using namespace testing::ext; +using namespace OHOS::DistributedHardware::DistributedInput; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +void DistributedInputSourceHandlerTest::SetUp() +{ +} + +void DistributedInputSourceHandlerTest::TearDown() +{ +} + +void DistributedInputSourceHandlerTest::SetUpTestCase() +{ +} + +void DistributedInputSourceHandlerTest::TearDownTestCase() +{ +} + +int32_t DistributedInputSourceHandlerTest::TestRegisterDInputCallback::OnRegisterResult(const std::string &devId, + const std::string &dhId, int32_t status, const std::string &data) +{ + std::cout << "OnRegisterResult" << std::endl; + return SUCCESS; +} + +int32_t DistributedInputSourceHandlerTest::TestUnregisterDInputCallback::OnUnregisterResult(const std::string &devId, + const std::string &dhId, int32_t status, const std::string &data) +{ + std::cout << "OnUnregisterResult" << std::endl; + return SUCCESS; +} + +HWTEST_F(DistributedInputSourceHandlerTest, InitSource01, testing::ext::TestSize.Level0) +{ + int32_t ret = DistributedInputSourceHandler::GetInstance().InitSource(""); + EXPECT_EQ(SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceHandlerTest, InitSource02, testing::ext::TestSize.Level0) +{ + std::string dhId = "test"; + int32_t ret = DistributedInputSourceHandler::GetInstance().InitSource(dhId); + EXPECT_EQ(SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceHandlerTest, ReleaseSource01, testing::ext::TestSize.Level0) +{ + int32_t ret = DistributedInputSourceHandler::GetInstance().ReleaseSource(); + EXPECT_EQ(SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceHandlerTest, RegisterDistributedHardware01, testing::ext::TestSize.Level0) +{ + std::shared_ptr registerCallback = std::make_shared(); + int32_t ret = DistributedInputSourceHandler::GetInstance().RegisterDistributedHardware( + "devId", + "dhId", + OHOS::DistributedHardware::EnableParam { + "version", "attrs" + }, + registerCallback); + EXPECT_EQ(SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceHandlerTest, RegisterDistributedHardware02, testing::ext::TestSize.Level0) +{ + std::cout << "RegisterDistributedHardware02"<< std::endl; + std::shared_ptr registerCallback = nullptr; + std::string devId = ""; + std::string dhId = ""; + int32_t ret = DistributedInputSourceHandler::GetInstance().RegisterDistributedHardware( + devId, dhId, OHOS::DistributedHardware::EnableParam {"version", "attrs" }, registerCallback); + EXPECT_EQ(SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceHandlerTest, RegisterDistributedHardware03, testing::ext::TestSize.Level0) +{ + std::cout << "RegisterDistributedHardware03"<< std::endl; + std::shared_ptr registerCallback = std::make_shared(); + int32_t ret = DistributedInputSourceHandler::GetInstance().RegisterDistributedHardware( + "devId", + "dhId", + OHOS::DistributedHardware::EnableParam { + "version", "attrs" + }, + registerCallback); + EXPECT_EQ(SUCCESS, ret); +} +HWTEST_F(DistributedInputSourceHandlerTest, UnregisterDistributedHardware01, testing::ext::TestSize.Level0) +{ + std::cout << "UnregisterDistributedHardware01"<< std::endl; + std::shared_ptr unregisterDInputCallback = + std::make_shared(); + int32_t ret = DistributedInputSourceHandler::GetInstance().UnregisterDistributedHardware("devId", + "dhId", unregisterDInputCallback); + EXPECT_EQ(SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceHandlerTest, UnregisterDistributedHardware02, testing::ext::TestSize.Level0) +{ + std::shared_ptr unregisterDInputCallback = nullptr; + std::string devId = ""; + std::string dhId = ""; + int32_t ret = DistributedInputSourceHandler::GetInstance().UnregisterDistributedHardware("devId", + "dhId", unregisterDInputCallback); + EXPECT_EQ(SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceHandlerTest, UnregisterDistributedHardware03, testing::ext::TestSize.Level0) +{ + std::shared_ptr unregisterDInputCallback = + std::make_shared(); + + int32_t ret = DistributedInputSourceHandler::GetInstance().UnregisterDistributedHardware("devId", + "dhId", unregisterDInputCallback); + EXPECT_EQ(SUCCESS, ret); +} + +HWTEST_F(DistributedInputSourceHandlerTest, ConfigDistributedHardware01, testing::ext::TestSize.Level0) +{ + int32_t ret = DistributedInputSourceHandler::GetInstance().ConfigDistributedHardware("devId", + "dhId", "key", "value"); + EXPECT_EQ(SUCCESS, ret); +} +} +} +} \ No newline at end of file diff --git a/sourcehandler/test/unittest/distributed_input_sourcehandler_test.h b/sourcehandler/test/unittest/distributed_input_sourcehandler_test.h new file mode 100644 index 0000000..06793be --- /dev/null +++ b/sourcehandler/test/unittest/distributed_input_sourcehandler_test.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2021-2022 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 DISRIBUTED_INPUT_SOURCEHANDLER_TEST_H +#define DISRIBUTED_INPUT_SOURCEHANDLER_TEST_H + +#include +#include +#include + +#include +#include + +#include "idistributed_hardware_source.h" +#include "system_ability_load_callback_stub.h" + +#include "constants_dinput.h" +#include "distributed_input_kit.h" +#include "distributed_input_source_handler.h" +#include "i_distributed_source_input.h" +#include "register_d_input_call_back_stub.h" +#include "unregister_d_input_call_back_stub.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class DistributedInputSourceHandlerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; + int32_t CheckSystemAbility(); + + class TestRegisterDInputCallback : public OHOS::DistributedHardware::RegisterCallback { + public: + TestRegisterDInputCallback() = default; + virtual ~TestRegisterDInputCallback() = default; + virtual int32_t OnRegisterResult(const std::string &devId, const std::string &dhId, int32_t status, + const std::string &data) override; + }; + + class TestUnregisterDInputCallback : public OHOS::DistributedHardware::UnregisterCallback { + public: + TestUnregisterDInputCallback() = default; + virtual ~TestUnregisterDInputCallback() = default; + virtual int32_t OnUnregisterResult(const std::string &devId, const std::string &dhId, int32_t status, + const std::string &data) override; + }; +}; +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS +#endif // DISRIBUTED_INPUT_SOURCEHANDLER_TEST_H diff --git a/sourcehandler/test/unittest/mock/mock_distributed_input_client.cpp b/sourcehandler/test/unittest/mock/mock_distributed_input_client.cpp new file mode 100644 index 0000000..c12cd15 --- /dev/null +++ b/sourcehandler/test/unittest/mock/mock_distributed_input_client.cpp @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2021-2022 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 "nlohmann/json.hpp" +#include "distributed_hardware_log.h" + +#include "constants_dinput.h" +#include "distributed_input_client.h" +#include "white_list_util.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +DistributedInputClient &DistributedInputClient::GetInstance() +{ + static DistributedInputClient instance; + return instance; +} + +void DistributedInputClient::RegisterDInputCb::OnResult( + const std::string& devId, const std::string& dhId, const int32_t& status) +{ + for (std::vector::iterator iter = + DistributedInputClient::GetInstance().dHardWareFwkRstInfos.begin(); + iter != DistributedInputClient::GetInstance().dHardWareFwkRstInfos.end(); + iter++) { + if (iter->devId == devId && iter->dhId == dhId) { + iter->callback->OnRegisterResult(devId, dhId, status, ""); + DistributedInputClient::GetInstance().dHardWareFwkRstInfos.erase(iter); + return; + } + } +} + +void DistributedInputClient::UnregisterDInputCb::OnResult( + const std::string& devId, const std::string& dhId, const int32_t& status) + +{ + for (std::vector::iterator iter = + DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos.begin(); + iter != DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos.end(); + iter++) { + if (iter->devId == devId && iter->dhId == dhId) { + iter->callback->OnUnregisterResult(devId, dhId, status, ""); + DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos.erase(iter); + return; + } + } +} + +void DistributedInputClient::StartDInputServerCb::OnResult(const int32_t& status, const uint32_t& inputTypes) +{ + if (DInputServerType::SOURCE_SERVER_TYPE == static_cast(status)) { + DistributedInputClient::GetInstance().serverType = DInputServerType::SOURCE_SERVER_TYPE; + DistributedInputClient::GetInstance().inputTypes = inputTypes; + } else if (DInputServerType::SINK_SERVER_TYPE == static_cast(status)) { + DistributedInputClient::GetInstance().serverType = DInputServerType::SINK_SERVER_TYPE; + DistributedInputClient::GetInstance().inputTypes = inputTypes; + } else { + DistributedInputClient::GetInstance().serverType = DInputServerType::NULL_SERVER_TYPE; + DistributedInputClient::GetInstance().inputTypes = INPUT_TYPE_NULL; + } +} + +void DistributedInputClient::AddWhiteListInfosCb::OnResult(const std::string &deviceId, const std::string &strJson) +{ + nlohmann::json inputData = nlohmann::json::parse(strJson); + int jsonSize = inputData.size(); + DHLOGI("AddWhiteListInfosCb OnResult json size:%d.\n", jsonSize); + TYPE_WHITE_LIST_VEC vecWhiteList = inputData; + WhiteListUtil::GetInstance().SyncWhiteList(deviceId, vecWhiteList); +} + +void DistributedInputClient::DelWhiteListInfosCb::OnResult(const std::string& deviceId) +{ + WhiteListUtil::GetInstance().ClearWhiteList(deviceId); +} + +int32_t DistributedInputClient::InitSource() +{ + return SUCCESS; +} + +int32_t DistributedInputClient::InitSink() +{ + return SUCCESS; +} + +int32_t DistributedInputClient::ReleaseSource() +{ + return SUCCESS; +} + +int32_t DistributedInputClient::ReleaseSink() +{ + return SUCCESS; +} + +int32_t DistributedInputClient::RegisterDistributedHardware(const std::string& devId, const std::string& dhId, + const std::string& parameters, const std::shared_ptr& callback) +{ + return SUCCESS; +} + +int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string& devId, const std::string& dhId, + const std::shared_ptr& callback) +{ + return SUCCESS; +} + +int32_t DistributedInputClient::PrepareRemoteInput( + const std::string& deviceId, sptr callback) +{ + return SUCCESS; +} + +int32_t DistributedInputClient::UnprepareRemoteInput( + const std::string& deviceId, sptr callback) +{ + return SUCCESS; +} + +int32_t DistributedInputClient::StartRemoteInput( + const std::string& deviceId, const uint32_t& inputTypes, sptr callback) +{ + return SUCCESS; +} + +int32_t DistributedInputClient::StopRemoteInput( + const std::string& deviceId, const uint32_t& inputTypes, sptr callback) +{ + return SUCCESS; +} + +bool DistributedInputClient::IsNeedFilterOut(const std::string& deviceId, const BusinessEvent& event) +{ + return true; +} + +DInputServerType DistributedInputClient::IsStartDistributedInput(const uint32_t& inputType) +{ + return serverType; +} + +bool DistributedInputClient::GetDInputSourceProxy() +{ + return true; +} + +bool DistributedInputClient::GetDInputSinkProxy() +{ + return true; +} + +bool DistributedInputClient::IsJsonData(std::string strData) const +{ + return true; +} +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS