diff --git a/services/ans/test/unittest/notification_subscriber_manager_test.cpp b/services/ans/test/unittest/notification_subscriber_manager_test.cpp index d5b9e47e67ace7500931ee9eb4dde9a1981c315d..ab56d178c85d48df296b1893980a3edf093fd434 100644 --- a/services/ans/test/unittest/notification_subscriber_manager_test.cpp +++ b/services/ans/test/unittest/notification_subscriber_manager_test.cpp @@ -23,6 +23,7 @@ #include "ans_inner_errors.h" #include "ans_subscriber_listener.h" +#include "notification_analytics_util.h" using namespace testing::ext; using namespace testing; @@ -328,5 +329,344 @@ HWTEST_F(NotificationSubscriberManagerTest, OnRemoteDied_001, Function | SmallTe notificationSubscriberManager_->OnRemoteDied(obj); EXPECT_NE(notificationSubscriberManager_, nullptr); } + +/** + * @tc.number : IsDeviceFlag_001 + * @tc.name : DeviceFlag is DEVICE_TYPE_LITE_WEARABLE + * @tc.desc : Test IsDeviceFlag . + */ +HWTEST_F(NotificationSubscriberManagerTest, IsDeviceFlag_001, Function | SmallTest | Level1) +{ + sptr mockSubscriber = new MockAnsSubscriber(); + std::shared_ptr record = + notificationSubscriberManager_->CreateSubscriberRecord(mockSubscriber); + sptr request = new NotificationRequest(); + + std::shared_ptr flags = std::make_shared(); + std::map> map = { { DEVICE_TYPE_LITE_WEARABLE, flags } }; + std::shared_ptr>> mapFlags = + std::make_shared>>(map); + request->SetDeviceFlags(mapFlags); + sptr notification = new Notification(request); + + bool wearableFlag = false; + bool headsetFlag = false; + bool keyNodeFlag = false; + + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddDeviceType(DEVICE_TYPE_LITE_WEARABLE); + notificationSubscriberManager_->AddRecordInfo(record, subscribeInfo); + notificationSubscriberManager_->IsDeviceFlag(record, notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_TRUE(wearableFlag); +} + +/** + * @tc.number : IsDeviceFlag_002 + * @tc.name : DeviceFlag is DEVICE_TYPE_WEARABLE + * @tc.desc : Test IsDeviceFlag . + */ +HWTEST_F(NotificationSubscriberManagerTest, IsDeviceFlag_002, Function | SmallTest | Level1) +{ + sptr mockSubscriber = new MockAnsSubscriber(); + std::shared_ptr record = + notificationSubscriberManager_->CreateSubscriberRecord(mockSubscriber); + sptr request = new NotificationRequest(); + + std::shared_ptr flags = std::make_shared(); + std::map> map = { { DEVICE_TYPE_WEARABLE, flags } }; + std::shared_ptr>> mapFlags = + std::make_shared>>(map); + request->SetDeviceFlags(mapFlags); + sptr notification = new Notification(request); + + bool wearableFlag = false; + bool headsetFlag = false; + bool keyNodeFlag = false; + + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddDeviceType(DEVICE_TYPE_WEARABLE); + notificationSubscriberManager_->AddRecordInfo(record, subscribeInfo); + notificationSubscriberManager_->IsDeviceFlag(record, notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_TRUE(wearableFlag); +} + +/** + * @tc.number : IsDeviceFlag_003 + * @tc.name : DeviceFlag is DEVICE_TYPE_HEADSET + * @tc.desc : Test IsDeviceFlag . + */ +HWTEST_F(NotificationSubscriberManagerTest, IsDeviceFlag_003, Function | SmallTest | Level1) +{ + sptr mockSubscriber = new MockAnsSubscriber(); + std::shared_ptr record = + notificationSubscriberManager_->CreateSubscriberRecord(mockSubscriber); + sptr request = new NotificationRequest(); + + std::shared_ptr flags = std::make_shared(); + std::map> map = { { DEVICE_TYPE_HEADSET, flags } }; + std::shared_ptr>> mapFlags = + std::make_shared>>(map); + request->SetDeviceFlags(mapFlags); + sptr notification = new Notification(request); + + bool wearableFlag = false; + bool headsetFlag = false; + bool keyNodeFlag = false; + + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddDeviceType(DEVICE_TYPE_HEADSET); + notificationSubscriberManager_->AddRecordInfo(record, subscribeInfo); + notificationSubscriberManager_->IsDeviceFlag(record, notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_TRUE(headsetFlag); +} + +/** + * @tc.number : IsDeviceFlag_004 + * @tc.name : request->IsCommonLiveView() is true, keyNodeFlag is false + * @tc.desc : Test IsDeviceFlag . + */ +HWTEST_F(NotificationSubscriberManagerTest, IsDeviceFlag_004, Function | SmallTest | Level1) +{ + sptr mockSubscriber = new MockAnsSubscriber(); + std::shared_ptr record = + notificationSubscriberManager_->CreateSubscriberRecord(mockSubscriber); + sptr request = new NotificationRequest(); + + std::shared_ptr flags = std::make_shared(); + std::map> map = { { DEVICE_TYPE_HEADSET, flags } }; + std::shared_ptr>> mapFlags = + std::make_shared>>(map); + request->SetDeviceFlags(mapFlags); + + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + + std::shared_ptr content = std::make_shared(); + content->contentType_ = NotificationContent::Type::LIVE_VIEW; + request->SetContent(content); + + std::shared_ptr flag = nullptr; + request->SetFlags(flag); + sptr notification = new Notification(request); + + bool wearableFlag = false; + bool headsetFlag = false; + bool keyNodeFlag = false; + + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddDeviceType(DEVICE_TYPE_HEADSET); + notificationSubscriberManager_->AddRecordInfo(record, subscribeInfo); + notificationSubscriberManager_->IsDeviceFlag(record, notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_TRUE(headsetFlag); + EXPECT_FALSE(keyNodeFlag); +} + +/** + * @tc.number : IsDeviceFlag_005 + * @tc.name : request->IsCommonLiveView() is true, keyNodeFlag is true + * @tc.desc : Test IsDeviceFlag . + */ +HWTEST_F(NotificationSubscriberManagerTest, IsDeviceFlag_005, Function | SmallTest | Level1) +{ + sptr mockSubscriber = new MockAnsSubscriber(); + std::shared_ptr record = + notificationSubscriberManager_->CreateSubscriberRecord(mockSubscriber); + sptr request = new NotificationRequest(); + + std::shared_ptr flags = std::make_shared(); + std::map> map = { { DEVICE_TYPE_HEADSET, flags } }; + std::shared_ptr>> mapFlags = + std::make_shared>>(map); + request->SetDeviceFlags(mapFlags); + + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + + std::shared_ptr content = std::make_shared(); + content->contentType_ = NotificationContent::Type::LIVE_VIEW; + request->SetContent(content); + + flags->SetVibrationEnabled(NotificationConstant::FlagStatus::OPEN); + flags->SetSoundEnabled(NotificationConstant::FlagStatus::OPEN); + request->SetFlags(flags); + sptr notification = new Notification(request); + + bool wearableFlag = false; + bool headsetFlag = false; + bool keyNodeFlag = false; + + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddDeviceType(DEVICE_TYPE_HEADSET); + notificationSubscriberManager_->AddRecordInfo(record, subscribeInfo); + notificationSubscriberManager_->IsDeviceFlag(record, notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_TRUE(headsetFlag); + EXPECT_TRUE(keyNodeFlag); +} + +/** + * @tc.number : TrackCodeLog_001 + * @tc.name : CUSTOM and >=100 + * @tc.desc : Test TrackCodeLog . + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_001, Function | SmallTest | Level1) +{ + sptr request = new NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::CUSTOM); + sptr notification = new Notification(request); + + bool wearableFlag = true; + bool headsetFlag = true; + bool keyNodeFlag = false; + HaMetaMessage::syncWatchHeadSet_ = 100; + notificationSubscriberManager_->TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncWatchHeadSet_, 0); +} + +/** + * @tc.number : TrackCodeLog_002 + * @tc.name : LIVE_VIEW and >=100 + * @tc.desc : Test TrackCodeLog . + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_002, Function | SmallTest | Level1) +{ + sptr request = new NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + sptr notification = new Notification(request); + + bool wearableFlag = true; + bool headsetFlag = true; + bool keyNodeFlag = false; + HaMetaMessage::syncLiveViewWatchHeadSet_ = 100; + notificationSubscriberManager_->TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncLiveViewWatchHeadSet_, 0); +} + +/** + * @tc.number : TrackCodeLog_003 + * @tc.name : CUSTOM and Timeout + * @tc.desc : Test TrackCodeLog . + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_003, Function | SmallTest | Level1) +{ + sptr request = new NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::CUSTOM); + sptr notification = new Notification(request); + + bool wearableFlag = true; + bool headsetFlag = true; + bool keyNodeFlag = false; + HaMetaMessage::syncWatchHeadSet_ = 10; + HaMetaMessage::time_ = 0; + notificationSubscriberManager_->TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncWatchHeadSet_, 0); +} + +/** + * @tc.number : TrackCodeLog_004 + * @tc.name : LIVE_VIEW and Timeout + * @tc.desc : Test TrackCodeLog . + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_004, Function | SmallTest | Level1) +{ + sptr request = new NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + sptr notification = new Notification(request); + + bool wearableFlag = true; + bool headsetFlag = true; + bool keyNodeFlag = false; + HaMetaMessage::syncLiveViewWatchHeadSet_ = 10; + HaMetaMessage::liveViewTime_ = 0; + notificationSubscriberManager_->TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncLiveViewWatchHeadSet_, 0); +} + +/** + * @tc.number : TrackCodeLog_005 + * @tc.name : SlotType is LIVE_VIEW + * @tc.desc : Test TrackCodeLog . + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_005, Function | SmallTest | Level1) +{ + sptr liveViewRrequest = new NotificationRequest(); + liveViewRrequest->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + sptr liveViewNotification = new Notification(liveViewRrequest); + + bool wearableFlag = false; + bool headsetFlag = true; + bool keyNodeFlag = true; + notificationSubscriberManager_->TrackCodeLog(liveViewNotification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncLiveViewHeadSet_, 0); + EXPECT_EQ(HaMetaMessage::keyNode_, 0); + + wearableFlag = true; + headsetFlag = true; + keyNodeFlag = false; + notificationSubscriberManager_->TrackCodeLog(liveViewNotification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncLiveViewWatchHeadSet_, 1); + + wearableFlag = false; + headsetFlag = true; + keyNodeFlag = false; + notificationSubscriberManager_->TrackCodeLog(liveViewNotification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncLiveViewHeadSet_, 1); + + wearableFlag = true; + headsetFlag = false; + keyNodeFlag = false; + notificationSubscriberManager_->TrackCodeLog(liveViewNotification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncLiveViewWatch_, 1); + + wearableFlag = false; + headsetFlag = false; + keyNodeFlag = false; + notificationSubscriberManager_->TrackCodeLog(liveViewNotification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncLiveViewWatchHeadSet_, 1); + EXPECT_EQ(HaMetaMessage::syncLiveViewHeadSet_, 1); + EXPECT_EQ(HaMetaMessage::syncLiveViewWatch_, 1); +} + +/** + * @tc.number : TrackCodeLog_006 + * @tc.name : SlotType is CUSTOM + * @tc.desc : Test TrackCodeLog . + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_006, Function | SmallTest | Level1) +{ + sptr request = new NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::CUSTOM); + sptr notification = new Notification(request); + + bool wearableFlag = true; + bool headsetFlag = true; + bool keyNodeFlag = false; + notificationSubscriberManager_->TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncWatchHeadSet_, 1); + + wearableFlag = false; + headsetFlag = true; + keyNodeFlag = false; + notificationSubscriberManager_->TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncHeadSet_, 1); + + wearableFlag = true; + headsetFlag = false; + keyNodeFlag = false; + notificationSubscriberManager_->TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncWatch_, 1); + + wearableFlag = false; + headsetFlag = false; + keyNodeFlag = false; + notificationSubscriberManager_->TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + EXPECT_EQ(HaMetaMessage::syncWatchHeadSet_, 1); + EXPECT_EQ(HaMetaMessage::syncHeadSet_, 1); + EXPECT_EQ(HaMetaMessage::syncWatch_, 1); + + HaMetaMessage message; + message.SlotType(NotificationConstant::SlotType::CUSTOM); + NotificationAnalyticsUtil::BuildAnsData(message); + EXPECT_EQ(HaMetaMessage::syncWatchHeadSet_, 0); + EXPECT_EQ(HaMetaMessage::syncHeadSet_, 0); + EXPECT_EQ(HaMetaMessage::syncWatch_, 0); +} } // namespace Notification } // namespace OHOS diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index 72ff01623d4e150d43bec40c0c8ba4e65d668387..f823af1e6e9fefae43aeb93b82141bbe3b81dedb 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -439,6 +439,7 @@ ohos_unittest("distributed_service_test") { "/${services_path}/distributed/src/soft_bus/distributed_cmd_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_liveview_all_scenarios_extension_wrapper.cpp", "/${services_path}/distributed/src/soft_bus/distributed_local_config.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_manager.cpp", "/${services_path}/distributed/src/soft_bus/distributed_observer_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_publish_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_server.cpp", diff --git a/services/distributed/test/unittest/distributed_service_test.cpp b/services/distributed/test/unittest/distributed_service_test.cpp index 77176da3a796ae462eedb8f4f544ac76b6fa673e..e60e8b0f14a257a63bcf1b393841710183248850 100644 --- a/services/distributed/test/unittest/distributed_service_test.cpp +++ b/services/distributed/test/unittest/distributed_service_test.cpp @@ -18,6 +18,7 @@ #include "gtest/gtest.h" #define private public #include "batch_remove_box.h" +#include "distributed_manager.h" #include "distributed_service.h" #undef private #include "remove_box.h" @@ -27,6 +28,10 @@ using namespace testing::ext; namespace OHOS { namespace Notification { +namespace { + constexpr const int32_t DELETE_ERROR_EVENT_CODE = 5; +} + class DistributedServiceTest : public testing::Test { public: void SetUp() override; @@ -206,5 +211,40 @@ HWTEST_F(DistributedServiceTest, DistributedServiceTest_01100, Function | SmallT std::string result = DistributedService::GetInstance().GetNotificationKey(notification); ASSERT_EQ(result, "ans_distributed_notificationKey"); } + +/** + * @tc.name : DistributedServiceTest_01200 + * @tc.number : DistributedServiceTest_01200 + * @tc.desc : Test the RemoveNotification function. + */ +HWTEST_F(DistributedServiceTest, DistributedServiceTest_01200, Function | SmallTest | Level1) +{ + NotificationRemoveBox removeBox; + std::string notificationKey = "notificationId"; + removeBox.SetNotificationHashCode(notificationKey); + auto haCallBack = [](int32_t code, int32_t errCode, uint32_t branchID, std::string reason) {}; + DistributedManager::GetInstance().InitHACallBack(haCallBack); + DistributedService::GetInstance().RemoveNotification(removeBox.box_); + ASSERT_EQ(DistributedService::GetInstance().code_, DELETE_ERROR_EVENT_CODE); +} + +/** + * @tc.name : DistributedServiceTest_01300 + * @tc.number : DistributedServiceTest_01300 + * @tc.desc : Test the RemoveNotification function. + */ +HWTEST_F(DistributedServiceTest, DistributedServiceTest_01300, Function | SmallTest | Level1) +{ + BatchRemoveNotificationBox batchRemoveBox; + std::string hashCodes = "notificationId1 notificationId2"; + std::string slotTypes = "5 5"; + auto haCallBack = [](int32_t code, int32_t errCode, uint32_t branchID, std::string reason) {}; + DistributedManager::GetInstance().InitHACallBack(haCallBack); + auto reportCallBack = [](int32_t errorCode, uint32_t branchId, const std::string& errorReason) {}; + DistributedManager::GetInstance().InitSendReportCallBack(reportCallBack); + batchRemoveBox.SetNotificationHashCode(hashCodes); + DistributedService::GetInstance().RemoveNotifications(batchRemoveBox.box_); + ASSERT_EQ(DistributedService::GetInstance().code_, DELETE_ERROR_EVENT_CODE); +} } // namespace Notification } // namespace OHOS \ No newline at end of file