From 0a35b5c1ab10c5adbad91ed8fc41f544dabdf74b Mon Sep 17 00:00:00 2001 From: wangsen1994 Date: Thu, 12 Jun 2025 21:59:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85TDD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangsen1994 --- .../distributed_extension_service.h | 2 + .../distributed_extension_service.cpp | 8 +- services/ans/test/unittest/BUILD.gn | 52 ++++ .../mock/include/mock_device_manager_impl.h | 42 +++ .../mock_distributed_operation_callback.h | 38 +++ .../device_manager_impl_mock.cpp | 246 ++++++++++++++++++ .../distributed_device_data_service_test.cpp | 127 +++++++++ .../distributed_extension_service_test.cpp | 130 +++++++++ .../mock_distributed_operation_callback.cpp | 40 +++ .../notification_operation_service_test.cpp | 107 ++++++++ .../softbus_socket_mock.cpp | 77 ++++++ 11 files changed, 867 insertions(+), 2 deletions(-) create mode 100644 services/ans/test/unittest/mock/include/mock_device_manager_impl.h create mode 100644 services/ans/test/unittest/mock/include/mock_distributed_operation_callback.h create mode 100644 services/ans/test/unittest/notification_extension/device_manager_impl_mock.cpp create mode 100644 services/ans/test/unittest/notification_extension/distributed_device_data_service_test.cpp create mode 100644 services/ans/test/unittest/notification_extension/distributed_extension_service_test.cpp create mode 100644 services/ans/test/unittest/notification_extension/mock_distributed_operation_callback.cpp create mode 100644 services/ans/test/unittest/notification_extension/notification_operation_service_test.cpp create mode 100644 services/ans/test/unittest/notification_extension/softbus_socket_mock.cpp diff --git a/services/ans/include/notification_extension/distributed_extension_service.h b/services/ans/include/notification_extension/distributed_extension_service.h index c08eb02d2..4b9ec595a 100644 --- a/services/ans/include/notification_extension/distributed_extension_service.h +++ b/services/ans/include/notification_extension/distributed_extension_service.h @@ -55,6 +55,8 @@ public: void OnDeviceChanged(const DmDeviceInfo &deviceInfo); void DeviceStatusChange(const DeviceStatueChangeInfo& changeInfo); static DistributedExtensionService& GetInstance(); + static std::string TransDeviceTypeToName(uint16_t deviceType_); + static std::string DeviceTypeToTypeString(uint16_t deviceType_); void HADotCallback(int32_t code, int32_t ErrCode, uint32_t branchId, std::string reason); void SendReportCallback(int32_t messageType, int32_t errCode, std::string reason); int32_t GetOperationReplyTimeout(); diff --git a/services/ans/src/notification_extension/distributed_extension_service.cpp b/services/ans/src/notification_extension/distributed_extension_service.cpp index c9bd75ee1..b90df103d 100644 --- a/services/ans/src/notification_extension/distributed_extension_service.cpp +++ b/services/ans/src/notification_extension/distributed_extension_service.cpp @@ -61,7 +61,7 @@ constexpr const int32_t ANS_CUSTOMIZE_CODE = 7; constexpr int64_t DURATION_ONE_SECOND = 1000; // 1s, millisecond } -std::string TransDeviceTypeToName(uint16_t deviceType_) +std::string DistributedExtensionService::TransDeviceTypeToName(uint16_t deviceType_) { switch (deviceType_) { case DmDeviceType::DEVICE_TYPE_WATCH: { @@ -84,7 +84,7 @@ std::string TransDeviceTypeToName(uint16_t deviceType_) } } -std::string DeviceTypeToTypeString(uint16_t deviceType) +std::string DistributedExtensionService::DeviceTypeToTypeString(uint16_t deviceType) { switch (deviceType) { case DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD: { @@ -121,6 +121,10 @@ DistributedExtensionService::DistributedExtensionService() DistributedExtensionService::~DistributedExtensionService() { + if (distributedQueue_ == nullptr) { + ANS_LOGI("Dans already release."); + return; + } std::function task = std::bind([&]() { ReleaseLocalDevice(); dansHandler_.reset(); diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 204ae80d2..1ed43ff9d 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -1434,6 +1434,57 @@ ohos_unittest("common_utils_test") { part_name = "${component_name}" } +ohos_unittest("notification_extension_test") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + + module_out_path = module_output_path + + include_dirs = [ + ".", + "mock/include", + "/${services_path}/ans/include", + "/${services_path}/ans/include/utils", + "/${services_path}/ans/include/notification_extension", + ] + + sources = [ + "notification_extension/device_manager_impl_mock.cpp", + "notification_extension/distributed_device_data_service_test.cpp", + "notification_extension/distributed_extension_service_test.cpp", + "notification_extension/mock_distributed_operation_callback.cpp", + "notification_extension/notification_operation_service_test.cpp", + "notification_extension/softbus_socket_mock.cpp", + ] + + defines = [] + + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "${services_path}/ans:libans", + ] + + external_deps = [ + "ability_base:zuri", + "c_utils:utils", + "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "time_service:time_client", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + group("unittest") { testonly = true deps = [ @@ -1446,6 +1497,7 @@ group("unittest") { ":common_utils_test", ":notification_config_parse_test", ":notification_dialog_test", + ":notification_extension_test", ":notification_extension_wrapper_unit_test", ":notification_preferences_database_branch_test", ":notification_preferences_database_test", diff --git a/services/ans/test/unittest/mock/include/mock_device_manager_impl.h b/services/ans/test/unittest/mock/include/mock_device_manager_impl.h new file mode 100644 index 000000000..616c35291 --- /dev/null +++ b/services/ans/test/unittest/mock/include/mock_device_manager_impl.h @@ -0,0 +1,42 @@ +/* + * 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 MOCK_DISTRIBUTED_DEVICE_MANAGER_IMPL_H +#define MOCK_DISTRIBUTED_DEVICE_MANAGER_IMPL_H + +#include "device_manager_impl.h" + +namespace OHOS { +namespace Notification { + +class DeviceTrigger { +public: + static void TriggerDeviceOnline(); + static void TriggerDeviceOffline(); + static void TriggerDeviceChanged(); + static void TriggerDeviceReady(); +}; + +class DeviceCheck { +public: + static bool CheckDeviceOnline(); + static void ResetDeviceData(); + static std::string GetDeviceNetworkId(std::string deviceId); +}; + +} // namespace Notification +} // namespace OHOS + +#endif // MOCK_DISTRIBUTED_DEVICE_MANAGER_IMPL_H diff --git a/services/ans/test/unittest/mock/include/mock_distributed_operation_callback.h b/services/ans/test/unittest/mock/include/mock_distributed_operation_callback.h new file mode 100644 index 000000000..948393720 --- /dev/null +++ b/services/ans/test/unittest/mock/include/mock_distributed_operation_callback.h @@ -0,0 +1,38 @@ +/* +* 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 MOCK_DISTRIBUTED_DISTRIBUTED_OPERATION_CALLBACK_STUB_H +#define MOCK_DISTRIBUTED_DISTRIBUTED_OPERATION_CALLBACK_STUB_H + +#include "ans_operation_callback_stub.h" + +namespace OHOS { +namespace Notification { + +class MockOperationCallback : public AnsOperationCallbackStub { +public: + MockOperationCallback() = default; + ~MockOperationCallback() override {}; + static int32_t GetOperationResult(); + static void ResetOperationResult(); + ErrCode OnOperationCallback(const int32_t operationResult) override; + +private: + static int32_t result; +}; + +} // namespace Notification +} // namespace OHOS +#endif // MOCK_DISTRIBUTED_DISTRIBUTED_OPERATION_CALLBACK_STUB_H diff --git a/services/ans/test/unittest/notification_extension/device_manager_impl_mock.cpp b/services/ans/test/unittest/notification_extension/device_manager_impl_mock.cpp new file mode 100644 index 000000000..4ae044a72 --- /dev/null +++ b/services/ans/test/unittest/notification_extension/device_manager_impl_mock.cpp @@ -0,0 +1,246 @@ +/* + * 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 "device_manager_impl.h" + +#include "mock_device_manager_impl.h" +namespace OHOS { + +namespace Notification { +static std::shared_ptr deviceManagerCallback = nullptr; + +void DeviceTrigger::TriggerDeviceOnline() +{ + DistributedHardware::DmDeviceInfo remoteDevice; + memset_s(&remoteDevice, sizeof(remoteDevice), 0, sizeof(remoteDevice)); + strcpy_s(remoteDevice.deviceId, sizeof(remoteDevice.deviceId) - 1, "remoteDeviceId"); + strcpy_s(remoteDevice.networkId, sizeof(remoteDevice.networkId) - 1, "remoteNetWorkId"); + remoteDevice.deviceTypeId = DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH; + deviceManagerCallback->OnDeviceOnline(remoteDevice); +} + +void DeviceTrigger::TriggerDeviceOffline() +{ + DistributedHardware::DmDeviceInfo remoteDevice; + memset_s(&remoteDevice, sizeof(remoteDevice), 0, sizeof(remoteDevice)); + strcpy_s(remoteDevice.deviceId, sizeof(remoteDevice.deviceId) - 1, "remoteDeviceId"); + strcpy_s(remoteDevice.networkId, sizeof(remoteDevice.networkId) - 1, "remoteNetWorkId"); + deviceManagerCallback->OnDeviceOffline(remoteDevice); +} + +void DeviceTrigger::TriggerDeviceChanged() +{ + DistributedHardware::DmDeviceInfo remoteDevice; + memset_s(&remoteDevice, sizeof(remoteDevice), 0, sizeof(remoteDevice)); + strcpy_s(remoteDevice.deviceId, sizeof(remoteDevice.deviceId) - 1, "remoteDeviceId"); + strcpy_s(remoteDevice.networkId, sizeof(remoteDevice.networkId) - 1, "newRemoteNetWorkId"); + deviceManagerCallback->OnDeviceChanged(remoteDevice); +} + +void DeviceTrigger::TriggerDeviceReady() +{ + DistributedHardware::DmDeviceInfo remoteDevice; + deviceManagerCallback->OnDeviceReady(remoteDevice); +} +} + +namespace DistributedHardware { + +DeviceManagerImpl &DeviceManagerImpl::GetInstance() +{ + static DeviceManagerImpl deviceManagerImpl; + return deviceManagerImpl; +} + +int32_t DeviceManagerImpl::InitDeviceManager(const std::string &pkgName, std::shared_ptr dmInitCallback) +{ + (void)dmInitCallback; + printf("InitDeviceManager pkgName:%s\n", pkgName.c_str()); + return 0; +} + +int32_t DeviceManagerImpl::UnInitDeviceManager(const std::string &pkgName) +{ + printf("UnInitDeviceManager pkgName:%s\n", pkgName.c_str()); + return 0; +} + +int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList) +{ + DmDeviceInfo remoteDevice; + memset_s(&remoteDevice, sizeof(remoteDevice), 0, sizeof(remoteDevice)); + strcpy_s(remoteDevice.deviceId, sizeof(remoteDevice.deviceId) - 1, "remoteDeviceId"); + strcpy_s(remoteDevice.deviceName, sizeof(remoteDevice.deviceName) - 1, "remoteDeviceId"); + remoteDevice.deviceTypeId = DmDeviceType::DEVICE_TYPE_WATCH; + deviceList.push_back(remoteDevice); + return 0; +} + +int32_t DeviceManagerImpl::GetTrustedDeviceList( + const std::string &pkgName, const std::string &extra, bool isRefresh, std::vector &deviceList) +{ + DmDeviceInfo remoteDevice; + memset_s(&remoteDevice, sizeof(remoteDevice), 0, sizeof(remoteDevice)); + strcpy_s(remoteDevice.deviceId, sizeof(remoteDevice.deviceId) - 1, "remoteDeviceId"); + strcpy_s(remoteDevice.deviceName, sizeof(remoteDevice.deviceName) - 1, "remoteDeviceId"); + remoteDevice.deviceTypeId = DmDeviceType::DEVICE_TYPE_WATCH; + deviceList.push_back(remoteDevice); + return 0; +} + +int32_t DeviceManagerImpl::GetLocalDeviceInfo(const std::string &pkgName, DmDeviceInfo &deviceInfo) +{ + memset_s(&deviceInfo, sizeof(deviceInfo), 0, sizeof(deviceInfo)); + strcpy_s(deviceInfo.deviceId, sizeof(deviceInfo.deviceId) - 1, "localDeviceId"); + strcpy_s(deviceInfo.deviceName, sizeof(deviceInfo.deviceName) - 1, "localDeviceName"); + deviceInfo.deviceTypeId = DmDeviceType::DEVICE_TYPE_PHONE; + return 0; +} + +int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra, + std::shared_ptr callback) +{ + Notification::deviceManagerCallback = callback; + return 0; +} + +int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName) +{ + Notification::deviceManagerCallback = nullptr; + return 0; +} + +int32_t DeviceManagerImpl::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo, + const std::string &extra, std::shared_ptr callback) +{ + return 0; +} + +int32_t DeviceManagerImpl::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId) +{ + return 0; +} + +int32_t DeviceManagerImpl::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo, + std::shared_ptr callback) +{ + return 0; +} + +int32_t DeviceManagerImpl::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId) +{ + return 0; +} + +int32_t DeviceManagerImpl::AuthenticateDevice(const std::string &pkgName, int32_t authType, + const DmDeviceInfo &deviceInfo, const std::string &extra, + std::shared_ptr callback) +{ + return 0; +} + +int32_t DeviceManagerImpl::UnAuthenticateDevice(const std::string &pkgName, const DmDeviceInfo &deviceInfo) +{ + return 0; +} + +int32_t DeviceManagerImpl::RegisterDeviceManagerFaCallback(const std::string &pkgName, + std::shared_ptr callback) +{ + return 0; +} + +int32_t DeviceManagerImpl::UnRegisterDeviceManagerFaCallback(const std::string &pkgName) +{ + return 0; +} + +int32_t DeviceManagerImpl::VerifyAuthentication(const std::string &pkgName, const std::string &authPara, + std::shared_ptr callback) +{ + return 0; +} + +int32_t DeviceManagerImpl::GetFaParam(const std::string &pkgName, DmAuthParam &dmFaParam) +{ + return 0; +} + +int32_t DeviceManagerImpl::SetUserOperation(const std::string &pkgName, int32_t action, const std::string ¶ms) +{ + return 0; +} + +int32_t DeviceManagerImpl::GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, + std::string &udid) +{ + return 0; +} + +int32_t DeviceManagerImpl::GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, + std::string &uuid) +{ + return 0; +} + +int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra) +{ + return 0; +} + +int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra) +{ + return 0; +} + +int32_t DeviceManagerImpl::RequestCredential(const std::string &pkgName, const std::string &reqJsonStr, + std::string &returnJsonStr) +{ + return 0; +} + +int32_t DeviceManagerImpl::ImportCredential(const std::string &pkgName, const std::string &credentialInfo) +{ + return 0; +} + +int32_t DeviceManagerImpl::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo) +{ + return 0; +} + +int32_t DeviceManagerImpl::RegisterCredentialCallback(const std::string &pkgName, + std::shared_ptr callback) +{ + return 0; +} + +int32_t DeviceManagerImpl::UnRegisterCredentialCallback(const std::string &pkgName) +{ + return 0; +} + +int32_t DeviceManagerImpl::NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event) +{ + return 0; +} + +int32_t DeviceManagerImpl::OnDmServiceDied() +{ + return 0; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/ans/test/unittest/notification_extension/distributed_device_data_service_test.cpp b/services/ans/test/unittest/notification_extension/distributed_device_data_service_test.cpp new file mode 100644 index 000000000..900646a9e --- /dev/null +++ b/services/ans/test/unittest/notification_extension/distributed_device_data_service_test.cpp @@ -0,0 +1,127 @@ +/* + * 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 "gtest/gtest.h" + +#define private public + +#include "ans_inner_errors.h" +#include "distributed_data_define.h" +#include "distributed_device_data_service.h" + +namespace OHOS { +namespace Notification { + +using namespace testing::ext; + +namespace { +const std::string DEVICE_ID = "abcd"; +const std::string DEVICE_TYPE = "pc"; +} +class DistributedDeviceDataServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() {}; + void TearDown() {}; +}; + +void DistributedDeviceDataServiceTest::SetUpTestCase() +{ + GTEST_LOG_(INFO) << "SetUp Case start"; + DistributedDeviceDataService::GetInstance().ResetTargetDevice(DEVICE_TYPE, DEVICE_ID); + GTEST_LOG_(INFO) << "SetUp end"; +} + +void DistributedDeviceDataServiceTest::TearDownTestCase() +{ + GTEST_LOG_(INFO) << "TearDown case"; +} + +/** + * @tc.name: Device sync switch check + * @tc.desc: Test device data service + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DistributedDeviceDataServiceTest, DeviceData_00001, Function | SmallTest | Level1) +{ + // add device sync switch data failed, because deviceType or deviceId is empty. + int32_t result = DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch("", "", true, true); + ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM); + + // add device sync switch data + result = DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch(DEVICE_TYPE, + DEVICE_ID, true, true); + ASSERT_EQ(result, (int)ERR_OK); + + bool liveView = DistributedDeviceDataService::GetInstance().GetDeviceLiveViewEnable(DEVICE_TYPE, DEVICE_ID); + ASSERT_EQ(true, liveView); + + bool notification = DistributedDeviceDataService::GetInstance().GetDeviceNotificationEnable(DEVICE_TYPE, DEVICE_ID); + ASSERT_EQ(true, notification); + + // change device sync switch data + result = DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch(DEVICE_TYPE, + DEVICE_ID, true, false); + + notification = DistributedDeviceDataService::GetInstance().GetDeviceLiveViewEnable(DEVICE_TYPE, DEVICE_ID); + ASSERT_EQ(false, notification); +} + +/** + * @tc.name: Device sync installed bundle check + * @tc.desc: Test device data service + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DistributedDeviceDataServiceTest, DeviceData_00002, Function | SmallTest | Level1) +{ + StringAnonymous(DEVICE_ID); + // add device installed bundles failed, because deviceType or deviceId is empty. + int32_t result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList("", + DEVICE_ID, BunleListOperationType::ADD_BUNDLES, {}); + ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM); + + // add device installed bundles + std::vector bundleList = { "ohos.com.test1", "ohos.com.test2" }; + result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE, + DEVICE_ID, BunleListOperationType::ADD_BUNDLES, bundleList); + ASSERT_EQ(result, (int)ERR_OK); + bool exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist(DEVICE_TYPE, + DEVICE_ID, "ohos.com.test1"); + ASSERT_EQ(exist, true); + + // remove device installed bundles + result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE, + DEVICE_ID, BunleListOperationType::REMOVE_BUNDLES, { "ohos.com.test1" }); + ASSERT_EQ(result, (int)ERR_OK); + exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist(DEVICE_TYPE, + DEVICE_ID, "ohos.com.test1"); + ASSERT_EQ(exist, false); + exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist(DEVICE_TYPE, + DEVICE_ID, "ohos.com.test2"); + ASSERT_EQ(exist, true); + + // clear device installed bundles + result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE, + DEVICE_ID, BunleListOperationType::RELEASE_BUNDLES, {}); + ASSERT_EQ(result, (int)ERR_OK); + exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist(DEVICE_TYPE, + DEVICE_ID, "ohos.com.test2"); + ASSERT_EQ(exist, false); +} +} +} diff --git a/services/ans/test/unittest/notification_extension/distributed_extension_service_test.cpp b/services/ans/test/unittest/notification_extension/distributed_extension_service_test.cpp new file mode 100644 index 000000000..8ad198c20 --- /dev/null +++ b/services/ans/test/unittest/notification_extension/distributed_extension_service_test.cpp @@ -0,0 +1,130 @@ +/* + * 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 "gtest/gtest.h" + +#define private public +#include "ans_inner_errors.h" +#include "distributed_data_define.h" +#include "distributed_extension_service.h" +#include "distributed_device_manager.h" +#include "mock_device_manager_impl.h" + +namespace OHOS { +namespace Notification { + +using namespace testing::ext; +using namespace DistributedHardware; + +class DistributedExtensionServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() {}; + void TearDown() {}; +}; + +void DistributedExtensionServiceTest::SetUpTestCase() +{ + GTEST_LOG_(INFO) << "SetUp Case start"; + GTEST_LOG_(INFO) << "SetUp end"; +} + +void DistributedExtensionServiceTest::TearDownTestCase() +{ + GTEST_LOG_(INFO) << "TearDown case"; +} + +/** + * @tc.name: Distributed extension service config check + * @tc.desc: Test device data service + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DistributedExtensionServiceTest, extension_00001, Function | SmallTest | Level1) +{ + // init extension conifg. + bool result = DistributedExtensionService::GetInstance().initConfig(); + ASSERT_EQ(result, true); + DistributedDeviceConfig config = DistributedExtensionService::GetInstance().deviceConfig_; + if (config.localType == "Phone") { + ASSERT_EQ(config.supportPeerDevice.count("Pc"), 1); + ASSERT_EQ(config.supportPeerDevice.count("Watch"), 1); + ASSERT_EQ(config.supportPeerDevice.count("Tablet"), 1); + } +} + +/** + * @tc.name: Distributed extension service + * @tc.desc: Check device Type Convert + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DistributedExtensionServiceTest, extension_00003, Function | SmallTest | Level1) +{ + std::string deviceType; + deviceType = DistributedExtensionService::TransDeviceTypeToName(DmDeviceType::DEVICE_TYPE_WATCH); + ASSERT_EQ(deviceType, "Watch"); + deviceType = DistributedExtensionService::TransDeviceTypeToName(DmDeviceType::DEVICE_TYPE_PAD); + ASSERT_EQ(deviceType, "Tablet"); + deviceType = DistributedExtensionService::TransDeviceTypeToName(DmDeviceType::DEVICE_TYPE_PHONE); + ASSERT_EQ(deviceType, "Phone"); + deviceType = DistributedExtensionService::TransDeviceTypeToName(DmDeviceType::DEVICE_TYPE_2IN1); + ASSERT_EQ(deviceType, "Pc"); + deviceType = DistributedExtensionService::TransDeviceTypeToName(DmDeviceType::DEVICE_TYPE_PC); + ASSERT_EQ(deviceType, "Pc"); + deviceType = DistributedExtensionService::DeviceTypeToTypeString(DmDeviceType::DEVICE_TYPE_PAD); + ASSERT_EQ(deviceType, "pad"); + deviceType = DistributedExtensionService::DeviceTypeToTypeString(DmDeviceType::DEVICE_TYPE_PC); + ASSERT_EQ(deviceType, "pc"); + deviceType = DistributedExtensionService::DeviceTypeToTypeString(DmDeviceType::DEVICE_TYPE_2IN1); + ASSERT_EQ(deviceType, "pc"); +} + +/** + * @tc.name: Distributed extension service + * @tc.desc: Check the device status change + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DistributedExtensionServiceTest, extension_00002, Function | SmallTest | Level1) +{ + // register device manager. + bool result = DistributedDeviceManager::GetInstance().RegisterDms(true); + ASSERT_EQ(result, true); + // register device manager. + DistributedDeviceManager::GetInstance().InitTrustList(); + // trigger device online + DeviceTrigger::TriggerDeviceOnline(); + DistributedExtensionService::GetInstance().OnAllConnectOnline(); + sleep(1); + // check online device + bool isEmpty = DistributedExtensionService::GetInstance().deviceMap_.empty(); + ASSERT_EQ(isEmpty, false); + // trigger device ready + DeviceTrigger::TriggerDeviceReady(); + // trigger device change + DeviceTrigger::TriggerDeviceChanged(); + // trigger device offline + DeviceTrigger::TriggerDeviceOffline(); + sleep(1); + // check online device + isEmpty = DistributedExtensionService::GetInstance().deviceMap_.empty(); + ASSERT_EQ(isEmpty, true); + sleep(1); + DistributedExtensionService::GetInstance().distributedQueue_ = nullptr; +} +} +} diff --git a/services/ans/test/unittest/notification_extension/mock_distributed_operation_callback.cpp b/services/ans/test/unittest/notification_extension/mock_distributed_operation_callback.cpp new file mode 100644 index 000000000..cb8b9161d --- /dev/null +++ b/services/ans/test/unittest/notification_extension/mock_distributed_operation_callback.cpp @@ -0,0 +1,40 @@ +/* + * 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 "mock_distributed_operation_callback.h" + +namespace OHOS { + +namespace Notification { + +int32_t MockOperationCallback::result = -1; +int32_t MockOperationCallback::GetOperationResult() +{ + return result; +} + +void MockOperationCallback::ResetOperationResult() +{ + result = -1; +} + +ErrCode MockOperationCallback::OnOperationCallback(const int32_t operationResult) +{ + result = operationResult; + return 0; +} + +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/ans/test/unittest/notification_extension/notification_operation_service_test.cpp b/services/ans/test/unittest/notification_extension/notification_operation_service_test.cpp new file mode 100644 index 000000000..95e9ce658 --- /dev/null +++ b/services/ans/test/unittest/notification_extension/notification_operation_service_test.cpp @@ -0,0 +1,107 @@ +/* + * 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 "gtest/gtest.h" + +#define private public + +#include "ans_inner_errors.h" +#include "mock_distributed_operation_callback.h" +#include "notification_operation_service.h" +#include "mock_distributed_operation_callback.h" + +namespace OHOS { +namespace Notification { + +using namespace testing::ext; + +class DistributedOperationServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() {}; + void TearDown() {}; +}; + +void DistributedOperationServiceTest::SetUpTestCase() +{ + GTEST_LOG_(INFO) << "SetUp Case start"; + GTEST_LOG_(INFO) << "SetUp end"; +} + +void DistributedOperationServiceTest::TearDownTestCase() +{ + GTEST_LOG_(INFO) << "TearDown case"; +} + +/** + * @tc.name: Device sync switch check + * @tc.desc: Test device data service + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DistributedOperationServiceTest, DeviceData_00001, Function | SmallTest | Level1) +{ + MockOperationCallback::ResetOperationResult(); + // add operation. + sptr callback = new (std::nothrow) MockOperationCallback(); + DistributedOperationService::GetInstance().AddOperation("abcd", callback); + // remove operation. + DistributedOperationService::GetInstance().RemoveOperationResponse("abcd"); + // not return operation result. + int32_t result = MockOperationCallback::GetOperationResult(); + ASSERT_EQ(result, -1); +} + +/** + * @tc.name: Device sync switch check + * @tc.desc: Test device data service + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DistributedOperationServiceTest, DeviceData_00002, Function | SmallTest | Level1) +{ + MockOperationCallback::ResetOperationResult(); + // add operation. + sptr callback = new (std::nothrow) MockOperationCallback(); + DistributedOperationService::GetInstance().AddOperation("abcd", callback); + // invoke time out operation. + DistributedOperationService::GetInstance().HandleOperationTimeOut("abcd"); + // time out operation result. + sleep(1); + int32_t result = MockOperationCallback::GetOperationResult(); + ASSERT_EQ(result, (int)ERR_ANS_OPERATION_TIMEOUT); +} + +/** + * @tc.name: Device sync switch check + * @tc.desc: Test device data service + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DistributedOperationServiceTest, DeviceData_00003, Function | SmallTest | Level1) +{ + MockOperationCallback::ResetOperationResult(); + // add operation. + sptr callback = new (std::nothrow) MockOperationCallback(); + DistributedOperationService::GetInstance().AddOperation("abcd", callback); + // invoke successful operation. + DistributedOperationService::GetInstance().ReplyOperationResponse("abcd", 0); + // invoke successful. + int32_t result = MockOperationCallback::GetOperationResult(); + ASSERT_EQ(result, 0); +} +} +} diff --git a/services/ans/test/unittest/notification_extension/softbus_socket_mock.cpp b/services/ans/test/unittest/notification_extension/softbus_socket_mock.cpp new file mode 100644 index 000000000..271a358bd --- /dev/null +++ b/services/ans/test/unittest/notification_extension/softbus_socket_mock.cpp @@ -0,0 +1,77 @@ +/* + * 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 "socket.h" + + +int32_t Socket(SocketInfo info) +{ + return 1; +} + +int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener) +{ + return 0; +} + +int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener) +{ + return 0; +} + +int32_t BindAsync(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener) +{ + return 0; +} + +int32_t SendBytes(int32_t socket, const void *data, uint32_t len) +{ + return 0; +} + +int32_t SendBytesAsync(int32_t socket, uint32_t dataSeq, const void *data, uint32_t len) +{ + return 0; +} + +int32_t SendMessage(int32_t socket, const void *data, uint32_t len) +{ + return 0; +} + +int32_t SendStream(int32_t socket, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *param) +{ + return 0; +} + +int32_t SendFile(int32_t socket, const char *sFileList[], const char *dFileList[], uint32_t fileCnt) +{ + return 0; +} + +void Shutdown(int32_t socket) +{ +} + +int32_t EvaluateQos(const char *peerNetworkId, TransDataType dataType, const QosTV *qos, uint32_t qosCount) +{ + return 0; +} + +int32_t SetAccessInfo(int32_t socket, SocketAccessInfo accessInfo) +{ + return 0; +} -- Gitee