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 #undef private
23 #include "remove_box.h"
24
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Notification {
29
30 class DistributedServiceTest : public testing::Test {
31 public:
32 void SetUp() override;
33 void TearDown() override;
34 };
35
SetUp()36 void DistributedServiceTest::SetUp() {}
37
TearDown()38 void DistributedServiceTest::TearDown() {}
39
40 /**
41 * @tc.name : DistributedServiceTest_00100
42 * @tc.number : DistributedServiceTest_00100
43 * @tc.desc : Test the RemoveNotification function with a null boxMessage.
44 */
45 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00100, Function | SmallTest | Level1)
46 {
47 std::shared_ptr<TlvBox> boxMessage = nullptr;
48 DistributedService::GetInstance().RemoveNotification(boxMessage);
49 ASSERT_EQ(boxMessage, nullptr);
50 }
51
52 /**
53 * @tc.name : DistributedServiceTest_00200
54 * @tc.number : DistributedServiceTest_00200
55 * @tc.desc : Test the RemoveNotification function with a valid NotificationRemoveBox.
56 */
57 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00200, Function | SmallTest | Level1)
58 {
59 NotificationRemoveBox removeBox;
60 std::string notificationKey = "notificationId";
61 removeBox.SetNotificationHashCode(notificationKey);
62 DistributedService::GetInstance().RemoveNotification(removeBox.box_);
63 ASSERT_NE(removeBox.box_, nullptr);
64 }
65
66 /**
67 * @tc.name : DistributedServiceTest_00300
68 * @tc.number : DistributedServiceTest_00300
69 * @tc.desc : Test the RemoveNotification function with empty notificationKey.
70 */
71 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00300, Function | SmallTest | Level1)
72 {
73 std::shared_ptr<TlvBox> boxMessage = nullptr;
74 DistributedService::GetInstance().RemoveNotifications(boxMessage);
75 ASSERT_EQ(boxMessage, nullptr);
76 }
77
78 /**
79 * @tc.name : DistributedServiceTest_00400
80 * @tc.number : DistributedServiceTest_00400
81 * @tc.desc : Test the OnBatchCanceled function with an valid notifications.
82 */
83 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00400, Function | SmallTest | Level1)
84 {
85 BatchRemoveNotificationBox batchRemoveBox;
86 std::string hashCodes = "notificationId1 notificationId2";
87 batchRemoveBox.SetNotificationHashCode(hashCodes);
88 DistributedService::GetInstance().RemoveNotifications(batchRemoveBox.box_);
89 ASSERT_NE(batchRemoveBox.box_, nullptr);
90 }
91
92 /**
93 * @tc.name : DistributedServiceTest_00500
94 * @tc.number : DistributedServiceTest_00500
95 * @tc.desc : Test the OnBatchCanceled function with a null serviceQueue_.
96 */
97 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00500, Function | SmallTest | Level1)
98 {
99 std::vector<std::shared_ptr<Notification>> notifications;
100 DistributedDeviceInfo peerDevice;
101
102 std::shared_ptr<ffrt::queue> serviceQueue = DistributedService::GetInstance().serviceQueue_;
103 DistributedService::GetInstance().serviceQueue_ = nullptr;
104 DistributedService::GetInstance().OnBatchCanceled(notifications, peerDevice);
105
106 ASSERT_EQ(DistributedService::GetInstance().serviceQueue_, nullptr);
107 DistributedService::GetInstance().serviceQueue_ = serviceQueue;
108 }
109
110 /**
111 * @tc.name : DistributedServiceTest_00600
112 * @tc.number : DistributedServiceTest_00600
113 * @tc.desc : Test the OnBatchCanceled function with valid notifications and peer device.
114 */
115 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00600, Function | SmallTest | Level1)
116 {
117 std::vector<std::shared_ptr<Notification>> notifications;
118 DistributedDeviceInfo peerDevice;
119 peerDevice.deviceId_ = 1;
120 peerDevice.deviceType_ = 1;
121
122 std::shared_ptr<Notification> notificationNull = nullptr;
123 sptr<NotificationRequest> request = new NotificationRequest();
124 std::shared_ptr<Notification> notification = std::make_shared<Notification>(request);
125 notification->SetKey("notificationKey");
126 notifications.push_back(notificationNull);
127 notifications.push_back(notification);
128 DistributedService::GetInstance().OnBatchCanceled(notifications, peerDevice);
129 ASSERT_EQ((notificationNull == nullptr && notification != nullptr), true);
130 }
131
132 /**
133 * @tc.name : DistributedServiceTest_00700
134 * @tc.number : DistributedServiceTest_00700
135 * @tc.desc : Test the OnCanceled function with a valid notification and peer device.
136 */
137 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00700, Function | SmallTest | Level1)
138 {
139 sptr<NotificationRequest> request = new NotificationRequest();
140 std::shared_ptr<Notification> notification = std::make_shared<Notification>(request);
141 notification->SetKey("notificationKey");
142 DistributedDeviceInfo peerDevice;
143 peerDevice.deviceId_ = 1;
144 peerDevice.deviceType_ = 1;
145
146 std::shared_ptr<ffrt::queue> serviceQueue = DistributedService::GetInstance().serviceQueue_;
147 DistributedService::GetInstance().serviceQueue_ = nullptr;
148 DistributedService::GetInstance().OnCanceled(notification, peerDevice);
149
150 ASSERT_EQ(DistributedService::GetInstance().serviceQueue_, nullptr);
151 DistributedService::GetInstance().serviceQueue_ = serviceQueue;
152 }
153
154 /**
155 * @tc.name : DistributedServiceTest_00800
156 * @tc.number : DistributedServiceTest_00800
157 * @tc.desc : Test the OnCanceled function with a null notification.
158 */
159 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00800, Function | SmallTest | Level1)
160 {
161 std::shared_ptr<Notification> notificationNull = nullptr;
162 DistributedDeviceInfo peerDevice;
163 DistributedService::GetInstance().OnCanceled(notificationNull, peerDevice);
164 ASSERT_EQ(notificationNull, nullptr);
165 }
166
167 /**
168 * @tc.name : DistributedServiceTest_00900
169 * @tc.number : DistributedServiceTest_00900
170 * @tc.desc : Test the OnCanceled function with a valid notification and null peer device.
171 */
172 HWTEST_F(DistributedServiceTest, DistributedServiceTest_00900, Function | SmallTest | Level1)
173 {
174 sptr<NotificationRequest> request = new NotificationRequest();
175 std::shared_ptr<Notification> notification = std::make_shared<Notification>(request);
176 notification->SetKey("notificationKey");
177 DistributedDeviceInfo peerDevice;
178 peerDevice.deviceId_ = 1;
179 peerDevice.deviceType_ = 1;
180 DistributedService::GetInstance().OnCanceled(notification, peerDevice);
181 ASSERT_NE(notification, nullptr);
182 }
183
184 /**
185 * @tc.name : DistributedServiceTest_01000
186 * @tc.number : DistributedServiceTest_01000
187 * @tc.desc : Test the OnCanceled function with a null notification.
188 */
189 HWTEST_F(DistributedServiceTest, DistributedServiceTest_01000, Function | SmallTest | Level1)
190 {
191 std::shared_ptr<Notification> notificationNull = nullptr;
192 std::string result = DistributedService::GetInstance().GetNotificationKey(notificationNull);
193 ASSERT_EQ(result, "");
194 }
195
196 /**
197 * @tc.name : DistributedServiceTest_01100
198 * @tc.number : DistributedServiceTest_01100
199 * @tc.desc : Test the OnCanceled function with a valid notification.
200 */
201 HWTEST_F(DistributedServiceTest, DistributedServiceTest_01100, Function | SmallTest | Level1)
202 {
203 sptr<NotificationRequest> request = new NotificationRequest();
204 std::shared_ptr<Notification> notification = std::make_shared<Notification>(request);
205 notification->SetKey("_notificationKey");
206 std::string result = DistributedService::GetInstance().GetNotificationKey(notification);
207 ASSERT_EQ(result, "ans_distributed_notificationKey");
208 }
209 } // namespace Notification
210 } // namespace OHOS