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