1 /*
2  * Copyright (c) 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 <chrono>
17 #include <functional>
18 #include <memory>
19 #include <thread>
20 
21 #include "gtest/gtest.h"
22 
23 #define private public
24 #include "advanced_notification_service.h"
25 #include "ans_const_define.h"
26 #include "ans_inner_errors.h"
27 #include "ans_log_wrapper.h"
28 #include "ans_ut_constant.h"
29 #include "iremote_object.h"
30 #include "ipc_skeleton.h"
31 #include "want_agent_info.h"
32 #include "want_agent_helper.h"
33 #include "want_params.h"
34 #include "int_wrapper.h"
35 #include "accesstoken_kit.h"
36 #include "notification_preferences.h"
37 #include "notification_constant.h"
38 #include "notification_record.h"
39 #include "notification_subscriber.h"
40 #include "refbase.h"
41 
42 using namespace testing::ext;
43 using namespace OHOS::Media;
44 using namespace OHOS::Security::AccessToken;
45 
46 namespace OHOS {
47 namespace Notification {
48 extern void MockIsVerfyPermisson(bool isVerify);
49 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
50 extern void MockIsSystemApp(bool isSystemApp);
51 
52 class AnsUtilsTest : public testing::Test {
53 public:
54     static void SetUpTestCase();
55     static void TearDownTestCase();
56     void SetUp();
57     void TearDown();
58 
59 private:
60     void TestAddNotification(int notificationId, const sptr<NotificationBundleOption> &bundle);
61 
62 private:
63     static sptr<AdvancedNotificationService> advancedNotificationService_;
64 };
65 
66 sptr<AdvancedNotificationService> AnsUtilsTest::advancedNotificationService_ = nullptr;
67 
SetUpTestCase()68 void AnsUtilsTest::SetUpTestCase() {}
69 
TearDownTestCase()70 void AnsUtilsTest::TearDownTestCase() {}
71 
SetUp()72 void AnsUtilsTest::SetUp()
73 {
74     GTEST_LOG_(INFO) << "SetUp start";
75 
76     advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService();
77     NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
78     advancedNotificationService_->CancelAll(0);
79     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
80     MockIsSystemApp(true);
81     GTEST_LOG_(INFO) << "SetUp end";
82 }
83 
TearDown()84 void AnsUtilsTest::TearDown()
85 {
86     advancedNotificationService_ = nullptr;
87     GTEST_LOG_(INFO) << "TearDown";
88 }
89 
SleepForFC()90 inline void SleepForFC()
91 {
92     // For ANS Flow Control
93     std::this_thread::sleep_for(std::chrono::seconds(1));
94 }
95 
96 class TestAnsSubscriber : public NotificationSubscriber {
97 public:
OnDied()98     void OnDied() override
99     {}
OnConnected()100     void OnConnected() override
101     {}
OnDisconnected()102     void OnDisconnected() override
103     {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)104     void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
105     {}
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)106     void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
107     {}
OnCanceled(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)108     void OnCanceled(const std::shared_ptr<Notification> &request,
109         const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
110     {}
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)111     void OnEnabledNotificationChanged(
112         const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
113     {}
OnConsumed(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)114     void OnConsumed(const std::shared_ptr<Notification> &request,
115         const std::shared_ptr<NotificationSortingMap> &sortingMap) override
116     {}
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)117     void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override
118     {}
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)119     void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override
120     {}
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)121     void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>>
122         &requestList, const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
123     {}
124 };
125 
TestAddNotification(int notificationId,const sptr<NotificationBundleOption> & bundle)126 void AnsUtilsTest::TestAddNotification(int notificationId, const sptr<NotificationBundleOption> &bundle)
127 {
128     auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
129     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
130     request->SetSlotType(slotType);
131     request->SetOwnerUserId(1);
132     request->SetCreatorUserId(2);
133     request->SetOwnerBundleName("test");
134     request->SetOwnerUid(0);
135     request->SetNotificationId(notificationId);
136     auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
137     auto ret = advancedNotificationService_->AssignToNotificationList(record);
138 }
139 
140 /**
141  * @tc.name: FillRequestByKeys_00001
142  * @tc.desc: Test FillRequestByKeys
143  * @tc.type: FUNC
144  * @tc.require: issue
145  */
146 HWTEST_F(AnsUtilsTest, FillRequestByKeys_00001, Function | SmallTest | Level1)
147 {
148     auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
149     sptr<NotificationRequest> oldRequest = new (std::nothrow) NotificationRequest();
150     oldRequest->SetSlotType(slotType);
151     auto liveContent = std::make_shared<NotificationLiveViewContent>();
152     auto extraInfo = std::make_shared<AAFwk::WantParams>();
153     sptr<AAFwk::IInterface> value = AAFwk::Integer::Box(1);
154     extraInfo->SetParam("key1", value);
155     auto content = std::make_shared<NotificationContent>(liveContent);
156     liveContent->SetExtraInfo(extraInfo);
157     oldRequest->SetContent(content);
158 
159     std::vector<std::string> keys = {"key1"};
160     sptr<NotificationRequest> newRequest;
161     ErrCode ret = advancedNotificationService_->FillRequestByKeys(oldRequest, keys, newRequest);
162     ASSERT_EQ(ret, (int)ERR_OK);
163 }
164 
165 /**
166  * @tc.name: IsAllowedGetNotificationByFilter_00001
167  * @tc.desc: Test IsAllowedGetNotificationByFilter
168  * @tc.type: FUNC
169  * @tc.require: issue
170  */
171 HWTEST_F(AnsUtilsTest, IsAllowedGetNotificationByFilter_00001, Function | SmallTest | Level1)
172 {
173     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
174     auto record = std::make_shared<NotificationRecord>();
175     record->bundleOption = new NotificationBundleOption("test", 1);
176     int ret = advancedNotificationService_->IsAllowedGetNotificationByFilter(record, bundle);
177     ASSERT_EQ(ret, (int)ERR_OK);
178 
179     record->bundleOption->SetBundleName("bundleName");
180     ret = advancedNotificationService_->IsAllowedGetNotificationByFilter(record, bundle);
181     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
182 }
183 
184 /**
185  * @tc.name: GetActiveNotificationByFilter_00001
186  * @tc.desc: Test GetActiveNotificationByFilter
187  * @tc.type: FUNC
188  * @tc.require: issue
189  */
190 HWTEST_F(AnsUtilsTest, GetActiveNotificationByFilter_00001, Function | SmallTest | Level1)
191 {
192     AdvancedNotificationService ans;
193     ans.notificationSvrQueue_ = nullptr;
194     std::string label = "testLabel";
195     std::vector<std::string> keys = {"key1"};
196     sptr<NotificationRequest> newRequest;
197     auto bundleOption = new NotificationBundleOption("test", 1);
198     int notificationId = 1;
199     ASSERT_EQ(ans.GetActiveNotificationByFilter(bundleOption, notificationId, label, keys, newRequest),
200         (int)ERR_ANS_INVALID_PARAM);
201 }
202 
203 /**
204  * @tc.name: GetActiveNotificationByFilter_00002
205  * @tc.desc: Test GetActiveNotificationByFilter
206  * @tc.type: FUNC
207  * @tc.require: issue
208  */
209 HWTEST_F(AnsUtilsTest, GetActiveNotificationByFilter_00002, Function | SmallTest | Level1)
210 {
211     std::string label = "testLabel";
212     std::vector<std::string> keys = {"key1"};
213     sptr<NotificationRequest> newRequest;
214     sptr<NotificationBundleOption> bundle;
215     int notificationId = 1;
216     ASSERT_EQ(advancedNotificationService_->GetActiveNotificationByFilter(
217         bundle, notificationId, label, keys, newRequest),
218         (int)ERR_ANS_INVALID_BUNDLE);
219 }
220 
221 /**
222  * @tc.name: GetActiveNotificationByFilter_00003
223  * @tc.desc: Test GetActiveNotificationByFilter
224  * @tc.type: FUNC
225  * @tc.require: issue
226  */
227 HWTEST_F(AnsUtilsTest, GetActiveNotificationByFilter_00003, Function | SmallTest | Level1)
228 {
229     auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
230     std::string label = "testLabel";
231     int notificationId = 1;
232 
233     sptr<NotificationRequest> oldRequest = new (std::nothrow) NotificationRequest();
234     oldRequest->SetSlotType(slotType);
235     auto liveContent = std::make_shared<NotificationLiveViewContent>();
236     liveContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE);
237     auto extraInfo = std::make_shared<AAFwk::WantParams>();
238     sptr<AAFwk::IInterface> value = AAFwk::Integer::Box(1);
239     extraInfo->SetParam("key1", value);
240     liveContent->SetExtraInfo(extraInfo);
241     auto content = std::make_shared<NotificationContent>(liveContent);
242     oldRequest->SetContent(content);
243     oldRequest->SetNotificationId(notificationId);
244 
245     oldRequest->SetLabel(label);
246     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
247     auto record = advancedNotificationService_->MakeNotificationRecord(oldRequest, bundle);
248     advancedNotificationService_->AssignToNotificationList(record);
249 
250 
251     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
252     MockIsSystemApp(true);
253     MockIsVerfyPermisson(false);
254 
255     std::vector<std::string> keys;
256     sptr<NotificationRequest> newRequest;
257     ASSERT_EQ(advancedNotificationService_->GetActiveNotificationByFilter(bundle,
258         notificationId, label, keys, newRequest), (int)ERR_ANS_PERMISSION_DENIED);
259 
260     MockIsVerfyPermisson(true);
261     ASSERT_EQ(advancedNotificationService_->GetActiveNotificationByFilter(bundle,
262         notificationId, label, keys, newRequest), (int)ERR_OK);
263 
264     keys.emplace_back("test1");
265     ASSERT_EQ(advancedNotificationService_->GetActiveNotificationByFilter(bundle,
266         notificationId, label, keys, newRequest), (int)ERR_OK);
267 }
268 
269 /**
270  * @tc.name: RecentNotificationDump_00001
271  * @tc.desc: Test RecentNotificationDump
272  * @tc.type: FUNC
273  * @tc.require: issue
274  */
275 HWTEST_F(AnsUtilsTest, RecentNotificationDump_00001, Function | SmallTest | Level1)
276 {
277     auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
278     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
279     request->SetSlotType(slotType);
280     request->SetOwnerUserId(1);
281     request->SetCreatorUserId(1);
282     request->SetOwnerBundleName("test");
283     request->SetOwnerUid(0);
284     request->SetNotificationId(1);
285     auto notification = new (std::nothrow) Notification(request);
286 
287     auto recentNotification = std::make_shared<AdvancedNotificationService::RecentNotification>();
288     recentNotification->isActive = true;
289     recentNotification->notification = notification;
290     advancedNotificationService_->recentInfo_->list.emplace_front(recentNotification);
291 
292     std::vector<std::string> dumpInfo;
293     int ret = advancedNotificationService_->RecentNotificationDump("test", 1, 1, dumpInfo);
294     ASSERT_EQ(ret, (int)ERR_OK);
295     ASSERT_EQ(dumpInfo.size(), 1);
296 }
297 
298 /**
299  * @tc.name: GetLocalNotificationKeys_00001
300  * @tc.desc: Test GetLocalNotificationKeys
301  * @tc.type: FUNC
302  * @tc.require: issue
303  */
304 HWTEST_F(AnsUtilsTest, GetLocalNotificationKeys_00001, Function | SmallTest | Level1)
305 {
306     int notificationId = 1;
307     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
308     TestAddNotification(notificationId, bundle);
309 
310     notificationId = 2;
311     sptr<NotificationBundleOption> bundle2 = new NotificationBundleOption("test1", 2);
312     TestAddNotification(notificationId, bundle2);
313     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 2);
314 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
315     auto keys = advancedNotificationService_->GetLocalNotificationKeys(bundle2);
316     ASSERT_EQ(keys.size(), 1);
317 #endif
318 }
319 
320 /**
321  * @tc.name: OnBundleDataCleared_00001
322  * @tc.desc: Test OnBundleDataCleared
323  * @tc.type: FUNC
324  * @tc.require: issue
325  */
326 HWTEST_F(AnsUtilsTest, OnBundleDataCleared_00001, Function | SmallTest | Level1)
327 {
328     int notificationId = 1;
329     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
330     TestAddNotification(notificationId, bundle);
331 
332     advancedNotificationService_->OnBundleDataCleared(bundle);
333 }
334 
335 /**
336  * @tc.name: InitNotificationEnableList_00001
337  * @tc.desc: Test InitNotificationEnableList
338  * @tc.type: FUNC
339  * @tc.require: issue
340  */
341 // HWTEST_F(AnsUtilsTest, InitNotificationEnableList_00001, Function | SmallTest | Level1)
342 // {
343 //     advancedNotificationService_->InitNotificationEnableList();
344 // }
345 
346 /**
347  * @tc.name: GetBundleInfoByNotificationBundleOption_00001
348  * @tc.desc: Test GetBundleInfoByNotificationBundleOption
349  * @tc.type: FUNC
350  * @tc.require: issue
351  */
352 // HWTEST_F(AnsUtilsTest, GetBundleInfoByNotificationBundleOption_00001, Function | SmallTest | Level1)
353 // {
354 //     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
355 //     AppExecFwk::BundleInfo bundleInfo;
356 //     bool res = advancedNotificationService_->GetBundleInfoByNotificationBundleOption(bundle, bundleInfo);
357 //     ASSERT_EQ(res, true);
358 // }
359 
360 /**
361  * @tc.name: OnBundleRemoved_00001
362  * @tc.desc: Test OnBundleRemoved
363  * @tc.type: FUNC
364  * @tc.require: issue
365  */
366 HWTEST_F(AnsUtilsTest, OnBundleRemoved_00001, Function | SmallTest | Level1)
367 {
368     int notificationId = 1;
369     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
370     TestAddNotification(notificationId, bundle);
371     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
372 
373     advancedNotificationService_->OnBundleRemoved(bundle);
374     SleepForFC();
375     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 0);
376 }
377 
378 /**
379  * @tc.name: OnBundleDataAdd_00001
380  * @tc.desc: Test OnBundleDataAdd
381  * @tc.type: FUNC
382  * @tc.require: issue
383  */
384 HWTEST_F(AnsUtilsTest, OnBundleDataAdd_00001, Function | SmallTest | Level1)
385 {
386     int notificationId = 1;
387     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
388     TestAddNotification(notificationId, bundle);
389 
390     advancedNotificationService_->OnBundleDataAdd(bundle);
391     SleepForFC();
392     bool enable = false;
393     NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundle, enable);
394     ASSERT_EQ(enable, false);
395 }
396 
397 /**
398  * @tc.name: OnBundleDataUpdate_00001
399  * @tc.desc: Test OnBundleDataUpdate
400  * @tc.type: FUNC
401  * @tc.require: issue
402  */
403 HWTEST_F(AnsUtilsTest, OnBundleDataUpdate_00001, Function | SmallTest | Level1)
404 {
405     int notificationId = 1;
406     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
407     TestAddNotification(notificationId, bundle);
408 
409     NotificationPreferences::GetInstance()->SetHasPoppedDialog(bundle, true);
410     advancedNotificationService_->OnBundleDataUpdate(bundle);
411 }
412 }  // namespace Notification
413 }  // namespace OHOS
414