diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 4ab411fb9a9d30ae63576eaff504169a56453f7d..c3d6c9758839953033747785174ce57b6950254a 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -28,6 +28,7 @@ group("unittest") { ":UTTest_device_manager_service_impl", ":UTTest_device_manager_service_impl_first", ":UTTest_device_manager_service_listener", + ":UTTest_device_manager_service_notify", ":UTTest_device_manager_service_three", ":UTTest_discovery_filter", ":UTTest_discovery_manager", @@ -420,6 +421,30 @@ ohos_unittest("UTTest_device_manager_service_three") { ## UnitTest UTTest_device_manager_service_three }}} +## UnitTest UTTest_device_manager_service_notify {{{ +ohos_unittest("UTTest_device_manager_service_notify") { + module_out_path = module_out_path + + sources = [ "UTTest_device_manager_service_notify.cpp" ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "cJSON:cjson", + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "dsoftbus:softbus_client", + "ffrt:libffrt", + "googletest:gmock", + ] +} + +## UnitTest UTTest_device_manager_service_notify }}} + ## UnitTest UTTest_hichain_auth_connector {{{ ohos_unittest("UTTest_hichain_auth_connector") { module_out_path = module_out_path @@ -894,6 +919,7 @@ ohos_unittest("UTTest_device_manager_impl") { "UTTest_device_manager_impl.cpp", "UTTest_device_manager_impl_three.cpp", "UTTest_device_manager_impl_two.cpp", + "mock/device_manager_notify_mock.cpp", ] deps = [ ":device_manager_test_common" ] @@ -906,6 +932,7 @@ ohos_unittest("UTTest_device_manager_impl") { "dsoftbus:softbus_client", "ffrt:libffrt", "googletest:gmock", + "googletest:gmock_main", ] cflags = [ diff --git a/test/unittest/UTTest_device_manager_impl.cpp b/test/unittest/UTTest_device_manager_impl.cpp index c846464ec9859163997373d02fb3af6abae90ca6..3ccd06d71a8928dc81aa8ea40f6042d98fc8c978 100644 --- a/test/unittest/UTTest_device_manager_impl.cpp +++ b/test/unittest/UTTest_device_manager_impl.cpp @@ -71,10 +71,13 @@ void DeviceManagerImplTest::TearDown() void DeviceManagerImplTest::SetUpTestCase() { + DmDeviceManagerNotify::dmDeviceManagerNotify = deviceManagerNotifyMock_; } void DeviceManagerImplTest::TearDownTestCase() { + DmDeviceManagerNotify::dmDeviceManagerNotify = nullptr; + deviceManagerNotifyMock_ = nullptr; } namespace { diff --git a/test/unittest/UTTest_device_manager_impl.h b/test/unittest/UTTest_device_manager_impl.h index bb1de8da259588fa180ca6a9e60ca465d189194a..92f1c661f96f35504f17ff98184f090ed8aba383 100644 --- a/test/unittest/UTTest_device_manager_impl.h +++ b/test/unittest/UTTest_device_manager_impl.h @@ -26,6 +26,7 @@ #include "dm_single_instance.h" #include "device_manager_impl.h" #include "softbus_error_code.h" +#include "device_manager_notify_mock.h" namespace OHOS { namespace DistributedHardware { @@ -35,6 +36,9 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); + + static inline std::shared_ptr deviceManagerNotifyMock_ = + std::make_shared(); }; class DeviceDiscoveryCallbackTest : public DiscoveryCallback { @@ -125,6 +129,22 @@ public: } void OnCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode) override {} }; + +class GetDeviceProfileInfoListCallbackTest : public GetDeviceProfileInfoListCallback { +public: + virtual ~GetDeviceProfileInfoListCallbackTest() + { + } + void OnResult(const std::vector &deviceProfileInfos, int32_t code) override {} +}; + +class GetDeviceIconInfoCallbackTest : public GetDeviceIconInfoCallback { +public: + virtual ~GetDeviceIconInfoCallbackTest() + { + } + void OnResult(const DmDeviceIconInfo &deviceIconInfo, int32_t code) override {} +}; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_impl_two.cpp b/test/unittest/UTTest_device_manager_impl_two.cpp index 36a75549f3fe404678e5f7a5307e3daac20b2a47..80b05c9423c5555e13e8a38c3ab3c7848758f2f9 100644 --- a/test/unittest/UTTest_device_manager_impl_two.cpp +++ b/test/unittest/UTTest_device_manager_impl_two.cpp @@ -16,6 +16,7 @@ #include "UTTest_device_manager_impl.h" #include "dm_device_info.h" +#include #include #include "accesstoken_kit.h" #include "device_manager_notify.h" @@ -31,13 +32,15 @@ #include "ipc_rsp.h" #include "ipc_set_useroperation_req.h" #include "ipc_skeleton.h" - #include "ipc_publish_req.h" +#include "ipc_publish_req.h" #include "ipc_unpublish_req.h" #include "ipc_unauthenticate_device_req.h" #include "nativetoken_kit.h" #include "securec.h" #include "token_setproc.h" +using namespace testing; +using namespace testing::ext; namespace OHOS { namespace DistributedHardware { namespace { @@ -1541,6 +1544,371 @@ HWTEST_F(DeviceManagerImplTest, UnRegisterDeviceManagerFaCallback_005, testing:: // 8. check checkMap not null ASSERT_NE(checkMap, nullptr); } + +HWTEST_F(DeviceManagerImplTest, RegisterDevStatusCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + std::string extra = "extra"; + std::shared_ptr callback = nullptr; + int32_t ret = DeviceManager::GetInstance().RegisterDevStatusCallback(pkgName, extra, callback); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, UnRegisterDevStatusCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int32_t ret = DeviceManager::GetInstance().UnRegisterDevStatusCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + ret = DeviceManager::GetInstance().UnRegisterDevStatusCallback(pkgName); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + uint64_t tokenId = 1; + std::string filterOptions = "filterOptions"; + std::shared_ptr callback = nullptr; + int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(pkgName, tokenId, filterOptions, callback); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, StopDeviceDiscovery_201, testing::ext::TestSize.Level0) +{ + uint64_t tokenId = 1; + std::string pkgName = ""; + int32_t ret = DeviceManager::GetInstance().StopDeviceDiscovery(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + ret = DeviceManager::GetInstance().StopDeviceDiscovery(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, StopAuthenticateDevice_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int32_t ret = DeviceManager::GetInstance().StopAuthenticateDevice(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + ret = DeviceManager::GetInstance().StopAuthenticateDevice(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, VerifyAuthentication_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::string authPara = "authPara"; + std::shared_ptr callback = nullptr; + int32_t ret = DeviceManager::GetInstance().VerifyAuthentication(pkgName); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, GetFaParam_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + DmAuthParam dmFaParam; + int32_t ret = DeviceManager::GetInstance().GetFaParam(pkgName, dmFaParam); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, RegisterDevStateCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::string extra = ""; + int32_t ret = DeviceManager::GetInstance().RegisterDevStateCallback(pkgName, extra); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, UnRegisterDevStateCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::string extra = ""; + int32_t ret = DeviceManager::GetInstance().UnRegisterDevStateCallback(pkgName, extra); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, RegisterUiStateCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int32_t ret = DeviceManager::GetInstance().RegisterUiStateCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, UnRegisterUiStateCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int32_t ret = DeviceManager::GetInstance().UnRegisterUiStateCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, RequestCredential_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + std::string reqJsonStr = "reqJsonStr"; + std::string returnJsonStr = ""; + int32_t ret = DeviceManager::GetInstance().RequestCredential(pkgName, reqJsonStr, returnJsonStr); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, ImportCredential_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + std::string credentialInfo = "credentialInfo"; + int32_t ret = DeviceManager::GetInstance().ImportCredential(pkgName, credentialInfo); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, DeleteCredential_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + std::string deleteInfo = "deleteInfo"; + int32_t ret = DeviceManager::GetInstance().DeleteCredential(pkgName, deleteInfo); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, RegisterCredentialCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::shared_ptr callback = std::make_shared(); + int32_t ret = DeviceManager::GetInstance().RegisterCredentialCallback(pkgName, callback); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + callback = nullptr; + ret = DeviceManager::GetInstance().RegisterCredentialCallback(pkgName, callback); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + callback = std::make_shared(); + ret = DeviceManager::GetInstance().RegisterCredentialCallback(pkgName, callback); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, UnRegisterCredentialCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int32_t ret = DeviceManager::GetInstance().UnRegisterCredentialCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + ret = DeviceManager::GetInstance().RegisterCredentialCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, NotifyEvent_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int32_t eventId = 1; + std::string event = ""; + int32_t ret = DeviceManager::GetInstance().NotifyEvent(pkgName, eventId, event); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + eventId = DM_NOTIFY_EVENT_START; + ret = DeviceManager::GetInstance().NotifyEvent(pkgName, eventId, event); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + eventId = DM_NOTIFY_EVENT_ONDEVICEREADY; + ret = DeviceManager::GetInstance().NotifyEvent(pkgName, eventId, event); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, OnDmServiceDied_201, testing::ext::TestSize.Level0) +{ + int32_t ret = DeviceManager::GetInstance().OnDmServiceDied(); + ASSERT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, CheckAPIAccessPermission_201, testing::ext::TestSize.Level0) +{ + int32_t ret = DeviceManager::GetInstance().CheckAPIAccessPermission(); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, CheckNewAPIAccessPermission_201, testing::ext::TestSize.Level0) +{ + int32_t ret = DeviceManager::GetInstance().CheckNewAPIAccessPermission(); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, UnBindDevice_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName_201"; + std::string deviceId = "deviceId201"; + std::string extra = "extra"; + int32_t ret = DeviceManager::GetInstance().UnBindDevice(pkgName, deviceId, extra); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, ImportAuthCode_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName_201"; + std::string authCode = "asdsedwe"; + int32_t ret = DeviceManager::GetInstance().ImportAuthCode(pkgName, authCode); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + authCode = "123456"; + ret = DeviceManager::GetInstance().ImportAuthCode(pkgName, authCode); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, StartAdvertising_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::map advertiseParam; + int32_t ret = DeviceManager::GetInstance().StartAdvertising(pkgName, advertiseParam); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, CheckAccessToTarget_201, testing::ext::TestSize.Level0) +{ + uint64_t tokenId = 1; + std::string targetId = ""; + int32_t ret = DeviceManager::GetInstance().CheckAccessToTarget(tokenId, targetId); + ASSERT_EQ(ret, ERR_DM_UNSUPPORTED_METHOD); +} + +HWTEST_F(DeviceManagerImplTest, AddDiscoveryCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + std::map discoverParam; + std::shared_ptr callback = nullptr; + int32_t ret = DeviceManager::GetInstance().AddDiscoveryCallback(pkgName, discoverParam, callback); + ASSERT_EQ(ret, DM_INVALID_FLAG_ID); +} + +HWTEST_F(DeviceManagerImplTest, SetDnPolicy_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::map policy; + int32_t ret = DeviceManager::GetInstance().SetDnPolicy(pkgName, policy); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, RegDevTrustChangeCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::shared_ptr callback = nullptr; + int32_t ret = DeviceManager::GetInstance().RegDevTrustChangeCallback(pkgName, callback); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + callback = std::make_shared(); + ret = DeviceManager::GetInstance().RegDevTrustChangeCallback(pkgName, callback); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, RegisterDeviceScreenStatusCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::shared_ptr callback = nullptr; + int32_t ret = DeviceManager::GetInstance().RegisterDeviceScreenStatusCallback(pkgName, callback); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + callback = std::make_shared(); + ret = DeviceManager::GetInstance().RegisterDeviceScreenStatusCallback(pkgName, callback); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, UnRegisterDeviceScreenStatusCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int32_t ret = DeviceManager::GetInstance().UnRegisterDeviceScreenStatusCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + ret = DeviceManager::GetInstance().UnRegisterDeviceScreenStatusCallback(pkgName); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, GetNetworkIdByUdid_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::string udid = ""; + std::string networkId = ""; + int32_t ret = DeviceManager::GetInstance().GetNetworkIdByUdid(pkgName, udid, networkId); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, RegisterCredentialAuthStatusCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::shared_ptr callback = nullptr; + int32_t ret = DeviceManager::GetInstance().RegisterCredentialAuthStatusCallback(pkgName, callback); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, UnRegisterCredentialAuthStatusCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int32_t ret = DeviceManager::GetInstance().UnRegisterCredentialAuthStatusCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + DmCommonNotifyEvent dmCommonNotifyEvent = DmCommonNotifyEvent::REG_DEVICE_SCREEN_STATE; + std::string pkgName = ""; + DeviceManager::GetInstance().SyncCallbackToService(dmCommonNotifyEvent, pkgName); + + pkgName = "pkgName"; + dmCommonNotifyEvent = DmCommonNotifyEvent::MIN; + DeviceManager::GetInstance().SyncCallbackToService(dmCommonNotifyEvent, pkgName); +} + +HWTEST_F(DeviceManagerImplTest, RegisterSinkBindCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + std::shared_ptr callback callback = nullptr; + int32_t ret = DeviceManager::GetInstance().RegisterSinkBindCallback(pkgName, callback); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerImplTest, UnRegisterSinkBindCallback_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int32_t ret = DeviceManager::GetInstance().UnRegisterSinkBindCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "pkgName"; + ret = DeviceManager::GetInstance().UnRegisterSinkBindCallback(pkgName); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, GetDeviceProfileInfoList_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + DmDeviceProfileInfoFilterOptions filterOptions; + std::shared_ptr callback = + std::make_shared(); + EXPECT_CALL(*deviceManagerNotifyMock_, RegisterGetDeviceProfileInfoListCallback(_, _)) + .WillOnce(Return(ERR_DM_FAILED)); + int32_t ret = DeviceManager::GetInstance().GetDeviceProfileInfoList(pkgName, filterOptions, callback); + ASSERT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*deviceManagerNotifyMock_, RegisterGetDeviceProfileInfoListCallback(_, _)) + .WillOnce(Return(DM_OK)); + ret = DeviceManager::GetInstance().GetDeviceProfileInfoList(pkgName, filterOptions, callback); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, GetDeviceIconInfo_201, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + DmDeviceIconInfoFilterOptions filterOptions; + std::shared_ptr callback = std::make_shared(); + EXPECT_CALL(*deviceManagerNotifyMock_, RegisterGetDeviceIconInfoCallback(_, _, _)) + .WillOnce(Return(ERR_DM_FAILED)); + int32_t ret = DeviceManager::GetInstance().GetDeviceProfileInfoList(pkgName, filterOptions, callback); + ASSERT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*deviceManagerNotifyMock_, RegisterGetDeviceIconInfoCallback(_, _, _)) + .WillOnce(Return(DM_OK)); + int32_t ret = DeviceManager::GetInstance().GetDeviceProfileInfoList(pkgName, filterOptions, callback); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_notify.cpp b/test/unittest/UTTest_device_manager_service_notify.cpp new file mode 100644 index 0000000000000000000000000000000000000000..613a738b05a325bd30427db4887fe885a62cb899 --- /dev/null +++ b/test/unittest/UTTest_device_manager_service_notify.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2025 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 "UTTest_device_manager_service_notify.h" +#include "device_manager_service_notify.h" +#include "dm_device_info.h" + +#include + +namespace OHOS { +namespace DistributedHardware { +DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerServiceNotify); +void DeviceManagerServiceNotifyTest::SetUp() {} + +void DeviceManagerServiceNotifyTest::TearDown() {} + +void DeviceManagerServiceNotifyTest::SetUpTestCase() {} + +void DeviceManagerServiceNotifyTest::TearDownTestCase() {} +namespace { + +HWTEST_F(DeviceManagerServiceNotifyTest, RegisterCallBack_001, testing::ext::TestSize.Level0) +{ + int32_t dmCommonNotifyEvent = 1; + ProcessInfo processInfo; + int32_t ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + processInfo.pkgName = "pkgName"; + processInfo.userId = 2; + ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo); + ASSERT_EQ(ret, DM_OK); + + dmCommonNotifyEvent = 10; + ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + dmCommonNotifyEvent = 2; + ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo); + ASSERT_EQ(ret, DM_OK); + + std::set processInfoSet; + processInfoSet.insert(processInfo); + DeviceManagerServiceNotify::GetInstance().callbackMap_ + .insert(std::make_pair(static_cast(dmCommonNotifyEvent), processInfoSet)); + ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo); + ASSERT_EQ(ret, DM_OK); + + dmCommonNotifyEvent = 3; + DmCommonNotifyEvent notifyEvent = static_cast(dmCommonNotifyEvent); + std::set processInfoSet; + processInfoSet.insert(processInfo); + DeviceManagerServiceNotify::GetInstance().callbackMap_[notifyEvent] = processInfoSet; + processInfo.pkgName = "proName"; + processInfo.userId = 10; + ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo); + ASSERT_EQ(ret, DM_OK); + + dmCommonNotifyEvent = 4; + std::set processInfos; + DeviceManagerServiceNotify::GetInstance().GetCallBack(dmCommonNotifyEvent, processInfos); + + dmCommonNotifyEvent = 3; + DeviceManagerServiceNotify::GetInstance().GetCallBack(dmCommonNotifyEvent, processInfos); + + ProcessInfo processInfo1; + DeviceManagerServiceNotify::GetInstance().callbackMap_[notifyEvent].insert(processInfo1); + DeviceManagerServiceNotify::GetInstance().ClearDiedProcessCallback(processInfo1); +} +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_notify.h b/test/unittest/UTTest_device_manager_service_notify.h new file mode 100644 index 0000000000000000000000000000000000000000..260c1823a94a8dc63953b135a9396608e0bb3532 --- /dev/null +++ b/test/unittest/UTTest_device_manager_service_notify.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 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 OHOS_DEVICE_MANAGER_SERVICE_NOTIFY_TEST_H +#define OHOS_DEVICE_MANAGER_SERVICE_NOTIFY_TEST_H + +#include +#include +#include +#include +#include +#include +#include + +#include "dm_device_info.h" +#include "dm_single_instance.h" +namespace OHOS { +namespace DistributedHardware { +class DeviceManagerServiceNotifyTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_DEVICE_MANAGER_SERVICE_NOTIFY_TEST_H diff --git a/test/unittest/UTTest_device_manager_service_two.cpp b/test/unittest/UTTest_device_manager_service_two.cpp index 41b977333a8220fea15f71ffbb2dce560056dfcf..eaf285ac6b1b608c1b5a0e48f82ec26b933f46f0 100644 --- a/test/unittest/UTTest_device_manager_service_two.cpp +++ b/test/unittest/UTTest_device_manager_service_two.cpp @@ -1168,6 +1168,25 @@ HWTEST_F(DeviceManagerServiceTest, StopAuthenticateDevice_004, testing::ext::Tes EXPECT_EQ(ret, ERR_DM_FAILED); } +HWTEST_F(DeviceManagerServiceTest, IsDMServiceAdapterSoLoaded_201, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().isAdapterResidentSoLoaded_ = false; + bool ret = DeviceManagerService::GetInstance().IsDMServiceAdapterSoLoaded(); + EXPECT_FALSE(ret); + + DeviceManagerService::GetInstance().isAdapterResidentSoLoaded_ = true; + DeviceManagerService::GetInstance().dmServiceImplExtResident_ = nullptr; + ret = DeviceManagerService::GetInstance().IsDMServiceAdapterSoLoaded(); + EXPECT_FALSE(ret); + + DeviceManagerService::GetInstance().isAdapterResidentSoLoaded_ = false; + DeviceManagerService::GetInstance().IsDMServiceAdapterResidentLoad(); + ret = DeviceManagerService::GetInstance().IsDMServiceAdapterSoLoaded(); + EXPECT_TRUE(ret); + + DeviceManagerService::GetInstance().UnloadDMServiceAdapterResident(); +} + HWTEST_F(DeviceManagerServiceTest, LoadHardwareFwkService_202, testing::ext::TestSize.Level0) { DeviceManagerService::GetInstance().softbusListener_ = std::make_shared(); diff --git a/test/unittest/mock/device_manager_notify_mock.cpp b/test/unittest/mock/device_manager_notify_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acc15eb94d14c74f0fcd7af9ac47add32463eadc --- /dev/null +++ b/test/unittest/mock/device_manager_notify_mock.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025 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 "device_manager_notify_mock.h" +#include "gtest/gtest.h" + +namespace OHOS { +namespace DistributedHardware { +DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerNotify); +int32_t DeviceManagerNotify::RegisterGetDeviceProfileInfoListCallback(const std::string &pkgName, + std::shared_ptr callback) +{ + return DmDeviceManagerNotify::dmDeviceManagerNotify->RegisterGetDeviceProfileInfoListCallback(pkgName, callback); +} + +int32_t DeviceManagerNotify::RegisterGetDeviceIconInfoCallback(const std::string &pkgName, const std::string &uk, + std::shared_ptr callback) +{ + return DmDeviceManagerNotify::dmDeviceManagerNotify->RegisterGetDeviceIconInfoCallback(pkgName, uk, callback); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/device_manager_notify_mock.h b/test/unittest/mock/device_manager_notify_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..a67f46b745769c4aec7149ce7188bf6b46c18068 --- /dev/null +++ b/test/unittest/mock/device_manager_notify_mock.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2025 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 OHOS_DEVICE_MANAGER_NOTIFY_MOCK_H +#define OHOS_DEVICE_MANAGER_NOTIFY_MOCK_H + +#include +#include + +#include "device_manager_notify.h" + +namespace OHOS { +namespace DistributedHardware { +class DmDeviceManagerNotify { +public: + virtual ~DmDeviceManagerNotify() = default; +public: + virtual int32_t RegisterGetDeviceIconInfoCallback(const std::string &pkgName, const std::string &uk, + std::shared_ptr callback) = 0; + virtual int32_t RegisterGetDeviceProfileInfoListCallback(const std::string &pkgName, + std::shared_ptr callback) = 0; +public: + static inline std::shared_ptr dmDeviceManagerNotify = nullptr; +}; + +class DeviceManagerNotifyMock : public DmDeviceManagerNotify { +public: + MOCK_METHOD(int32_t, RegisterGetDeviceIconInfoCallback, (const std::string &, const std::string &, + std::shared_ptr)); + MOCK_METHOD(int32_t, RegisterGetDeviceProfileInfoListCallback, (const std::string &, + std::shared_ptr)); +}; +} +} +#endif