1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <memory>
17
18 #include "gtest/gtest.h"
19 #define private public
20 #include "batch_remove_box.h"
21 #include "distributed_service.h"
22 #include "distributed_publish_service.h"
23 #undef private
24 #include "remove_box.h"
25
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace Notification {
30
31 class DistributedServiceTest : public testing::Test {
32 public:
33 void SetUp() override;
34 void TearDown() override;
35 };
36
SetUp()37 void DistributedServiceTest::SetUp() {}
38
TearDown()39 void DistributedServiceTest::TearDown() {}
40
41 /**
42 * @tc.name : DistributedServiceTest_00100
43 * @tc.number : DistributedServiceTest_00100
44 * @tc.desc : Test the RemoveNotification function with a null boxMessage.
45 */
46 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00100, Function | SmallTest | Level1)
47 {
48 std::shared_ptr<TlvBox> boxMessage = nullptr;
49 DistributedPublishService::GetInstance().RemoveNotification(boxMessage);
50 ASSERT_EQ(boxMessage, nullptr);
51 }
52
53 /**
54 * @tc.name : DistributedServiceTest_00200
55 * @tc.number : DistributedServiceTest_00200
56 * @tc.desc : Test the RemoveNotification function with a valid NotificationRemoveBox.
57 */
58 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00200, Function | SmallTest | Level1)
59 {
60 NotificationRemoveBox removeBox;
61 std::string notificationKey = "notificationId";
62 removeBox.SetNotificationHashCode(notificationKey);
63 DistributedPublishService::GetInstance().RemoveNotification(removeBox.box_);
64 ASSERT_NE(removeBox.box_, nullptr);
65 }
66
67 /**
68 * @tc.name : DistributedServiceTest_00300
69 * @tc.number : DistributedServiceTest_00300
70 * @tc.desc : Test the RemoveNotification function with empty notificationKey.
71 */
72 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00300, Function | SmallTest | Level1)
73 {
74 std::shared_ptr<TlvBox> boxMessage = nullptr;
75 DistributedPublishService::GetInstance().RemoveNotifications(boxMessage);
76 ASSERT_EQ(boxMessage, nullptr);
77 }
78
79 /**
80 * @tc.name : DistributedServiceTest_00400
81 * @tc.number : DistributedServiceTest_00400
82 * @tc.desc : Test the OnBatchCanceled function with an valid notifications.
83 */
84 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00400, Function | SmallTest | Level1)
85 {
86 BatchRemoveNotificationBox batchRemoveBox;
87 std::string hashCodes = "notificationId1 notificationId2";
88 batchRemoveBox.SetNotificationHashCodes(hashCodes);
89 DistributedPublishService::GetInstance().RemoveNotifications(batchRemoveBox.box_);
90 ASSERT_NE(batchRemoveBox.box_, nullptr);
91 }
92
93 /**
94 * @tc.name : DistributedServiceTest_00500
95 * @tc.number : DistributedServiceTest_00500
96 * @tc.desc : Test the OnBatchCanceled function with a null serviceQueue_.
97 */
98 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00500, Function | SmallTest | Level1)
99 {
100 std::vector<std::shared_ptr<Notification>> notifications;
101 DistributedDeviceInfo peerDevice;
102
103 std::shared_ptr<ffrt::queue> serviceQueue = DistributedService::GetInstance().serviceQueue_;
104 DistributedService::GetInstance().serviceQueue_ = nullptr;
105 DistributedService::GetInstance().OnBatchCanceled(notifications, peerDevice);
106
107 ASSERT_EQ(DistributedService::GetInstance().serviceQueue_, nullptr);
108 DistributedService::GetInstance().serviceQueue_ = serviceQueue;
109 }
110
111 /**
112 * @tc.name : DistributedServiceTest_00600
113 * @tc.number : DistributedServiceTest_00600
114 * @tc.desc : Test the OnBatchCanceled function with valid notifications and peer device.
115 */
116 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00600, Function | SmallTest | Level1)
117 {
118 std::vector<std::shared_ptr<Notification>> notifications;
119 DistributedDeviceInfo peerDevice;
120 peerDevice.deviceId_ = 1;
121 peerDevice.deviceType_ = 1;
122
123 std::shared_ptr<Notification> notificationNull = nullptr;
124 sptr<NotificationRequest> request = new NotificationRequest();
125 std::shared_ptr<Notification> notification = std::make_shared<Notification>(request);
126 notification->SetKey("notificationKey");
127 notifications.push_back(notificationNull);
128 notifications.push_back(notification);
129 DistributedService::GetInstance().OnBatchCanceled(notifications, peerDevice);
130 ASSERT_EQ((notificationNull == nullptr && notification != nullptr), true);
131 }
132
133 /**
134 * @tc.name : DistributedServiceTest_00700
135 * @tc.number : DistributedServiceTest_00700
136 * @tc.desc : Test the OnCanceled function with a valid notification and peer device.
137 */
138 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00700, Function | SmallTest | Level1)
139 {
140 sptr<NotificationRequest> request = new NotificationRequest();
141 std::shared_ptr<Notification> notification = std::make_shared<Notification>(request);
142 notification->SetKey("notificationKey");
143 DistributedDeviceInfo peerDevice;
144 peerDevice.deviceId_ = 1;
145 peerDevice.deviceType_ = 1;
146
147 std::shared_ptr<ffrt::queue> serviceQueue = DistributedService::GetInstance().serviceQueue_;
148 DistributedService::GetInstance().serviceQueue_ = nullptr;
149 DistributedService::GetInstance().OnCanceled(notification, peerDevice);
150
151 ASSERT_EQ(DistributedService::GetInstance().serviceQueue_, nullptr);
152 DistributedService::GetInstance().serviceQueue_ = serviceQueue;
153 }
154
155 /**
156 * @tc.name : DistributedServiceTest_00800
157 * @tc.number : DistributedServiceTest_00800
158 * @tc.desc : Test the OnCanceled function with a null notification.
159 */
160 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00800, Function | SmallTest | Level1)
161 {
162 std::shared_ptr<Notification> notificationNull = nullptr;
163 DistributedDeviceInfo peerDevice;
164 DistributedService::GetInstance().OnCanceled(notificationNull, peerDevice);
165 ASSERT_EQ(notificationNull, nullptr);
166 }
167
168 /**
169 * @tc.name : DistributedServiceTest_00900
170 * @tc.number : DistributedServiceTest_00900
171 * @tc.desc : Test the OnCanceled function with a valid notification and null peer device.
172 */
173 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00900, Function | SmallTest | Level1)
174 {
175 sptr<NotificationRequest> request = new NotificationRequest();
176 std::shared_ptr<Notification> notification = std::make_shared<Notification>(request);
177 notification->SetKey("notificationKey");
178 DistributedDeviceInfo peerDevice;
179 peerDevice.deviceId_ = 1;
180 peerDevice.deviceType_ = 1;
181 DistributedService::GetInstance().OnCanceled(notification, peerDevice);
182 ASSERT_NE(notification, nullptr);
183 }
184
185 /**
186 * @tc.name : DistributedServiceTest_01000
187 * @tc.number : DistributedServiceTest_01000
188 * @tc.desc : Test the OnCanceled function with a null notification.
189 */
190 HWTEST_F(DistributedServiceTest, DistributedServiceTest_01000, Function | SmallTest | Level1)
191 {
192 std::shared_ptr<Notification> notificationNull = nullptr;
193 std::string result = DistributedService::GetInstance().GetNotificationKey(notificationNull);
194 ASSERT_EQ(result, "");
195 }
196
197 /**
198 * @tc.name : DistributedServiceTest_01100
199 * @tc.number : DistributedServiceTest_01100
200 * @tc.desc : Test the OnCanceled function with a valid notification.
201 */
202 HWTEST_F(DistributedServiceTest, DistributedServiceTest_01100, Function | SmallTest | Level1)
203 {
204 sptr<NotificationRequest> request = new NotificationRequest();
205 std::shared_ptr<Notification> notification = std::make_shared<Notification>(request);
206 notification->SetKey("_notificationKey");
207 std::string result = DistributedService::GetInstance().GetNotificationKey(notification);
208 ASSERT_EQ(result, "ans_distributed_notificationKey");
209 }
210 } // namespace Notification
211 } // namespace OHOS
212