From fe8cf1784e670929ef9fe521637f780591d2e0b0 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Fri, 14 Jul 2023 12:00:43 +0800 Subject: [PATCH 1/6] add dinput function test Signed-off-by: wangxuanxuan --- test/BUILD.gn | 10 +- test/functiontest/BUILD.gn | 58 ++++++++ test/functiontest/Test.json | 21 +++ test/functiontest/dinput_automat_test.cpp | 104 ++++++++++++++ test/functiontest/dinput_demo.cpp | 162 ++++++++++++++++++++++ test/functiontest/dinput_demo.h | 96 +++++++++++++ 6 files changed, 450 insertions(+), 1 deletion(-) create mode 100644 test/functiontest/BUILD.gn create mode 100644 test/functiontest/Test.json create mode 100644 test/functiontest/dinput_automat_test.cpp create mode 100644 test/functiontest/dinput_demo.cpp create mode 100644 test/functiontest/dinput_demo.h diff --git a/test/BUILD.gn b/test/BUILD.gn index d43e349..00e85cb 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 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 @@ -11,8 +11,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +declare_args() { + distributedhardware_function_test = false +} + group("test") { testonly = true deps = [ "fuzztest:fuzztest" ] + + if (distributedhardware_function_test) { + deps += [ "functiontest:dinputfunctiontest" ] + } } diff --git a/test/functiontest/BUILD.gn b/test/functiontest/BUILD.gn new file mode 100644 index 0000000..08d3026 --- /dev/null +++ b/test/functiontest/BUILD.gn @@ -0,0 +1,58 @@ +# Copyright (c) 2023 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/config/features.gni") +import("//build/test.gni") +import("../../distributedinput.gni") +import("//test/xts/tools/build/suite.gni") + +ohos_moduletest_suite("dinputfunctiontest") { + module_out_path = "distributed_input/functiontest/" + + include_dirs = [ + "${distributedinput_path}/interfaces/ipc/include", + "${utils_path}/include", + "../functiontest", + ] + + sources = [ + "dinput_automat_test.cpp", + "dinput_demo.cpp" + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DinputFunctionTest\"", + "LOG_DOMAIN=0xD004100", + ] + + deps = [ + "${utils_path}:libdinput_utils", + "${innerkits_path}:libdinput_sdk", + "//third_party/libevdev:libevdev", + "//third_party/jsoncpp:jsoncpp", + "//third_party/openssl:libcrypto_static", + ] + + external_deps = [ + "c_utils:utils", + "dsoftbus:softbus_client", + "ipc:ipc_core", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + ] + + subsystem_name = "distributedhardware" + part_name = "distributed_input" +} \ No newline at end of file diff --git a/test/functiontest/Test.json b/test/functiontest/Test.json new file mode 100644 index 0000000..679de30 --- /dev/null +++ b/test/functiontest/Test.json @@ -0,0 +1,21 @@ +{ + "description": "Config for disInput test cases", + "driver": { + "module-name": "DctsdisInputTest", + "native-test-timeout": "120000", + "native-test-device-path": "/data/local/tmp", + "runtime-hint": "1s", + "type": "CppTest" + }, + "kits": [ + { + "post-push" : [ + "chmod -R 777 /data/local/tmp/*" + ], + "push": [ + "DctsdisInputTest->/data/local/tmp/DctsdisInputTest" + ], + "type": "PushKit" + } + ] +} \ No newline at end of file diff --git a/test/functiontest/dinput_automat_test.cpp b/test/functiontest/dinput_automat_test.cpp new file mode 100644 index 0000000..9aa45d6 --- /dev/null +++ b/test/functiontest/dinput_automat_test.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2023 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 "dinput_demo.h" + +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "softbus_bus_center.h" +#include "softbus_common.h" + +using namespace testing::ext; +using namespace OHOS::DistributedHardware; +using namespace OHOS::Security::AccessToken; + +static constexpr int32_t RUN_TIMES = 50; + +class DInputAutomatTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + DInputAutomatTest(); +}; +void DInputAutomatTest::SetUpTestCase(void) +{ + const char *perms[2]; + perms[0] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER"; + perms[1] = "ohos.permission.DISTRIBUTED_DATASYNC"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = NULL, + .perms = perms, + .acls = NULL, + .processName = "dinput_function_test_service", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + sleep(SLEEP_OK); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +void DInputAutomatTest::TearDownTestCase(void) {} +void DInputAutomatTest::SetUp(void) {} +void DInputAutomatTest::TearDown(void) {} + +DInputAutomatTest::DInputAutomatTest(void) {} + +HWTEST_F(DInputAutomatTest, DInputTest_0001, TestSize.Level1) +{ + int32_t ret = PrepareRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0002, TestSize.Level1) +{ + int32_t ret = StartRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0003, TestSize.Level1) +{ + int32_t ret = StopRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0004, TestSize.Level1) +{ + for (int32_t i = 0; i < RUN_TIMES; i++) { + int32_t ret = StartRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); + ret = StopRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); + } +} + +HWTEST_F(DInputAutomatTest, DInputTest_0005, TestSize.Level1) +{ + int32_t ret = UnprepareRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} \ No newline at end of file diff --git a/test/functiontest/dinput_demo.cpp b/test/functiontest/dinput_demo.cpp new file mode 100644 index 0000000..b8b4b73 --- /dev/null +++ b/test/functiontest/dinput_demo.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2023 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 "dinput_demo.h" + +#include + +#include "distributed_input_client.h" +#include "dinput_log.h" +#include "i_prepare_d_input_call_back.h" + +#include "softbus_bus_center.h" +#include "softbus_common.h" + +using namespace OHOS::DistributedHardware; + +namespace { + const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; + std::string g_srcId; + std::string g_sinkId; + uint32_t input_type_all = 0x0007; +} + +void TestPrepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) +{ + (void)deviceId; + (void)status; +} + +void TestUnprepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) +{ + (void)deviceId; + (void)status; +} + +void TestStartDInputCallback::OnResult(const std::string& deviceId, const uint32_t& inputTypes, const int32_t& status) +{ + (void)deviceId; + (void)inputTypes; + (void)status; +} + +void TestStopDInputCallback::OnResult(const std::string& deviceId, const uint32_t& inputTypes, const int32_t& status) +{ + (void)deviceId; + (void)inputTypes; + (void)status; +} + +int32_t GetSrcDevId() +{ + std::cout << "Start get local device id." << std::endl; + auto info = std::make_unique(); + auto ret = GetLocalNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), info.get()); + if (ret != DINPUT_OK) { + std::cout << "GetLocalNodeDeviceInfo is fail." << "errCode = " << ret << std::endl; + return DINPUT_FAIL; + } + g_srcId = info->networkId; + if (g_srcId.empty()) { + std::cout << "g_srcId is empty." << std::endl; + return DINPUT_FAIL; + } + std::cout << "g_srcId = " << g_srcId << std::endl; + return DINPUT_OK; +} + +int32_t GetSinkDevId() +{ + sleep(SLEEP_OK); + std::cout << "Start get remote device id." << std::endl; + NodeBasicInfo *info = NULL; + int32_t infoNum = 0; + auto ret = GetAllNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), &info, &infoNum); + if (ret != DINPUT_OK) { + std::cout << "get remote device id fail." << std::endl; + return DINPUT_FAIL; + } + g_sinkId = info->networkId; + if (g_sinkId.empty()) { + std::cout << "g_sinkId is empty." << std::endl; + return DINPUT_FAIL; + } + std::cout << "g_sinkId = " << g_sinkId << std::endl; + return DINPUT_OK; +} + +int32_t PrepareRemote() +{ + if ((GetSrcDevId() != DINPUT_OK) || (GetSinkDevId() != DINPUT_OK)) { + std::cout << "get sink or src id failed." << std::endl; + return DINPUT_FAIL; + } + auto prepareCb = new(std::nothrow) TestPrepareDInputCallback(); + int32_t ret = OHOS::DistributedHardware::DistributedInput::DistributedInputKit::PrepareRemoteInput(g_srcId, g_sinkId, prepareCb); + if (ret != DINPUT_OK) { + std::cout << "PrepareRemoteInput is failed." << std::endl; + return ret; + } + std::cout << "PrepareRemoteInput is success." << std::endl; + return ret; +} + +int32_t UnprepareRemote() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto unprepareCb = new(std::nothrow) TestUnprepareDInputCallback(); + int32_t ret = OHOS::DistributedHardware::DistributedInput::DistributedInputKit::UnprepareRemoteInput(g_srcId, g_sinkId, unprepareCb); + if (ret != DINPUT_OK) { + std::cout << "UnprepareRemoteInput is failed." << std::endl; + return ret; + } + std::cout << "UnprepareRemoteInput is success." << std::endl; + return ret; +} + +int32_t StartRemote() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto startCb = new(std::nothrow) TestStartDInputCallback(); + int32_t ret = OHOS::DistributedHardware::DistributedInput::DistributedInputKit::StartRemoteInput(g_sinkId, input_type_all, startCb); + if (ret != DINPUT_OK) { + std::cout << "StartRemoteInput is failed." << std::endl; + return ret; + } + std::cout << "StartRemoteInput is success." << std::endl; + return ret; +} + +int32_t StopRemote() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto stopCb = new(std::nothrow) TestStopDInputCallback(); + int32_t ret = OHOS::DistributedHardware::DistributedInput::DistributedInputKit::StopRemoteInput(g_sinkId, input_type_all, stopCb); + if (ret != DINPUT_OK) { + std::cout << "StopRemoteInput is failed." << std::endl; + return ret; + } + std::cout << "StopRemoteInput is success." << std::endl; + return ret; +} \ No newline at end of file diff --git a/test/functiontest/dinput_demo.h b/test/functiontest/dinput_demo.h new file mode 100644 index 0000000..8c8b338 --- /dev/null +++ b/test/functiontest/dinput_demo.h @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2023 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 DINPUT_DEMO_H +#define DINPUT_DEMO_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "distributed_input_kit.h" +#include "prepare_d_input_call_back_stub.h" +#include "unprepare_d_input_call_back_stub.h" +#include "start_d_input_call_back_stub.h" +#include "stop_d_input_call_back_stub.h" + +namespace { + constexpr int32_t DINPUT_OK = 0; + constexpr int32_t DINPUT_FAIL = -1; + constexpr int32_t SLEEP_OK = 1; +} + +int32_t GetSrcDevId(); +int32_t GetSinkDevId(); +std::string StringPrintf(const char* format, ...); +int32_t PrepareRemote(); +int32_t UnprepareRemote(); +int32_t StartRemote(); +int32_t StopRemote(); + +class TestPrepareDInputCallback : + public OHOS::DistributedHardware::DistributedInput::PrepareDInputCallbackStub { +public: + TestPrepareDInputCallback() = default; + virtual ~TestPrepareDInputCallback() = default; + void OnResult(const std::string& deviceId, const int32_t& status); +}; + +class TestUnprepareDInputCallback : + public OHOS::DistributedHardware::DistributedInput::UnprepareDInputCallbackStub { +public: + TestUnprepareDInputCallback() = default; + virtual ~TestUnprepareDInputCallback() = default; + void OnResult(const std::string& deviceId, const int32_t& status); +}; + +class TestStartDInputCallback : + public OHOS::DistributedHardware::DistributedInput::StartDInputCallbackStub { +public: + TestStartDInputCallback() = default; + virtual ~TestStartDInputCallback() = default; + void OnResult(const std::string& deviceId, const uint32_t& inputTypes, const int32_t& status); +}; + +class TestStopDInputCallback : + public OHOS::DistributedHardware::DistributedInput::StopDInputCallbackStub { +public: + TestStopDInputCallback() = default; + virtual ~TestStopDInputCallback() = default; + void OnResult(const std::string& deviceId, const uint32_t& inputTypes, const int32_t& status); +}; +#define CooDevMgr ::OHOS::DelayedSingleton::GetInstance() +#endif // DINPUT_DEMO_H \ No newline at end of file -- Gitee From 45d01fdce05fb84df9867c00b91092a743f2e5f8 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Fri, 14 Jul 2023 14:48:36 +0800 Subject: [PATCH 2/6] add dinput function test Signed-off-by: wangxuanxuan --- test/functiontest/BUILD.gn | 24 ++++++++++++------------ test/functiontest/dinput_demo.cpp | 8 ++++---- test/functiontest/dinput_demo.h | 27 ++++++++++++--------------- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/test/functiontest/BUILD.gn b/test/functiontest/BUILD.gn index 08d3026..247b2ee 100644 --- a/test/functiontest/BUILD.gn +++ b/test/functiontest/BUILD.gn @@ -13,12 +13,12 @@ import("//build/config/features.gni") import("//build/test.gni") -import("../../distributedinput.gni") import("//test/xts/tools/build/suite.gni") +import("../../distributedinput.gni") ohos_moduletest_suite("dinputfunctiontest") { module_out_path = "distributed_input/functiontest/" - + include_dirs = [ "${distributedinput_path}/interfaces/ipc/include", "${utils_path}/include", @@ -27,7 +27,7 @@ ohos_moduletest_suite("dinputfunctiontest") { sources = [ "dinput_automat_test.cpp", - "dinput_demo.cpp" + "dinput_demo.cpp", ] defines = [ @@ -35,24 +35,24 @@ ohos_moduletest_suite("dinputfunctiontest") { "DH_LOG_TAG=\"DinputFunctionTest\"", "LOG_DOMAIN=0xD004100", ] - + deps = [ - "${utils_path}:libdinput_utils", "${innerkits_path}:libdinput_sdk", - "//third_party/libevdev:libevdev", + "${utils_path}:libdinput_utils", "//third_party/jsoncpp:jsoncpp", + "//third_party/libevdev:libevdev", "//third_party/openssl:libcrypto_static", ] - + external_deps = [ - "c_utils:utils", - "dsoftbus:softbus_client", - "ipc:ipc_core", "access_token:libaccesstoken_sdk", "access_token:libnativetoken", "access_token:libtoken_setproc", + "c_utils:utils", + "dsoftbus:softbus_client", + "ipc:ipc_core", ] - + subsystem_name = "distributedhardware" part_name = "distributed_input" -} \ No newline at end of file +} diff --git a/test/functiontest/dinput_demo.cpp b/test/functiontest/dinput_demo.cpp index b8b4b73..4ed11a1 100644 --- a/test/functiontest/dinput_demo.cpp +++ b/test/functiontest/dinput_demo.cpp @@ -104,7 +104,7 @@ int32_t PrepareRemote() return DINPUT_FAIL; } auto prepareCb = new(std::nothrow) TestPrepareDInputCallback(); - int32_t ret = OHOS::DistributedHardware::DistributedInput::DistributedInputKit::PrepareRemoteInput(g_srcId, g_sinkId, prepareCb); + int32_t ret = DistributedInput::DistributedInputKit::PrepareRemoteInput(g_srcId, g_sinkId, prepareCb); if (ret != DINPUT_OK) { std::cout << "PrepareRemoteInput is failed." << std::endl; return ret; @@ -120,7 +120,7 @@ int32_t UnprepareRemote() return DINPUT_FAIL; } auto unprepareCb = new(std::nothrow) TestUnprepareDInputCallback(); - int32_t ret = OHOS::DistributedHardware::DistributedInput::DistributedInputKit::UnprepareRemoteInput(g_srcId, g_sinkId, unprepareCb); + int32_t ret = DistributedInput::DistributedInputKit::UnprepareRemoteInput(g_srcId, g_sinkId, unprepareCb); if (ret != DINPUT_OK) { std::cout << "UnprepareRemoteInput is failed." << std::endl; return ret; @@ -136,7 +136,7 @@ int32_t StartRemote() return DINPUT_FAIL; } auto startCb = new(std::nothrow) TestStartDInputCallback(); - int32_t ret = OHOS::DistributedHardware::DistributedInput::DistributedInputKit::StartRemoteInput(g_sinkId, input_type_all, startCb); + int32_t ret = DistributedInput::DistributedInputKit::StartRemoteInput(g_sinkId, input_type_all, startCb); if (ret != DINPUT_OK) { std::cout << "StartRemoteInput is failed." << std::endl; return ret; @@ -152,7 +152,7 @@ int32_t StopRemote() return DINPUT_FAIL; } auto stopCb = new(std::nothrow) TestStopDInputCallback(); - int32_t ret = OHOS::DistributedHardware::DistributedInput::DistributedInputKit::StopRemoteInput(g_sinkId, input_type_all, stopCb); + int32_t ret = DistributedInput::DistributedInputKit::StopRemoteInput(g_sinkId, input_type_all, stopCb); if (ret != DINPUT_OK) { std::cout << "StopRemoteInput is failed." << std::endl; return ret; diff --git a/test/functiontest/dinput_demo.h b/test/functiontest/dinput_demo.h index 8c8b338..46df18b 100644 --- a/test/functiontest/dinput_demo.h +++ b/test/functiontest/dinput_demo.h @@ -25,17 +25,17 @@ #include #include -#include -#include -#include #include #include #include #include #include +#include +#include #include #include #include +#include #include #include #include @@ -47,19 +47,16 @@ #include "start_d_input_call_back_stub.h" #include "stop_d_input_call_back_stub.h" -namespace { - constexpr int32_t DINPUT_OK = 0; - constexpr int32_t DINPUT_FAIL = -1; - constexpr int32_t SLEEP_OK = 1; -} +constexpr int32_t DINPUT_OK = 0; +constexpr int32_t DINPUT_FAIL = -1; +constexpr int32_t SLEEP_OK = 1; -int32_t GetSrcDevId(); -int32_t GetSinkDevId(); -std::string StringPrintf(const char* format, ...); -int32_t PrepareRemote(); -int32_t UnprepareRemote(); -int32_t StartRemote(); -int32_t StopRemote(); +int32_t GetSrcDevId(void); +int32_t GetSinkDevId(void); +int32_t PrepareRemote(void); +int32_t UnprepareRemote(void); +int32_t StartRemote(void); +int32_t StopRemote(void); class TestPrepareDInputCallback : public OHOS::DistributedHardware::DistributedInput::PrepareDInputCallbackStub { -- Gitee From 7910e2dfacd743292e71076842c2e6232c6f8b37 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Fri, 14 Jul 2023 15:49:37 +0800 Subject: [PATCH 3/6] add dinput function test Signed-off-by: wangxuanxuan --- test/functiontest/dinput_automat_test.cpp | 5 +++-- test/functiontest/dinput_demo.cpp | 2 +- test/functiontest/dinput_demo.h | 25 +++++++++++------------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/test/functiontest/dinput_automat_test.cpp b/test/functiontest/dinput_automat_test.cpp index 9aa45d6..6f4f59f 100644 --- a/test/functiontest/dinput_automat_test.cpp +++ b/test/functiontest/dinput_automat_test.cpp @@ -27,6 +27,7 @@ using namespace OHOS::DistributedHardware; using namespace OHOS::Security::AccessToken; static constexpr int32_t RUN_TIMES = 50; +static constexpr int32_t PERMISSION_NUM = 2; class DInputAutomatTest : public testing::Test { public: @@ -38,12 +39,12 @@ public: }; void DInputAutomatTest::SetUpTestCase(void) { - const char *perms[2]; + const char *perms[PERMISSION_NUM]; perms[0] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER"; perms[1] = "ohos.permission.DISTRIBUTED_DATASYNC"; NativeTokenInfoParams infoInstance = { .dcapsNum = 0, - .permsNum = 2, + .permsNum = PERMISSION_NUM, .aclsNum = 0, .dcaps = NULL, .perms = perms, diff --git a/test/functiontest/dinput_demo.cpp b/test/functiontest/dinput_demo.cpp index 4ed11a1..1f80554 100644 --- a/test/functiontest/dinput_demo.cpp +++ b/test/functiontest/dinput_demo.cpp @@ -17,8 +17,8 @@ #include -#include "distributed_input_client.h" #include "dinput_log.h" +#include "distributed_input_client.h" #include "i_prepare_d_input_call_back.h" #include "softbus_bus_center.h" diff --git a/test/functiontest/dinput_demo.h b/test/functiontest/dinput_demo.h index 46df18b..594556a 100644 --- a/test/functiontest/dinput_demo.h +++ b/test/functiontest/dinput_demo.h @@ -17,35 +17,34 @@ #define DINPUT_DEMO_H #include -#include -#include -#include -#include -#include -#include -#include - #include #include +#include #include #include #include -#include -#include +#include +#include #include #include #include +#include +#include +#include +#include + +#include +#include #include #include +#include #include -#include -#include #include "distributed_input_kit.h" #include "prepare_d_input_call_back_stub.h" -#include "unprepare_d_input_call_back_stub.h" #include "start_d_input_call_back_stub.h" #include "stop_d_input_call_back_stub.h" +#include "unprepare_d_input_call_back_stub.h" constexpr int32_t DINPUT_OK = 0; constexpr int32_t DINPUT_FAIL = -1; -- Gitee From 24907a6e7857cabe3ad7fada4e06f5820f4dfa53 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Thu, 27 Jul 2023 14:22:59 +0800 Subject: [PATCH 4/6] add dinput function test Signed-off-by: wangxuanxuan --- test/functiontest/dinput_automat_test.cpp | 8 ++++++-- test/functiontest/dinput_demo.cpp | 9 ++++++--- test/functiontest/dinput_demo.h | 6 +----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/test/functiontest/dinput_automat_test.cpp b/test/functiontest/dinput_automat_test.cpp index 6f4f59f..0afb4e5 100644 --- a/test/functiontest/dinput_automat_test.cpp +++ b/test/functiontest/dinput_automat_test.cpp @@ -26,8 +26,12 @@ using namespace testing::ext; using namespace OHOS::DistributedHardware; using namespace OHOS::Security::AccessToken; -static constexpr int32_t RUN_TIMES = 50; -static constexpr int32_t PERMISSION_NUM = 2; +namespace { + constexpr int32_t RUN_TIMES = 50; + constexpr int32_t PERMISSION_NUM = 2; + constexpr int32_t SLEEP_OK = 1; + constexpr int32_t DINPUT_OK = 0; +} class DInputAutomatTest : public testing::Test { public: diff --git a/test/functiontest/dinput_demo.cpp b/test/functiontest/dinput_demo.cpp index 1f80554..e71fa5e 100644 --- a/test/functiontest/dinput_demo.cpp +++ b/test/functiontest/dinput_demo.cpp @@ -28,9 +28,12 @@ using namespace OHOS::DistributedHardware; namespace { const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; + constexpr int32_t DINPUT_OK = 0; + constexpr int32_t DINPUT_FAIL = -1; + constexpr int32_t SLEEP_OK = 1; + constexpr uint32_t INPUT_TYPE_ALL = 0x0007; std::string g_srcId; std::string g_sinkId; - uint32_t input_type_all = 0x0007; } void TestPrepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) @@ -136,7 +139,7 @@ int32_t StartRemote() return DINPUT_FAIL; } auto startCb = new(std::nothrow) TestStartDInputCallback(); - int32_t ret = DistributedInput::DistributedInputKit::StartRemoteInput(g_sinkId, input_type_all, startCb); + int32_t ret = DistributedInput::DistributedInputKit::StartRemoteInput(g_sinkId, INPUT_TYPE_ALL, startCb); if (ret != DINPUT_OK) { std::cout << "StartRemoteInput is failed." << std::endl; return ret; @@ -152,7 +155,7 @@ int32_t StopRemote() return DINPUT_FAIL; } auto stopCb = new(std::nothrow) TestStopDInputCallback(); - int32_t ret = DistributedInput::DistributedInputKit::StopRemoteInput(g_sinkId, input_type_all, stopCb); + int32_t ret = DistributedInput::DistributedInputKit::StopRemoteInput(g_sinkId, INPUT_TYPE_ALL, stopCb); if (ret != DINPUT_OK) { std::cout << "StopRemoteInput is failed." << std::endl; return ret; diff --git a/test/functiontest/dinput_demo.h b/test/functiontest/dinput_demo.h index 594556a..3faa193 100644 --- a/test/functiontest/dinput_demo.h +++ b/test/functiontest/dinput_demo.h @@ -34,11 +34,11 @@ #include #include -#include #include #include #include #include +#include #include "distributed_input_kit.h" #include "prepare_d_input_call_back_stub.h" @@ -46,10 +46,6 @@ #include "stop_d_input_call_back_stub.h" #include "unprepare_d_input_call_back_stub.h" -constexpr int32_t DINPUT_OK = 0; -constexpr int32_t DINPUT_FAIL = -1; -constexpr int32_t SLEEP_OK = 1; - int32_t GetSrcDevId(void); int32_t GetSinkDevId(void); int32_t PrepareRemote(void); -- Gitee From f6e67f8b86b17235afd3b3ea78cb63de27250d44 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Tue, 1 Aug 2023 17:47:51 +0800 Subject: [PATCH 5/6] add dinput function test Signed-off-by: wangxuanxuan --- test/functiontest/dinput_automat_test.cpp | 26 ++++++++ test/functiontest/dinput_demo.cpp | 79 ++++++++++++++++++++++- test/functiontest/dinput_demo.h | 21 ++++++ 3 files changed, 124 insertions(+), 2 deletions(-) diff --git a/test/functiontest/dinput_automat_test.cpp b/test/functiontest/dinput_automat_test.cpp index 0afb4e5..1ce28ab 100644 --- a/test/functiontest/dinput_automat_test.cpp +++ b/test/functiontest/dinput_automat_test.cpp @@ -102,6 +102,32 @@ HWTEST_F(DInputAutomatTest, DInputTest_0004, TestSize.Level1) } HWTEST_F(DInputAutomatTest, DInputTest_0005, TestSize.Level1) +{ + int32_t ret = StartRemoteByDhIds(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0006, TestSize.Level1) +{ + int32_t ret = StopRemoteByDhIds(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0007, TestSize.Level1) +{ + for (int32_t i = 0; i < RUN_TIMES; i++) { + int32_t ret = StartRemoteByDhIds(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); + ret = StopRemoteByDhIds(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); + } +} + +HWTEST_F(DInputAutomatTest, DInputTest_0008, TestSize.Level1) { int32_t ret = UnprepareRemote(); EXPECT_EQ(DINPUT_OK, ret); diff --git a/test/functiontest/dinput_demo.cpp b/test/functiontest/dinput_demo.cpp index e71fa5e..948b147 100644 --- a/test/functiontest/dinput_demo.cpp +++ b/test/functiontest/dinput_demo.cpp @@ -15,9 +15,12 @@ #include "dinput_demo.h" +#include #include +#include #include "dinput_log.h" +#include "dinput_utils_tool.h" #include "distributed_input_client.h" #include "i_prepare_d_input_call_back.h" @@ -34,6 +37,8 @@ namespace { constexpr uint32_t INPUT_TYPE_ALL = 0x0007; std::string g_srcId; std::string g_sinkId; + std::vector g_dhIds; + std::mutex g_dhIdsMutex; } void TestPrepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) @@ -62,6 +67,36 @@ void TestStopDInputCallback::OnResult(const std::string& deviceId, const uint32_ (void)status; } +void TestInputNodeListener::OnNodeOnLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId, + const std::string nodeDesc) +{ + (void)srcDevId; + (void)sinkDevId; + (void)nodeDesc; + std::lock_guard lock(g_dhIdsMutex); + g_dhIds.emplace_back(sinkNodeId); +} + +void TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId) +{ + (void)srcDevId; + (void)sinkDevId; + std::lock_guard lock(g_dhIdsMutex); + for (auto it = g_dhIds.begin(); it != g_dhIds.end();) { + if (*it == sinkNodeId) { + it = g_dhIds.erase(it); + continue; + } + ++it; + } +} + +void TestStartStopDInputCallback::OnResultDhids(const std::string &devId, const int32_t &status) +{ + (void)devId; + (void)status; +} + int32_t GetSrcDevId() { std::cout << "Start get local device id." << std::endl; @@ -76,7 +111,7 @@ int32_t GetSrcDevId() std::cout << "g_srcId is empty." << std::endl; return DINPUT_FAIL; } - std::cout << "g_srcId = " << g_srcId << std::endl; + std::cout << "g_srcId = " << DistributedInput::GetAnonyString(g_srcId) << std::endl; return DINPUT_OK; } @@ -96,7 +131,7 @@ int32_t GetSinkDevId() std::cout << "g_sinkId is empty." << std::endl; return DINPUT_FAIL; } - std::cout << "g_sinkId = " << g_sinkId << std::endl; + std::cout << "g_sinkId = " << DistributedInput::GetAnonyString(g_sinkId) << std::endl; return DINPUT_OK; } @@ -113,6 +148,14 @@ int32_t PrepareRemote() return ret; } std::cout << "PrepareRemoteInput is success." << std::endl; + + OHOS::sptr inputNodeListener(new TestInputNodeListener()); + ret = DistributedInput::DistributedInputKit::RegisterInputNodeListener(inputNodeListener); + if (ret != DINPUT_OK) { + std::cout << "Register Input Node Listener is failed." << std::endl; + return ret; + } + std::cout << "Register Input Node Listener success." << std::endl; return ret; } @@ -162,4 +205,36 @@ int32_t StopRemote() } std::cout << "StopRemoteInput is success." << std::endl; return ret; +} + +int32_t StartRemoteByDhIds() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto startCb = new(std::nothrow) TestStartStopDInputCallback(); + int32_t ret = DistributedInput::DistributedInputKit::StartRemoteInput(g_sinkId, g_dhIds, startCb); + if (ret != DINPUT_OK) { + std::cout << "StartRemoteByDhIds is failed." << std::endl; + return ret; + } + std::cout << "StartRemoteByDhIds is success." << std::endl; + return ret; +} + +int32_t StopRemoteByDhIds() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto stopCb = new(std::nothrow) TestStartStopDInputCallback(); + int32_t ret = DistributedInput::DistributedInputKit::StopRemoteInput(g_sinkId, g_dhIds, stopCb); + if (ret != DINPUT_OK) { + std::cout << "StopRemoteByDhIds is failed." << std::endl; + return ret; + } + std::cout << "StopRemoteByDhIds is success." << std::endl; + return ret; } \ No newline at end of file diff --git a/test/functiontest/dinput_demo.h b/test/functiontest/dinput_demo.h index 3faa193..11d86d0 100644 --- a/test/functiontest/dinput_demo.h +++ b/test/functiontest/dinput_demo.h @@ -41,6 +41,7 @@ #include #include "distributed_input_kit.h" +#include "input_node_listener_stub.h" #include "prepare_d_input_call_back_stub.h" #include "start_d_input_call_back_stub.h" #include "stop_d_input_call_back_stub.h" @@ -52,6 +53,8 @@ int32_t PrepareRemote(void); int32_t UnprepareRemote(void); int32_t StartRemote(void); int32_t StopRemote(void); +int32_t StartRemoteByDhIds(void); +int32_t StopRemoteByDhIds(void); class TestPrepareDInputCallback : public OHOS::DistributedHardware::DistributedInput::PrepareDInputCallbackStub { @@ -84,5 +87,23 @@ public: virtual ~TestStopDInputCallback() = default; void OnResult(const std::string& deviceId, const uint32_t& inputTypes, const int32_t& status); }; + +class TestInputNodeListener : + public OHOS::DistributedHardware::DistributedInput::InputNodeListenerStub { +public: + TestInputNodeListener() = default; + virtual ~TestInputNodeListener() = default; + void OnNodeOnLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId, + const std::string nodeDesc); + void OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId); +}; + +class TestStartStopDInputCallback : + public OHOS::DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { +public: + TestStartStopDInputCallback() = default; + virtual ~TestStartStopDInputCallback() = default; + void OnResultDhids(const std::string &devId, const int32_t &status); +}; #define CooDevMgr ::OHOS::DelayedSingleton::GetInstance() #endif // DINPUT_DEMO_H \ No newline at end of file -- Gitee From c64a178a400ac52d90e814a11e5477301419df59 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Tue, 1 Aug 2023 19:08:17 +0800 Subject: [PATCH 6/6] add dinput function test Signed-off-by: wangxuanxuan --- test/functiontest/dinput_demo.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/functiontest/dinput_demo.cpp b/test/functiontest/dinput_demo.cpp index 948b147..2d0fdee 100644 --- a/test/functiontest/dinput_demo.cpp +++ b/test/functiontest/dinput_demo.cpp @@ -67,8 +67,8 @@ void TestStopDInputCallback::OnResult(const std::string& deviceId, const uint32_ (void)status; } -void TestInputNodeListener::OnNodeOnLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId, - const std::string nodeDesc) +void TestInputNodeListener::OnNodeOnLine(const std::string srcDevId, const std::string sinkDevId, + const std::string sinkNodeId, const std::string nodeDesc) { (void)srcDevId; (void)sinkDevId; @@ -77,7 +77,8 @@ void TestInputNodeListener::OnNodeOnLine(const std::string srcDevId, const std:: g_dhIds.emplace_back(sinkNodeId); } -void TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId) +void TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, + const std::string sinkNodeId) { (void)srcDevId; (void)sinkDevId; -- Gitee