1 /*
2 * Copyright (c) 2021-2024 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 <gtest/gtest.h>
17 #include <iostream>
18
19 #define private public
20 #include "notification_subscriber.h"
21 #include "notification_subscriber_manager.h"
22 #include "mock_ans_subscriber.h"
23
24 #include "ans_inner_errors.h"
25 #include "ans_subscriber_listener.h"
26
27 using namespace testing::ext;
28 using namespace testing;
29
30 namespace OHOS {
31 namespace Notification {
32 class NotificationSubscriberManagerTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38
39 private:
40 class TestAnsSubscriber : public NotificationSubscriber {
41 public:
OnConnected()42 void OnConnected() override
43 {}
OnDisconnected()44 void OnDisconnected() override
45 {}
OnDied()46 void OnDied() override
47 {}
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)48 void OnEnabledNotificationChanged(
49 const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
50 {}
OnCanceled(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int deleteReason)51 void OnCanceled(const std::shared_ptr<Notification> &request,
52 const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason) override
53 {}
OnConsumed(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)54 void OnConsumed(const std::shared_ptr<Notification> &request,
55 const std::shared_ptr<NotificationSortingMap> &sortingMap) override
56 {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)57 void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
58 {}
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)59 void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
60 {}
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)61 void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override
62 {}
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)63 void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override
64 {}
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)65 void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>>
66 &requestList, const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
67 {}
OnApplicationInfoNeedChanged(const std::string & bundleName)68 void OnApplicationInfoNeedChanged(const std::string& bundleName) override
69 {}
70 };
71
72 static std::shared_ptr<NotificationSubscriberManager> notificationSubscriberManager_;
73 static TestAnsSubscriber testAnsSubscriber_;
74 static sptr<AnsSubscriberInterface> subscriber_;
75 };
76
77 std::shared_ptr<NotificationSubscriberManager> NotificationSubscriberManagerTest::notificationSubscriberManager_ =
78 nullptr;
79 sptr<AnsSubscriberInterface> NotificationSubscriberManagerTest::subscriber_ = nullptr;
80
SetUpTestCase()81 void NotificationSubscriberManagerTest::SetUpTestCase()
82 {
83 notificationSubscriberManager_ = NotificationSubscriberManager::GetInstance();
84 std::shared_ptr<NotificationSubscriber> testAnsSubscriber = std::make_shared<TestAnsSubscriber>();
85 subscriber_ = new (std::nothrow) SubscriberListener(testAnsSubscriber);
86 }
87
TearDownTestCase()88 void NotificationSubscriberManagerTest::TearDownTestCase()
89 {
90 subscriber_ = nullptr;
91 if (notificationSubscriberManager_ != nullptr) {
92 notificationSubscriberManager_->ResetFfrtQueue();
93 notificationSubscriberManager_ = nullptr;
94 }
95 }
96
SetUp()97 void NotificationSubscriberManagerTest::SetUp()
98 {
99 notificationSubscriberManager_->AddSubscriber(subscriber_, nullptr);
100 }
101
TearDown()102 void NotificationSubscriberManagerTest::TearDown()
103 {
104 notificationSubscriberManager_->RemoveSubscriber(subscriber_, nullptr);
105 }
106
107 /**
108 * @tc.number : NotificationSubscriberManagerTest_001
109 * @tc.name : ANS_AddSubscriber_0100
110 * @tc.desc : Test AddSubscriber function, return is ERR_OK.
111 */
112 HWTEST_F(NotificationSubscriberManagerTest, NotificationSubscriberManagerTest_001, Function | SmallTest | Level1)
113 {
114 // Test NotifyUpdated function.
115 const std::vector<NotificationSorting> sortingList;
116 sptr<NotificationSortingMap> map = new NotificationSortingMap(sortingList);
117 notificationSubscriberManager_->NotifyUpdated(map);
118
119 // Test AddSubscriber function.
120 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
121 info->AddAppName("test_bundle");
122 ASSERT_EQ(notificationSubscriberManager_->AddSubscriber(subscriber_, info), (int)ERR_OK);
123 ASSERT_EQ(notificationSubscriberManager_->AddSubscriber(subscriber_, nullptr), (int)ERR_OK);
124 }
125
126 /**
127 * @tc.number : NotificationSubscriberManagerTest_002
128 * @tc.name : ANS_AddSubscriber_0100
129 * @tc.desc : Test AddSubscriber function when subscriber is nullptr, return is ERR_ANS_INVALID_PARAM.
130 */
131 HWTEST_F(NotificationSubscriberManagerTest, NotificationSubscriberManagerTest_002, Function | SmallTest | Level1)
132 {
133 // Test NotifyDisturbModeChanged function.
134 sptr<NotificationDoNotDisturbDate> date =
135 new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
136 notificationSubscriberManager_->NotifyDoNotDisturbDateChanged(0, date);
137
138 // Test AddSubscriber function.
139 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
140 ASSERT_EQ(notificationSubscriberManager_->AddSubscriber(nullptr, info), (int)ERR_ANS_INVALID_PARAM);
141 }
142
143 /**
144 * @tc.number : NotificationSubscriberManagerTest_003
145 * @tc.name : ANS_RemoveSubscriber_0100
146 * @tc.desc : Test RemoveSubscriber function, return is ERR_OK.
147 */
148 HWTEST_F(NotificationSubscriberManagerTest, NotificationSubscriberManagerTest_003, Function | SmallTest | Level1)
149 {
150 // Test NotifyConsumed function.
151 std::vector<NotificationSorting> sortingList;
152 sptr<NotificationRequest> request = new NotificationRequest();
153 sptr<Notification> notification = new Notification(request);
154 sptr<NotificationSortingMap> notificationMap = new NotificationSortingMap(sortingList);
155 notificationSubscriberManager_->NotifyConsumed(notification, notificationMap);
156
157 // Test RemoveSubscriber function.
158 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
159 info->AddAppName("test_bundle");
160 ASSERT_EQ(notificationSubscriberManager_->RemoveSubscriber(subscriber_, info), (int)ERR_OK);
161 }
162
163 /**
164 * @tc.number : NotificationSubscriberManagerTest_004
165 * @tc.name : ANS_AddSubscriber_0100
166 * @tc.desc : Test RemoveSubscriber function when subscriber is nullptr, return is ERR_ANS_INVALID_PARAM.
167 */
168 HWTEST_F(NotificationSubscriberManagerTest, NotificationSubscriberManagerTest_004, Function | SmallTest | Level1)
169 {
170 // Test NotifyCanceled function.
171 std::vector<NotificationSorting> sortingList;
172 sptr<NotificationRequest> request = new NotificationRequest();
173 sptr<Notification> notification = new Notification(request);
174 sptr<NotificationSortingMap> notificationMap = new NotificationSortingMap(sortingList);
175 int deleteReason = 0;
176 notificationSubscriberManager_->NotifyCanceled(notification, notificationMap, deleteReason);
177
178 // Test RemoveSubscriber function.
179 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
180 ASSERT_EQ(notificationSubscriberManager_->RemoveSubscriber(nullptr, info), (int)ERR_ANS_INVALID_PARAM);
181 }
182
183 /**
184 * @tc.number : RegisterOnSubscriberAddCallbackTest_001
185 * @tc.name : RegisterOnSubscriberAddCallback and callback is not nullptr
186 * @tc.desc : Test RegisterOnSubscriberAddCallback .
187 */
OnSubscriberAddFake(const std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> & recode)188 void OnSubscriberAddFake(const std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> &recode) {}
189 HWTEST_F(NotificationSubscriberManagerTest, RegisterOnSubscriberAddCallbackTest_001, Function | SmallTest | Level1)
190 {
191 std::function<void(const std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> &)> callback =
192 std::bind(OnSubscriberAddFake, std::placeholders::_1);
193
194 notificationSubscriberManager_->RegisterOnSubscriberAddCallback(callback);
195 EXPECT_NE(notificationSubscriberManager_->onSubscriberAddCallback_, nullptr);
196 }
197
198 /**
199 * @tc.number : RegisterOnSubscriberAddCallbackTest_002
200 * @tc.name : RegisterOnSubscriberAddCallback and callback is nullptr
201 * @tc.desc : Test RegisterOnSubscriberAddCallback .
202 */
203 HWTEST_F(NotificationSubscriberManagerTest, RegisterOnSubscriberAddCallbackTest_002, Function | SmallTest | Level1)
204 {
205 std::function<void(const std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> &)> callback =
206 std::bind(OnSubscriberAddFake, std::placeholders::_1);
207 notificationSubscriberManager_->RegisterOnSubscriberAddCallback(callback);
208 EXPECT_NE(notificationSubscriberManager_->onSubscriberAddCallback_, nullptr);
209
210 // if callback exist, re-register a nullptr func will fail.
211 notificationSubscriberManager_->RegisterOnSubscriberAddCallback(nullptr);
212 EXPECT_NE(notificationSubscriberManager_->onSubscriberAddCallback_, nullptr);
213 }
214
215 /**
216 * @tc.number : UnRegisterOnSubscriberAddCallbackTest_001
217 * @tc.name : UnRegisterOnSubscriberAddCallback
218 * @tc.desc : Test UnRegisterOnSubscriberAddCallback .
219 */
220 HWTEST_F(NotificationSubscriberManagerTest, UnRegisterOnSubscriberAddCallbackTest_001, Function | SmallTest | Level1)
221 {
222 std::function<void(const std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> &)> callback =
223 std::bind(OnSubscriberAddFake, std::placeholders::_1);
224 notificationSubscriberManager_->RegisterOnSubscriberAddCallback(callback);
225 EXPECT_NE(notificationSubscriberManager_->onSubscriberAddCallback_, nullptr);
226
227 notificationSubscriberManager_->UnRegisterOnSubscriberAddCallback();
228 ASSERT_EQ(notificationSubscriberManager_->onSubscriberAddCallback_, nullptr);
229 }
230
231 /**
232 * @tc.number : BatchNotifyConsumedInner_001
233 * @tc.name : BatchNotifyConsumedInner
234 * @tc.desc : Test BatchNotifyConsumedInner .
235 */
236 HWTEST_F(NotificationSubscriberManagerTest, BatchNotifyConsumedInner_001, Function | SmallTest | Level1)
237 {
238 sptr<MockAnsSubscriber> mockSubscriber = new MockAnsSubscriber();
239 EXPECT_CALL(*mockSubscriber, OnConsumedList(_, _)).Times(1);
240
241 sptr<NotificationRequest> request = new NotificationRequest();
242 request->SetOwnerBundleName("test");
243 sptr<Notification> notification = new Notification(request);
244
245 std::vector<sptr<OHOS::Notification::Notification>> notifications;
246 notifications.emplace_back(notification);
247 sptr<NotificationSortingMap> notificationMap = new (std::nothrow) NotificationSortingMap();
248
249 std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> record =
250 notificationSubscriberManager_->CreateSubscriberRecord(mockSubscriber);
251 const sptr<NotificationSubscribeInfo> subscribeInfo = new NotificationSubscribeInfo();
252 subscribeInfo->AddAppName("test");
253 subscribeInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
254 notificationSubscriberManager_->AddRecordInfo(record, subscribeInfo);
255 notificationSubscriberManager_->BatchNotifyConsumedInner(notifications, notificationMap, record);
256 }
257
258 /**
259 * @tc.number : BatchNotifyConsumedInner_002
260 * @tc.name : BatchNotifyConsumedInner and params is invalid
261 * @tc.desc : Test BatchNotifyConsumedInner .
262 */
263 HWTEST_F(NotificationSubscriberManagerTest, BatchNotifyConsumedInner_002, Function | SmallTest | Level1)
264 {
265 sptr<MockAnsSubscriber> mockSubscriber = new MockAnsSubscriber();
266 EXPECT_CALL(*mockSubscriber, OnConsumedList(_, _)).Times(0);
267 std::vector<sptr<OHOS::Notification::Notification>> notifications;
268 notificationSubscriberManager_->BatchNotifyConsumedInner(notifications, nullptr, nullptr);
269 }
270
271 /**
272 * @tc.number : BatchNotifyConsumedInner_003
273 * @tc.name : BatchNotifyConsumedInner and subscriber isn't subscribed to this notification
274 * @tc.desc : Test BatchNotifyConsumedInner .
275 */
276 HWTEST_F(NotificationSubscriberManagerTest, BatchNotifyConsumedInner_003, Function | SmallTest | Level1)
277 {
278 sptr<MockAnsSubscriber> mockSubscriber = new MockAnsSubscriber();
279 EXPECT_CALL(*mockSubscriber, OnConsumedList(_, _)).Times(0);
280
281 sptr<NotificationRequest> request = new NotificationRequest();
282 request->SetOwnerBundleName("test");
283 sptr<Notification> notification = new Notification(request);
284
285 std::vector<sptr<OHOS::Notification::Notification>> notifications;
286 notifications.emplace_back(notification);
287 sptr<NotificationSortingMap> notificationMap = new (std::nothrow) NotificationSortingMap();
288
289 std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> record =
290 notificationSubscriberManager_->CreateSubscriberRecord(mockSubscriber);
291 const sptr<NotificationSubscribeInfo> subscribeInfo = new NotificationSubscribeInfo();
292 subscribeInfo->AddAppName("test_1");
293 subscribeInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
294 notificationSubscriberManager_->AddRecordInfo(record, subscribeInfo);
295 notificationSubscriberManager_->BatchNotifyConsumedInner(notifications, notificationMap, record);
296 }
297
298 /**
299 * @tc.number : BatchNotifyConsumed_001
300 * @tc.name : BatchNotifyConsumed and params is nullptr
301 * @tc.desc : Test BatchNotifyConsumed .
302 */
303 HWTEST_F(NotificationSubscriberManagerTest, BatchNotifyConsumed_001, Function | SmallTest | Level1)
304 {
305 std::vector<sptr<OHOS::Notification::Notification>> notifications;
306 sptr<NotificationSortingMap> notificationMap = new NotificationSortingMap();
307 sptr<MockAnsSubscriber> mockSubscriber = new MockAnsSubscriber();
308 auto record = notificationSubscriberManager_->CreateSubscriberRecord(mockSubscriber);
309 notificationSubscriberManager_->BatchNotifyConsumed(notifications, notificationMap, record);
310
311 sptr<NotificationRequest> request = new NotificationRequest();
312 sptr<Notification> notification = new Notification(request);
313 notifications.emplace_back(notification);
314 notificationSubscriberManager_->notificationSubQueue_ = nullptr;
315 notificationSubscriberManager_->BatchNotifyConsumed(notifications, notificationMap, record);
316 EXPECT_NE(notificationSubscriberManager_, nullptr);
317 }
318
319 /**
320 * @tc.number : OnRemoteDied_001
321 * @tc.name : OnRemoteDied and params is nullptr
322 * @tc.desc : Test OnRemoteDied .
323 */
324 HWTEST_F(NotificationSubscriberManagerTest, OnRemoteDied_001, Function | SmallTest | Level1)
325 {
326 notificationSubscriberManager_->notificationSubQueue_ = nullptr;
327 wptr<IRemoteObject> obj = nullptr;
328 notificationSubscriberManager_->OnRemoteDied(obj);
329 EXPECT_NE(notificationSubscriberManager_, nullptr);
330 }
331 } // namespace Notification
332 } // namespace OHOS
333