mirror of
https://github.com/openharmony/drivers_adapter.git
synced 2026-07-19 18:23:38 -04:00
!550 feat: support interface object cast to remote object
Merge pull request !550 from yuanbo/master
This commit is contained in:
@@ -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",
|
||||
]
|
||||
|
||||
@@ -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",
|
||||
]
|
||||
}
|
||||
@@ -14,6 +14,9 @@
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <hdf_log.h>
|
||||
#include <idevmgr_hdi.h>
|
||||
#include <iproxy_broker.h>
|
||||
#include <iremote_object.h>
|
||||
#include <map>
|
||||
#include <osal_mem.h>
|
||||
#include <thread>
|
||||
@@ -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<ISample> 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<ISample> 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<IRemoteObject> &object) override
|
||||
{
|
||||
HDF_LOGI("sample service dead");
|
||||
}
|
||||
};
|
||||
|
||||
// IPC mode add DeathRecipient
|
||||
HWTEST_F(SampleHdiCppTest, HdiCppTest003, TestSize.Level1)
|
||||
{
|
||||
sptr<ISample> sampleService = ISample::Get(TEST_SERVICE_NAME, false);
|
||||
ASSERT_TRUE(sampleService != nullptr);
|
||||
|
||||
const sptr<IRemoteObject::DeathRecipient> recipient = new SampleDeathRecipient();
|
||||
sptr<IRemoteObject> remote = OHOS::HDI::hdi_objcast<ISample>(sampleService);
|
||||
bool ret = remote->AddDeathRecipient(recipient);
|
||||
ASSERT_EQ(ret, true);
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -28,8 +28,6 @@ public:
|
||||
explicit IProxyBroker(const sptr<IRemoteObject> &object);
|
||||
virtual ~IProxyBroker() = default;
|
||||
virtual sptr<INTERFACE> AsInterface();
|
||||
|
||||
protected:
|
||||
sptr<IRemoteObject> AsObject() override;
|
||||
};
|
||||
|
||||
@@ -59,6 +57,13 @@ inline sptr<INTERFACE> hdi_facecast(const sptr<IRemoteObject> &object)
|
||||
INTERFACE *proxyBroker = static_cast<IProxyBroker<INTERFACE> *>(broker.GetRefPtr());
|
||||
return static_cast<INTERFACE *>(proxyBroker);
|
||||
}
|
||||
|
||||
template <typename INTERFACE>
|
||||
inline sptr<IRemoteObject> hdi_objcast(const sptr<INTERFACE> &iface)
|
||||
{
|
||||
IProxyBroker<INTERFACE> *brokerObject = static_cast<IProxyBroker<INTERFACE> *>(iface.GetRefPtr());
|
||||
return brokerObject->AsObject();
|
||||
}
|
||||
} // namespace HDI
|
||||
} // namespace OHOS
|
||||
|
||||
|
||||
Reference in New Issue
Block a user