From b25bc487885ee8a57a390af209c45e6c3e321f24 Mon Sep 17 00:00:00 2001 From: yuanbo Date: Mon, 25 Apr 2022 15:29:53 +0800 Subject: [PATCH] feat: support interface object cast to remote object Signed-off-by: yuanbo --- uhdf2/hdi/test/BUILD.gn | 1 + .../hdi_sample/sample_client_cpp/BUILD.gn | 37 +++++++++++++++++++ .../sample_client_cpp_test.cpp | 33 +++++++++++++++-- .../sample_client_cpp/unittest/BUILD.gn | 17 +++------ uhdf2/ipc/include/iproxy_broker.h | 9 ++++- 5 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 uhdf2/hdi/test/hdi_sample/sample_client_cpp/BUILD.gn diff --git a/uhdf2/hdi/test/BUILD.gn b/uhdf2/hdi/test/BUILD.gn index 65aac55..2ff57be 100644 --- a/uhdf2/hdi/test/BUILD.gn +++ b/uhdf2/hdi/test/BUILD.gn @@ -65,6 +65,7 @@ group("unittest") { deps = [ ":HdiServiceManagerTest", ":HdiServiceManagerTestCC", + "hdi_sample/sample_client_cpp:HdiSampleTestCC", "hdi_sample/sample_client_cpp/unittest:sample_client_cpp", "hdi_sample/sample_service_cpp:sample_service_cpp", ] diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/BUILD.gn b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/BUILD.gn new file mode 100644 index 0000000..ba35a97 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/BUILD.gn @@ -0,0 +1,37 @@ +# 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. + +import("//build/test.gni") +import("//drivers/adapter/uhdf2/uhdf.gni") + +module_output_path = "hdf/hdi" + +ohos_unittest("HdiSampleTestCC") { + module_out_path = module_output_path + sources = [ "sample_client_cpp_test.cpp" ] + + deps = [ + "$hdf_uhdf_path/hdi:libhdi", + "$hdf_uhdf_path/utils:libhdf_utils", + "unittest:libsample_client_cpp", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "samgr_standard:samgr_proxy", + "utils_base:utils", + ] +} diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/sample_client_cpp_test.cpp b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/sample_client_cpp_test.cpp index 076c1b9..06d01fa 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/sample_client_cpp_test.cpp +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/sample_client_cpp_test.cpp @@ -14,6 +14,9 @@ */ #include #include +#include +#include +#include #include #include #include @@ -24,12 +27,16 @@ using namespace testing::ext; using namespace OHOS::HDI::Sample::V1_0; +using OHOS::IRemoteObject; +using OHOS::sptr; +using OHOS::wptr; +using OHOS::HDI::DeviceManager::V1_0::IDeviceManager; #define HDF_LOG_TAG sample_client_cpp_test constexpr const char *TEST_SERVICE_NAME = "sample_driver_service"; -class SampleObjCPPTest : public testing::Test { +class SampleHdiCppTest : public testing::Test { public: static void SetUpTestCase() { @@ -50,7 +57,7 @@ public: }; // IPC mode get interface object -HWTEST_F(SampleObjCPPTest, ServMgrTest100, TestSize.Level1) +HWTEST_F(SampleHdiCppTest, HdiCppTest001, TestSize.Level1) { OHOS::sptr sampleService = ISample::Get(TEST_SERVICE_NAME, false); ASSERT_TRUE(sampleService != nullptr); @@ -65,7 +72,7 @@ HWTEST_F(SampleObjCPPTest, ServMgrTest100, TestSize.Level1) } // passthrough mode get interface object -HWTEST_F(SampleObjCPPTest, ServMgrTest101, TestSize.Level1) +HWTEST_F(SampleHdiCppTest, HdiCppTest002, TestSize.Level1) { OHOS::sptr sampleService = ISample::Get(TEST_SERVICE_NAME, true); ASSERT_TRUE(sampleService != nullptr); @@ -78,3 +85,23 @@ HWTEST_F(SampleObjCPPTest, ServMgrTest101, TestSize.Level1) ASSERT_EQ(ret, 0); ASSERT_EQ(value, true); } + +class SampleDeathRecipient : public IRemoteObject::DeathRecipient { +public: + void OnRemoteDied(const wptr &object) override + { + HDF_LOGI("sample service dead"); + } +}; + +// IPC mode add DeathRecipient +HWTEST_F(SampleHdiCppTest, HdiCppTest003, TestSize.Level1) +{ + sptr sampleService = ISample::Get(TEST_SERVICE_NAME, false); + ASSERT_TRUE(sampleService != nullptr); + + const sptr recipient = new SampleDeathRecipient(); + sptr remote = OHOS::HDI::hdi_objcast(sampleService); + bool ret = remote->AddDeathRecipient(recipient); + ASSERT_EQ(ret, true); +} diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn index 17ff575..9d5b49b 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn @@ -43,18 +43,11 @@ ohos_shared_library("libsample_client_cpp") { "//drivers/adapter/uhdf2/utils:libhdf_utils", ] - if (is_standard_system) { - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_single", - "utils_base:utils", - ] - } else { - external_deps = [ - "hilog:libhilog", - "ipc:ipc_single", - ] - } + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "utils_base:utils", + ] install_images = [ chipset_base_dir ] subsystem_name = "hdf" diff --git a/uhdf2/ipc/include/iproxy_broker.h b/uhdf2/ipc/include/iproxy_broker.h index 7b6e0c3..182d32d 100644 --- a/uhdf2/ipc/include/iproxy_broker.h +++ b/uhdf2/ipc/include/iproxy_broker.h @@ -28,8 +28,6 @@ public: explicit IProxyBroker(const sptr &object); virtual ~IProxyBroker() = default; virtual sptr AsInterface(); - -protected: sptr AsObject() override; }; @@ -59,6 +57,13 @@ inline sptr hdi_facecast(const sptr &object) INTERFACE *proxyBroker = static_cast *>(broker.GetRefPtr()); return static_cast(proxyBroker); } + +template +inline sptr hdi_objcast(const sptr &iface) +{ + IProxyBroker *brokerObject = static_cast *>(iface.GetRefPtr()); + return brokerObject->AsObject(); +} } // namespace HDI } // namespace OHOS