• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 <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 "advanced_datashare_helper.h"
26 #include "ability_manager_errors.h"
27 #include "ans_inner_errors.h"
28 #include "ans_log_wrapper.h"
29 #include "accesstoken_kit.h"
30 #include "notification_preferences.h"
31 #include "notification_constant.h"
32 #include "ans_ut_constant.h"
33 #include "ans_dialog_host_client.h"
34 #include "mock_parameters.h"
35 #include "mock_push_callback_stub.h"
36 #include "mock_ipc_skeleton.h"
37 #include "bool_wrapper.h"
38 #include "string_wrapper.h"
39 #include "want_params.h"
40 #include "int_wrapper.h"
41 #include "os_account_manager_helper.h"
42 
43 extern void MockIsOsAccountExists(bool exists);
44 extern void MockGetOsAccountLocalIdFromUid(bool mockRet, uint8_t mockCase);
45 extern void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase);
46 
47 using namespace testing::ext;
48 using namespace OHOS::Security::AccessToken;
49 
50 namespace OHOS {
51 namespace Notification {
52 extern void MockIsVerfyPermisson(bool isVerify);
53 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
54 extern void MockIsSystemApp(bool isSystemApp);
55 extern void MockIsAtomicServiceByFullTokenID(bool isAtomicService);
56 class AnsPublishServiceTest : public testing::Test {
57 public:
58     static void SetUpTestCase();
59     static void TearDownTestCase();
60     void SetUp();
61     void TearDown();
62 
63 private:
64     void TestAddNotification(int notificationId, const sptr<NotificationBundleOption> &bundle);
65     void RegisterPushCheck();
66 
67 private:
68     static sptr<AdvancedNotificationService> advancedNotificationService_;
69 };
70 
71 sptr<AdvancedNotificationService> AnsPublishServiceTest::advancedNotificationService_ = nullptr;
72 
SetUpTestCase()73 void AnsPublishServiceTest::SetUpTestCase() {}
74 
TearDownTestCase()75 void AnsPublishServiceTest::TearDownTestCase() {}
76 
SetUp()77 void AnsPublishServiceTest::SetUp()
78 {
79     GTEST_LOG_(INFO) << "SetUp start";
80 
81     advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService();
82     NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
83     advancedNotificationService_->CancelAll("");
84     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
85     MockIsSystemApp(true);
86     GTEST_LOG_(INFO) << "SetUp end";
87 }
88 
TearDown()89 void AnsPublishServiceTest::TearDown()
90 {
91     delete advancedNotificationService_;
92     advancedNotificationService_ = nullptr;
93     GTEST_LOG_(INFO) << "TearDown";
94 }
95 
TestAddNotification(int notificationId,const sptr<NotificationBundleOption> & bundle)96 void AnsPublishServiceTest::TestAddNotification(int notificationId, const sptr<NotificationBundleOption> &bundle)
97 {
98     auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
99     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
100     request->SetSlotType(slotType);
101     request->SetOwnerUserId(1);
102     request->SetCreatorUserId(1);
103     request->SetOwnerBundleName("test");
104     request->SetOwnerUid(0);
105     request->SetNotificationId(notificationId);
106     auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
107     auto ret = advancedNotificationService_->AssignToNotificationList(record);
108 }
109 
RegisterPushCheck()110 void AnsPublishServiceTest::RegisterPushCheck()
111 {
112     auto pushCallbackProxy = new (std::nothrow)MockPushCallBackStub();
113     EXPECT_NE(pushCallbackProxy, nullptr);
114     sptr<IRemoteObject> pushCallback = pushCallbackProxy->AsObject();
115     sptr<NotificationCheckRequest> checkRequest = new (std::nothrow) NotificationCheckRequest();
116     checkRequest->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
117     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
118     MockIsSystemApp(true);
119     MockIsVerfyPermisson(true);
120     ASSERT_EQ(advancedNotificationService_->RegisterPushCallback(pushCallback, checkRequest), ERR_OK);
121 }
122 
123 /**
124  * @tc.name: Publish_00001
125  * @tc.desc: Test Publish
126  * @tc.type: FUNC
127  * @tc.require: issue
128  */
129 HWTEST_F(AnsPublishServiceTest, Publish_00001, Function | SmallTest | Level1)
130 {
131     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
132     std::string label = "";
133     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
134     auto localLiveContent = std::make_shared<NotificationLocalLiveViewContent>();
135     auto content = std::make_shared<NotificationContent>(localLiveContent);
136     request->SetContent(content);
137     request->SetCreatorUid(1);
138     request->SetOwnerUid(1);
139     MockIsOsAccountExists(true);
140 
141     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
142     MockIsSystemApp(false);
143     auto ret = advancedNotificationService_->Publish(label, request);
144     ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
145 }
146 
147 /**
148  * @tc.name: Publish_00002
149  * @tc.desc: Test Publish
150  * @tc.type: FUNC
151  * @tc.require: issue
152  */
153 HWTEST_F(AnsPublishServiceTest, Publish_00002, Function | SmallTest | Level1)
154 {
155     ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(std::string(),
156         new NotificationBundleOption("bundleName", 1), true), (int)ERR_OK);
157     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
158     std::string label = "";
159     request->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
160     request->SetRemoveAllowed(false);
161     request->SetInProgress(true);
162     auto normalContent = std::make_shared<NotificationNormalContent>();
163     auto content = std::make_shared<NotificationContent>(normalContent);
164     request->SetContent(content);
165 
166     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
167     MockIsSystemApp(true);
168     MockIsVerfyPermisson(false);
169 
170     auto ret = advancedNotificationService_->Publish(label, request);
171     ASSERT_EQ(ret, (int)ERR_OK);
172 }
173 
174 /**
175  * @tc.name: Publish_00006
176  * @tc.desc: Publish test receiver user and checkUserExists is false
177  * @tc.type: FUNC
178  * @tc.require: issue
179  */
180 HWTEST_F(AnsPublishServiceTest, Publish_00006, Function | SmallTest | Level1)
181 {
182     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
183     std::string label = "";
184     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
185     request->SetNotificationId(1);
186     request->SetReceiverUserId(101);
187     auto liveContent = std::make_shared<NotificationLiveViewContent>();
188     auto content = std::make_shared<NotificationContent>(liveContent);
189     request->SetContent(content);
190     MockIsOsAccountExists(false);
191 
192     auto ret = advancedNotificationService_->Publish(label, request);
193     ASSERT_EQ(ret, (int)ERROR_USER_NOT_EXIST);
194 }
195 
196 /**
197  * @tc.name: Publish_00007
198  * @tc.desc: Test Publish
199  * @tc.type: FUNC
200  * @tc.require: issue
201  */
202 HWTEST_F(AnsPublishServiceTest, Publish_00007, Function | SmallTest | Level1)
203 {
204     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
205     std::string label = "";
206     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
207     request->SetOwnerUid(1);
208     request->SetIsAgentNotification(true);
209     request->SetIsDoNotDisturbByPassed(true);
210     MockIsOsAccountExists(true);
211 
212     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
213     MockIsSystemApp(false);
214     auto ret = advancedNotificationService_->Publish(label, request);
215     ASSERT_EQ(ret, (int)ERR_OK);
216 }
217 
218 /**
219  * @tc.name: Publish_00008
220  * @tc.desc: Test Publish
221  * @tc.type: FUNC
222  * @tc.require: issue
223  */
224 HWTEST_F(AnsPublishServiceTest, Publish_00008, Function | SmallTest | Level1)
225 {
226     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
227     std::string label = "";
228     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
229     request->SetOwnerUid(1);
230     request->SetCreatorUid(1);
231     request->SetCreatorUserId(100);
232     request->SetIsAgentNotification(true);
233     MockIsOsAccountExists(true);
234 
235     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
236     MockIsSystemApp(false);
237     auto ret = advancedNotificationService_->Publish(label, request);
238     ASSERT_EQ(ret, (int)ERR_OK);
239 }
240 
241 /**
242  * @tc.name: Publish_00009
243  * @tc.desc: Test Publish
244  * @tc.type: FUNC
245  * @tc.require: issue
246  */
247 HWTEST_F(AnsPublishServiceTest, Publish_00009, Function | SmallTest | Level1)
248 {
249     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
250     std::string label = "";
251     auto normalContent = std::make_shared<NotificationNormalContent>();
252     auto content = std::make_shared<NotificationContent>(normalContent);
253     request->SetContent(content);
254     MockIsAtomicServiceByFullTokenID(true);
255 
256     auto ret = advancedNotificationService_->Publish(label, request);
257     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
258     ret = advancedNotificationService_->PublishNotificationForIndirectProxy(request);
259     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
260 
261     MockIsAtomicServiceByFullTokenID(false);
262 }
263 
264 /**
265  * @tc.name: DeleteByBundle_00001
266  * @tc.desc: Test DeleteByBundle
267  * @tc.type: FUNC
268  * @tc.require: issue
269  */
270 HWTEST_F(AnsPublishServiceTest, DeleteByBundle_00001, Function | SmallTest | Level1)
271 {
272     MockIsVerfyPermisson(true);
273     sptr<NotificationBundleOption> bundleOption = nullptr;
274     auto ret = advancedNotificationService_->DeleteByBundle(bundleOption);
275     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
276 }
277 
278 /**
279  * @tc.name: DeleteByBundle_00002
280  * @tc.desc: Test DeleteByBundle
281  * @tc.type: FUNC
282  * @tc.require: issue
283  */
284 HWTEST_F(AnsPublishServiceTest, DeleteByBundle_00002, Function | SmallTest | Level1)
285 {
286     sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
287     advancedNotificationService_->notificationSvrQueue_ = nullptr;
288     auto ret = advancedNotificationService_->DeleteByBundle(bundle);
289     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
290 }
291 
292 /**
293  * @tc.name: DeleteByBundle_00003
294  * @tc.desc: Test DeleteByBundle
295  * @tc.type: FUNC
296  * @tc.require: issue
297  */
298 HWTEST_F(AnsPublishServiceTest, DeleteByBundle_00003, Function | SmallTest | Level1)
299 {
300     auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
301     TestAddNotification(1, bundle);
302     auto ret = advancedNotificationService_->DeleteByBundle(bundle);
303     ASSERT_EQ(ret, (int)ERR_OK);
304     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 0);
305 }
306 
307 /**
308  * @tc.name: DeleteByBundle_00004
309  * @tc.desc: Test DeleteByBundle,
310  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
311  *  2. Non-subsystem (HAP token) and system app -> return ERR_OK
312  *  3. Subsystem (Native token) and non-system app -> return ERR_OK
313  *  4. Subsystem (Native token) and system app -> return ERR_OK
314  * @tc.type: FUNC
315  * @tc.require: issue
316  */
317 HWTEST_F(AnsPublishServiceTest, DeleteByBundle_00004, Function | SmallTest | Level1)
318 {
319     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("bundleName", 1);
320     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
321     MockIsSystemApp(false);
322     auto result = advancedNotificationService_->DeleteByBundle(bundle);
323     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
324 
325     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
326     MockIsSystemApp(true);
327     result = advancedNotificationService_->DeleteByBundle(bundle);
328     ASSERT_EQ(result, ERR_OK);
329 
330     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
331     MockIsSystemApp(false);
332     result = advancedNotificationService_->DeleteByBundle(bundle);
333     ASSERT_EQ(result, ERR_OK);
334 
335     MockIsSystemApp(true);
336     result = advancedNotificationService_->DeleteByBundle(bundle);
337     ASSERT_EQ(result, ERR_OK);
338 }
339 
340 /**
341  * @tc.name: DeleteAll_00001
342  * @tc.desc: Test DeleteAll
343  * @tc.type: FUNC
344  * @tc.require: issue
345  */
346 HWTEST_F(AnsPublishServiceTest, DeleteAll_00001, Function | SmallTest | Level1)
347 {
348     advancedNotificationService_->notificationSvrQueue_ = nullptr;
349     auto ret = advancedNotificationService_->DeleteAll();
350     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
351 }
352 
353 /**
354  * @tc.name: RemoveDistributedNotifications_00001
355  * @tc.desc: delete distributed notificaitons test permission and param
356  * @tc.type: FUNC
357  * @tc.require: issue
358  */
359 HWTEST_F(AnsPublishServiceTest, RemoveDistributedNotifications_00001, Function | SmallTest | Level1)
360 {
361     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
362 
363     MockIsSystemApp(false);
364     std::vector<std::string> hashcodes;
365     auto ret = advancedNotificationService_->RemoveDistributedNotifications(
366         hashcodes, 99, 99, 99);
367     ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
368 
369     MockIsSystemApp(true);
370     MockIsVerfyPermisson(false);
371     ret = advancedNotificationService_->RemoveDistributedNotifications(
372         hashcodes, 99, 99, 99);
373     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
374 
375     MockIsSystemApp(true);
376     MockIsVerfyPermisson(true);
377     advancedNotificationService_->notificationSvrQueue_ = nullptr;
378     ret = advancedNotificationService_->RemoveDistributedNotifications(
379         hashcodes, 99, 99, 99);
380     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
381 }
382 
383 
384 /**
385  * @tc.name: RemoveDistributedNotifications_00002
386  * @tc.desc: delete distributed notificaitons test permission and param
387  * @tc.type: FUNC
388  * @tc.require: issue
389  */
390 HWTEST_F(AnsPublishServiceTest, RemoveDistributedNotifications_00002, Function | SmallTest | Level1)
391 {
392     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
393 
394     MockIsSystemApp(true);
395     MockIsVerfyPermisson(true);
396     std::vector<std::string> hashcodes;
397     auto ret = advancedNotificationService_->RemoveDistributedNotifications(
398         hashcodes, 99, NotificationConstant::DistributedDeleteType::ALL, 99);
399     ASSERT_EQ(ret, (int)ERR_OK);
400 
401     ret = advancedNotificationService_->RemoveDistributedNotifications(
402         hashcodes, 99, NotificationConstant::DistributedDeleteType::SLOT, 99);
403     ASSERT_EQ(ret, (int)ERR_OK);
404 
405     ret = advancedNotificationService_->RemoveDistributedNotifications(
406         hashcodes, 99, NotificationConstant::DistributedDeleteType::HASHCODES, 99);
407     ASSERT_EQ(ret, (int)ERR_OK);
408 }
409 
410 /**
411  * @tc.name: RemoveDistributedNotifications_00003
412  * @tc.desc: delete distributed notificaitons test permission and param
413  * @tc.type: FUNC
414  * @tc.require: issue
415  */
416 HWTEST_F(AnsPublishServiceTest, RemoveDistributedNotifications_00003, Function | SmallTest | Level1)
417 {
418     sptr<NotificationRequest> request(new (std::nothrow) NotificationRequest());
419     request->SetDistributedCollaborate(true);
420     sptr<Notification> notification(new (std::nothrow) Notification(request));
421     auto record = std::make_shared<NotificationRecord>();
422     record->request = request;
423     record->notification = notification;
424     advancedNotificationService_->notificationList_.push_back(record);
425     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
426 
427     sptr<NotificationRequest> request1(new (std::nothrow) NotificationRequest());
428     request1->SetLabel("123");
429     sptr<Notification> notification1(new (std::nothrow) Notification(request1));
430     auto record1 = std::make_shared<NotificationRecord>();
431     record1->request = request1;
432     record1->notification = notification1;
433     advancedNotificationService_->notificationList_.push_back(record1);
434     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 2);
435 
436     std::vector<std::string> hashcodes;
437     hashcodes.push_back(notification->GetKey());
438     auto ret = advancedNotificationService_->RemoveDistributedNotifications(
439         hashcodes, 99);
440     std::this_thread::sleep_for(std::chrono::milliseconds(1000));
441     ASSERT_EQ(ret, (int)ERR_OK);
442     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
443 }
444 
445 /**
446  * @tc.name: RemoveDistributedNotifications_00004
447  * @tc.desc: delete distributed notificaitons test permission and param
448  * @tc.type: FUNC
449  * @tc.require: issue
450  */
451 HWTEST_F(AnsPublishServiceTest, RemoveDistributedNotifications_00004, Function | SmallTest | Level1)
452 {
453     sptr<NotificationRequest> request(new (std::nothrow) NotificationRequest());
454     request->SetDistributedCollaborate(true);
455     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
456     sptr<Notification> notification(new (std::nothrow) Notification(request));
457     auto record = std::make_shared<NotificationRecord>();
458     record->request = request;
459     record->notification = notification;
460     advancedNotificationService_->notificationList_.push_back(record);
461     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
462 
463     sptr<NotificationRequest> request1(new (std::nothrow) NotificationRequest());
464     request1->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
465     sptr<Notification> notification1(new (std::nothrow) Notification(request1));
466     auto record1 = std::make_shared<NotificationRecord>();
467     record1->request = request1;
468     record1->notification = notification1;
469     advancedNotificationService_->notificationList_.push_back(record1);
470     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 2);
471 
472     std::vector<std::string> hashcodes;
473     hashcodes.push_back(notification->GetKey());
474     auto ret = advancedNotificationService_->RemoveDistributedNotifications(
475         NotificationConstant::SlotType::LIVE_VIEW, 99,
476         NotificationConstant::DistributedDeleteType::SLOT);
477     std::this_thread::sleep_for(std::chrono::milliseconds(1000));
478     ASSERT_EQ(ret, (int)ERR_OK);
479     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
480 }
481 
482 /**
483  * @tc.name: RemoveDistributedNotifications_00005
484  * @tc.desc: delete distributed notificaitons test permission and param
485  * @tc.type: FUNC
486  * @tc.require: issue
487  */
488 HWTEST_F(AnsPublishServiceTest, RemoveDistributedNotifications_00005, Function | SmallTest | Level1)
489 {
490     sptr<NotificationRequest> request(new (std::nothrow) NotificationRequest());
491     request->SetDistributedCollaborate(true);
492     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
493     sptr<Notification> notification(new (std::nothrow) Notification(request));
494     auto record = std::make_shared<NotificationRecord>();
495     record->request = request;
496     record->notification = notification;
497     advancedNotificationService_->notificationList_.push_back(record);
498     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
499 
500     sptr<NotificationRequest> request1(new (std::nothrow) NotificationRequest());
501     request1->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
502     request1->SetDistributedCollaborate(true);
503     sptr<Notification> notification1(new (std::nothrow) Notification(request1));
504     auto record1 = std::make_shared<NotificationRecord>();
505     record1->request = request1;
506     record1->notification = notification1;
507     advancedNotificationService_->notificationList_.push_back(record1);
508     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 2);
509 
510     std::vector<std::string> hashcodes;
511     hashcodes.push_back(notification->GetKey());
512     auto ret = advancedNotificationService_->RemoveDistributedNotifications(
513         NotificationConstant::SlotType::LIVE_VIEW, 99,
514         NotificationConstant::DistributedDeleteType::EXCLUDE_ONE_SLOT);
515     std::this_thread::sleep_for(std::chrono::milliseconds(1000));
516     ASSERT_EQ(ret, (int)ERR_OK);
517     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
518 }
519 
520 /**
521  * @tc.name: RemoveAllDistributedNotifications_00001
522  * @tc.desc: delete ALL distributed notificaitons
523  * @tc.type: FUNC
524  * @tc.require: issue
525  */
526 HWTEST_F(AnsPublishServiceTest, RemoveAllDistributedNotifications_00001, Function | SmallTest | Level1)
527 {
528     sptr<NotificationRequest> request(new (std::nothrow) NotificationRequest());
529     request->SetDistributedCollaborate(true);
530     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
531     sptr<Notification> notification(new (std::nothrow) Notification(request));
532     auto record = std::make_shared<NotificationRecord>();
533     record->request = request;
534     record->notification = notification;
535     advancedNotificationService_->notificationList_.push_back(record);
536     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
537 
538     sptr<NotificationRequest> request1(new (std::nothrow) NotificationRequest());
539     request1->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
540     sptr<Notification> notification1(new (std::nothrow) Notification(request1));
541     auto record1 = std::make_shared<NotificationRecord>();
542     record1->request = request1;
543     record1->notification = notification1;
544     advancedNotificationService_->notificationList_.push_back(record1);
545     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 2);
546 
547     std::vector<std::string> hashcodes;
548     hashcodes.push_back(notification->GetKey());
549     auto ret = advancedNotificationService_->RemoveAllDistributedNotifications(99);
550     std::this_thread::sleep_for(std::chrono::milliseconds(1000));
551     ASSERT_EQ(ret, (int)ERR_OK);
552     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
553 }
554 
555 /**
556  * @tc.name: ExecuteDeleteDistributedNotification_00001
557  * @tc.desc: delete ALL distributed notificaitons
558  * @tc.type: FUNC
559  * @tc.require: issue
560  */
561 HWTEST_F(AnsPublishServiceTest, ExecuteDeleteDistributedNotification_00001, Function | SmallTest | Level1)
562 {
563     std::vector<sptr<Notification>> notifications;
564     std::shared_ptr<NotificationRecord> record;
565     auto res = advancedNotificationService_->ExecuteDeleteDistributedNotification(
566         record, notifications, 99);
567     ASSERT_FALSE(res);
568     ASSERT_EQ(notifications.size(), 0);
569 
570     record = std::make_shared<NotificationRecord>();
571     res = advancedNotificationService_->ExecuteDeleteDistributedNotification(
572         record, notifications, 99);
573     ASSERT_FALSE(res);
574     ASSERT_EQ(notifications.size(), 0);
575 
576     sptr<NotificationRequest> request(new (std::nothrow) NotificationRequest());
577     request->SetDistributedCollaborate(true);
578     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
579     sptr<Notification> notification(new (std::nothrow) Notification(request));
580 
581     record->request = request;
582     record->notification = notification;
583     advancedNotificationService_->notificationList_.push_back(record);
584     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
585 
586     res = advancedNotificationService_->ExecuteDeleteDistributedNotification(
587         record, notifications, 99);
588     ASSERT_TRUE(res);
589     ASSERT_EQ(notifications.size(), 1);
590 }
591 
592 /**
593  * @tc.name: IsDistributedNotification_00001
594  * @tc.desc: delete ALL distributed notificaitons
595  * @tc.type: FUNC
596  * @tc.require: issue
597  */
598 HWTEST_F(AnsPublishServiceTest, IsDistributedNotification_00001, Function | SmallTest | Level1)
599 {
600     auto res = advancedNotificationService_->IsDistributedNotification(nullptr);
601     ASSERT_FALSE(res);
602 
603     sptr<NotificationRequest> request(new (std::nothrow) NotificationRequest());
604     request->SetDistributedCollaborate(true);
605     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
606     res = advancedNotificationService_->IsDistributedNotification(request);
607     ASSERT_TRUE(res);
608 
609     request->SetDistributedCollaborate(false);
610     res = advancedNotificationService_->IsDistributedNotification(request);
611     ASSERT_FALSE(res);
612 }
613 
614 /**
615  * @tc.name: DeleteAll_00002
616  * @tc.desc: Test DeleteAll,
617  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
618  *  2. Non-subsystem (HAP token) and system app -> return ERR_OK
619  *  3. Subsystem (Native token) and non-system app -> return ERR_OK
620  *  4. Subsystem (Native token) and system app -> return ERR_OK
621  * @tc.type: FUNC
622  * @tc.require: issue
623  */
624 HWTEST_F(AnsPublishServiceTest, DeleteAll_00002, Function | SmallTest | Level1)
625 {
626     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
627     MockIsSystemApp(false);
628     auto result = advancedNotificationService_->DeleteAll();
629     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
630 
631     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
632     MockIsSystemApp(true);
633     result = advancedNotificationService_->DeleteAll();
634     ASSERT_EQ(result, ERR_OK);
635 
636     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
637     MockIsSystemApp(false);
638     result = advancedNotificationService_->DeleteAll();
639     ASSERT_EQ(result, ERR_OK);
640 
641     MockIsSystemApp(true);
642     result = advancedNotificationService_->DeleteAll();
643     ASSERT_EQ(result, ERR_OK);
644 }
645 
646 /**
647  * @tc.name: DeleteAll_00003
648  * @tc.desc: Test DeleteAll when GetCurrentActiveUserId faild, except is ERR_OK
649  * @tc.type: FUNC
650  * @tc.require: issue
651  */
652 HWTEST_F(AnsPublishServiceTest, DeleteAll_00003, Function | SmallTest | Level1)
653 {
654     MockGetOsAccountLocalIdFromUid(false, 1);
655     auto ret = advancedNotificationService_->DeleteAll();
656     ASSERT_EQ(ret, (int)ERR_OK);
657 }
658 
659 /**
660  * @tc.name: SetShowBadgeEnabledForBundle_00001
661  * @tc.desc: Test SetShowBadgeEnabledForBundle
662  * @tc.type: FUNC
663  * @tc.require: issue
664  */
665 HWTEST_F(AnsPublishServiceTest, SetShowBadgeEnabledForBundle_00001, Function | SmallTest | Level1)
666 {
667     sptr<NotificationBundleOption> bundleOption = nullptr;
668     auto ret = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundleOption, true);
669     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
670 
671     bool enabled = false;
672     ret = advancedNotificationService_->GetShowBadgeEnabledForBundle(bundleOption, enabled);
673     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
674 }
675 
676 /**
677  * @tc.name: SetShowBadgeEnabledForBundle_00002
678  * @tc.desc: Test SetShowBadgeEnabledForBundle
679  * @tc.type: FUNC
680  * @tc.require: issue
681  */
682 HWTEST_F(AnsPublishServiceTest, SetShowBadgeEnabledForBundle_00002, Function | SmallTest | Level1)
683 {
684     sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
685     advancedNotificationService_->notificationSvrQueue_ = nullptr;
686     auto ret = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundle, true);
687     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
688 
689     bool enabled = false;
690     ret = advancedNotificationService_->GetShowBadgeEnabledForBundle(bundle, enabled);
691     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
692 }
693 
694 /**
695  * @tc.name: SetShowBadgeEnabledForBundle_00003
696  * @tc.desc: Test SetShowBadgeEnabledForBundle,
697  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
698  *  2. Non-subsystem (HAP token) and system app -> return ERR_OK
699  *  3. Subsystem (Native token) and non-system app -> return ERR_OK
700  *  4. Subsystem (Native token) and system app -> return ERR_OK
701  * @tc.type: FUNC
702  * @tc.require: issue
703  */
704 HWTEST_F(AnsPublishServiceTest, SetShowBadgeEnabledForBundle_00003, Function | SmallTest | Level1)
705 {
706     sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
707 
708     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
709     MockIsSystemApp(false);
710     bool enabled = true;
711     auto result = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundle, enabled);
712     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
713     result = advancedNotificationService_->GetShowBadgeEnabledForBundle(bundle, enabled);
714     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
715 
716     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
717     MockIsSystemApp(true);
718     result = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundle, enabled);
719     ASSERT_EQ(result, ERR_OK);
720     result = advancedNotificationService_->GetShowBadgeEnabledForBundle(bundle, enabled);
721     ASSERT_EQ(result, ERR_OK);
722 
723     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
724     MockIsSystemApp(false);
725     result = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundle, enabled);
726     ASSERT_EQ(result, ERR_OK);
727     result = advancedNotificationService_->GetShowBadgeEnabledForBundle(bundle, enabled);
728     ASSERT_EQ(result, ERR_OK);
729 
730     MockIsSystemApp(true);
731     result = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundle, enabled);
732     ASSERT_EQ(result, ERR_OK);
733     result = advancedNotificationService_->GetShowBadgeEnabledForBundle(bundle, enabled);
734     ASSERT_EQ(result, ERR_OK);
735 }
736 
737 /**
738  * @tc.name: GetShowBadgeEnabled_00001
739  * @tc.desc: Test GetShowBadgeEnabled
740  * @tc.type: FUNC
741  * @tc.require: issue
742  */
743 HWTEST_F(AnsPublishServiceTest, GetShowBadgeEnabled_00001, Function | SmallTest | Level1)
744 {
745     advancedNotificationService_->notificationSvrQueue_ = nullptr;
746     bool enabled = false;
747     auto ret = advancedNotificationService_->GetShowBadgeEnabled(enabled);
748     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
749 }
750 
751 /**
752  * @tc.name: GetShowBadgeEnabled_00002
753  * @tc.desc: Test GetShowBadgeEnabled
754  * @tc.type: FUNC
755  * @tc.require: issue
756  */
757 HWTEST_F(AnsPublishServiceTest, GetShowBadgeEnabled_00002, Function | SmallTest | Level1)
758 {
759     bool enabled = true;
760     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
761     MockIsSystemApp(true);
762     MockIsVerfyPermisson(true);
763     auto ret = advancedNotificationService_->GetShowBadgeEnabled(enabled);
764     ASSERT_EQ(ret, (int)ERR_OK);
765     ASSERT_EQ(enabled, true);
766 }
767 
768 /**
769  * @tc.name: RequestEnableNotification_00001
770  * @tc.desc: Test RequestEnableNotification
771  * @tc.type: FUNC
772  * @tc.require: issue
773  */
774 HWTEST_F(AnsPublishServiceTest, RequestEnableNotification_00001, Function | SmallTest | Level1)
775 {
776     std::string deviceId = "deviceId";
777     sptr<AnsDialogHostClient> client = nullptr;
778     AnsDialogHostClient::CreateIfNullptr(client);
779     client = AnsDialogHostClient::GetInstance();
780     sptr<IRemoteObject> callerToken = nullptr;
781 
782     auto ret = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), false);
783     ASSERT_EQ(ret, (int)ERR_OK);
784 
785     ret = advancedNotificationService_->RequestEnableNotification(deviceId, client, callerToken);
786     ASSERT_EQ(ret, (int)ERROR_INTERNAL_ERROR);
787 }
788 
789 /**
790  * @tc.name: RequestEnableNotification_00002
791  * @tc.desc: Test RequestEnableNotification
792  * @tc.type: FUNC
793  * @tc.require: issue
794  */
795 HWTEST_F(AnsPublishServiceTest, RequestEnableNotification_00002, Function | SmallTest | Level1)
796 {
797     std::string deviceId = "deviceId";
798     sptr<AnsDialogHostClient> client = nullptr;
799     AnsDialogHostClient::CreateIfNullptr(client);
800     client = AnsDialogHostClient::GetInstance();
801     sptr<IRemoteObject> callerToken = nullptr;
802 
803     auto ret = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true);
804     ASSERT_EQ(ret, (int)ERR_OK);
805 
806     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
807     ret = advancedNotificationService_->RequestEnableNotification(deviceId, client, callerToken);
808     ASSERT_EQ(ret, (int)ERR_OK);
809 }
810 
811 /**
812  * @tc.name: RequestEnableNotification_00003
813  * @tc.desc: Test RequestEnableNotification
814  * @tc.type: FUNC
815  * @tc.require: issue
816  */
817 HWTEST_F(AnsPublishServiceTest, RequestEnableNotification_00003, Function | SmallTest | Level1)
818 {
819     std::string deviceId = "deviceId";
820     sptr<AnsDialogHostClient> client = nullptr;
821     AnsDialogHostClient::CreateIfNullptr(client);
822     client = AnsDialogHostClient::GetInstance();
823     sptr<IRemoteObject> callerToken = nullptr;
824 
825     auto ret = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), false);
826     ASSERT_EQ(ret, (int)ERR_OK);
827 
828     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
829 
830     auto bundle = advancedNotificationService_->GenerateBundleOption();
831     NotificationPreferences::GetInstance()->SetHasPoppedDialog(bundle, true);
832 
833     ret = advancedNotificationService_->RequestEnableNotification(deviceId, client, callerToken);
834     ASSERT_EQ(ret, (int)ERR_ANS_NOT_ALLOWED);
835 
836     NotificationPreferences::GetInstance()->SetHasPoppedDialog(bundle, false);
837     ret = advancedNotificationService_->RequestEnableNotification(deviceId, client, callerToken);
838     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
839 }
840 
841 /**
842  * @tc.name: RequestEnableNotification_00004
843  * @tc.desc: Test RequestEnableNotification,two parameters,except is ERROR_INTERNAL_ERROR
844  * @tc.type: FUNC
845  * @tc.require: issue
846  */
847 HWTEST_F(AnsPublishServiceTest, RequestEnableNotification_00004, Function | SmallTest | Level1)
848 {
849     std::string bundleName = "bundleName1";
850     int32_t uid = 1;
851     auto ret = advancedNotificationService_->RequestEnableNotification(bundleName, uid);
852     ASSERT_EQ(ret, (int)ERROR_INTERNAL_ERROR);
853 }
854 
855 /**
856  * @tc.name: RequestEnableNotification_00005
857  * @tc.desc: Test RequestEnableNotification,
858  *  without permission, except is ERR_ANS_PERMISSION_DENIED
859  *  with permission, zyt, except is ERR_ANS_NOT_ALLOWED
860  *  with permission, easy, except is ERR_ANS_NOT_ALLOWED
861  * @tc.type: FUNC
862  * @tc.require: issue
863  */
864 HWTEST_F(AnsPublishServiceTest, RequestEnableNotification_00005, Function | SmallTest | Level1)
865 {
866     std::string bundleName = "com.zhuoyi.appstore.lite";
867     int32_t uid = 1;
868     MockIsVerfyPermisson(false);
869     auto ret = advancedNotificationService_->RequestEnableNotification(bundleName, uid);
870     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
871     MockIsVerfyPermisson(true);
872     ret = advancedNotificationService_->RequestEnableNotification(bundleName, uid);
873     ASSERT_EQ(ret, (int)ERR_ANS_NOT_ALLOWED);
874     bundleName = "com.easy.abroad";
875     ret = advancedNotificationService_->RequestEnableNotification(bundleName, uid);
876     ASSERT_EQ(ret, (int)ERR_ANS_NOT_ALLOWED);
877 }
878 
879 /**
880  * @tc.name: CommonRequestEnableNotification_00001
881  * @tc.desc: Test CommonRequestEnableNotification, when bundleOption is nullptr, except is ERROR_INTERNAL_ERROR
882  * @tc.type: FUNC
883  * @tc.require: issue
884  */
885 HWTEST_F(AnsPublishServiceTest, CommonRequestEnableNotification_00001, Function | SmallTest | Level1)
886 {
887     std::string deviceId = "";
888     sptr<NotificationBundleOption> bundleOption = nullptr;
889     bool innerLake = true;
890     auto ret = advancedNotificationService_->
891         CommonRequestEnableNotification(deviceId, nullptr, nullptr, bundleOption, innerLake, false);
892     ASSERT_EQ(ret, (int)ERROR_INTERNAL_ERROR);
893 }
894 
895 /**
896  * @tc.name: CommonRequestEnableNotification_00002
897  * @tc.desc: Test CommonRequestEnableNotification, when bundleOption is not nullptr,
898  *          IsAllowedNotifySelf is true, RequestEnableNotificationDailog falid,
899  *          except is ERROR_INTERNAL_ERROR
900  * @tc.type: FUNC
901  * @tc.require: issue
902  */
903 HWTEST_F(AnsPublishServiceTest, CommonRequestEnableNotification_00002, Function | SmallTest | Level1)
904 {
905     std::string deviceId = "";
906     sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
907     bool innerLake = true;
908     auto ret = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true);
909     ASSERT_EQ(ret, (int)ERR_OK);
910     ret = advancedNotificationService_->
911         CommonRequestEnableNotification(deviceId, nullptr, nullptr, bundle, innerLake, false);
912     ASSERT_EQ(ret, (int)ERR_OK);
913 }
914 
915 /**
916  * @tc.name: CommonRequestEnableNotification_00003
917  * @tc.desc: Test CommonRequestEnableNotification, when bundleOption is not nullptr,
918  *          IsAllowedNotifySelf is false, except is ERROR_INTERNAL_ERROR
919  * @tc.type: FUNC
920  * @tc.require: issue
921  */
922 HWTEST_F(AnsPublishServiceTest, CommonRequestEnableNotification_00003, Function | SmallTest | Level1)
923 {
924     std::string deviceId = "";
925     sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
926     bool innerLake = true;
927     auto ret = advancedNotificationService_->
928         CommonRequestEnableNotification(deviceId, nullptr, nullptr, bundle, innerLake, false);
929     ASSERT_EQ(ret, (int)ERROR_INTERNAL_ERROR);
930 }
931 
932 /**
933  * @tc.name: SetNotificationsEnabledForAllBundles_00001
934  * @tc.desc: Test SetNotificationsEnabledForAllBundles
935  * @tc.type: FUNC
936  * @tc.require: issue
937  */
938 HWTEST_F(AnsPublishServiceTest, SetNotificationsEnabledForAllBundles_00001, Function | SmallTest | Level1)
939 {
940     advancedNotificationService_->notificationSvrQueue_ = nullptr;
941     bool enabled = false;
942     auto ret = advancedNotificationService_->SetNotificationsEnabledForAllBundles("", enabled);
943     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
944 }
945 
946 /**
947  * @tc.name: SetNotificationsEnabledForAllBundles_00002
948  * @tc.desc: Test SetNotificationsEnabledForAllBundles,
949  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
950  *  2. Non-subsystem (HAP token) and system app -> return ERR_OK
951  *  3. Subsystem (Native token) and non-system app -> return ERR_OK
952  *  4. Subsystem (Native token) and system app -> return ERR_OK
953  *  5. GetcurrentActiveUserId faild -> return ERR_ANS_GET_ACTIVE_USER_FAILED
954  *  6. GetcurrentActiveUserId success -> return ERR_OK
955  * @tc.type: FUNC
956  * @tc.require: issue
957  */
958 HWTEST_F(AnsPublishServiceTest, SetNotificationsEnabledForAllBundles_00002, Function | SmallTest | Level1)
959 {
960     std::string deviceId = "";
961     bool enabled = true;
962     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
963     MockIsSystemApp(false);
964     auto result = advancedNotificationService_->SetNotificationsEnabledForAllBundles(deviceId, enabled);
965     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
966 
967     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
968     MockIsSystemApp(true);
969     result = advancedNotificationService_->SetNotificationsEnabledForAllBundles(deviceId, enabled);
970     ASSERT_EQ(result, ERR_OK);
971 
972     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
973     MockIsSystemApp(false);
974     result = advancedNotificationService_->SetNotificationsEnabledForAllBundles(deviceId, enabled);
975     ASSERT_EQ(result, ERR_OK);
976 
977     MockIsSystemApp(true);
978     result = advancedNotificationService_->SetNotificationsEnabledForAllBundles(deviceId, enabled);
979     ASSERT_EQ(result, ERR_OK);
980 
981     MockQueryForgroundOsAccountId(false, 1);
982     result = advancedNotificationService_->SetNotificationsEnabledForAllBundles(deviceId, enabled);
983     ASSERT_EQ(result, ERR_ANS_GET_ACTIVE_USER_FAILED);
984 
985     MockQueryForgroundOsAccountId(true, 1);
986     result = advancedNotificationService_->SetNotificationsEnabledForAllBundles(deviceId, enabled);
987     ASSERT_EQ(result, ERR_OK);
988 }
989 
990 /**
991  * @tc.name: SetNotificationsEnabledForSpecialBundle_00001
992  * @tc.desc: Test SetNotificationsEnabledForSpecialBundle
993  * @tc.type: FUNC
994  * @tc.require: issue
995  */
996 HWTEST_F(AnsPublishServiceTest, SetNotificationsEnabledForSpecialBundle_00001, Function | SmallTest | Level1)
997 {
998     sptr<NotificationBundleOption> bundle = nullptr;
999     bool enabled = false;
1000     std::string deviceId = "deviceId";
1001     auto ret = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(deviceId, bundle, enabled);
1002     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
1003 }
1004 
1005 /**
1006  * @tc.name: SetNotificationsEnabledForSpecialBundle_00002
1007  * @tc.desc: Test SetNotificationsEnabledForSpecialBundle,
1008  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
1009  *  2. Non-subsystem (HAP token) and system app -> return ERR_OK
1010  *  3. Subsystem (Native token) and non-system app -> return ERR_OK
1011  *  4. Subsystem (Native token) and system app -> return ERR_OK
1012  * @tc.type: FUNC
1013  * @tc.require: issue
1014  */
1015 HWTEST_F(AnsPublishServiceTest, SetNotificationsEnabledForSpecialBundle_00002, Function | SmallTest | Level1)
1016 {
1017     std::string deviceId = "";
1018     bool enabled = true;
1019     sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1020     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1021     MockIsSystemApp(false);
1022     auto result = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(deviceId, bundle, enabled);
1023     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
1024 
1025     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1026     MockIsSystemApp(true);
1027     result = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(deviceId, bundle, enabled);
1028     ASSERT_EQ(result, ERR_OK);
1029 
1030     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
1031     MockIsSystemApp(false);
1032     result = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(deviceId, bundle, enabled);
1033     ASSERT_EQ(result, ERR_OK);
1034 
1035     MockIsSystemApp(true);
1036     result = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(deviceId, bundle, enabled);
1037     ASSERT_EQ(result, ERR_OK);
1038 }
1039 
1040 /**
1041  * @tc.name: SetNotificationsEnabledForSpecialBundle_00003
1042  * @tc.desc: Test SetNotificationsEnabledForSpecialBundle,
1043  *  1. callingUid is not ans uid, and without permission -> return ERR_ANS_PERMISSION_DENIED
1044  *  2. callingUid is not ans uid, and with permission -> return ERR_OK
1045  *  3. callingUid is ans uid, and without permission -> return ERR_OK
1046  *  4. callingUid is ans uid, and with permission -> return ERR_OK
1047  * @tc.type: FUNC
1048  * @tc.require: issue
1049  */
1050 HWTEST_F(AnsPublishServiceTest, SetNotificationsEnabledForSpecialBundle_00003, Function | SmallTest | Level1)
1051 {
1052     std::string deviceId = "";
1053     bool enabled = true;
1054     int32_t ansUid = 5523;
1055     sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1056     MockIsVerfyPermisson(false);
1057     IPCSkeleton::SetCallingUid(12345);
1058     auto result = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(deviceId, bundle, enabled);
1059     ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
1060 
1061     MockIsVerfyPermisson(true);
1062     IPCSkeleton::SetCallingUid(12345);
1063     result = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(deviceId, bundle, enabled);
1064     ASSERT_EQ(result, ERR_OK);
1065 
1066     MockIsVerfyPermisson(false);
1067     IPCSkeleton::SetCallingUid(ansUid);
1068     result = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(deviceId, bundle, enabled);
1069     ASSERT_EQ(result, ERR_OK);
1070 
1071     MockIsSystemApp(true);
1072     result = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(deviceId, bundle, enabled);
1073     ASSERT_EQ(result, ERR_OK);
1074 }
1075 
1076 /**
1077  * @tc.name: IsAllowedNotify_00001
1078  * @tc.desc: Test IsAllowedNotify, when notificationSvrQueue_ is nullptr
1079  *  without permission
1080  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
1081  *  2. Non-subsystem (HAP token) and system app -> return ERR_ANS_PERMISSION_DENIED
1082  *  3. Subsystem (Native token) and non-system app -> return ERR_ANS_PERMISSION_DENIED
1083  *  4. Subsystem (Native token) and system app -> return ERR_ANS_PERMISSION_DENIED
1084  *  with permission
1085  *  5. GetcurrentActiveUserId faild -> return ERR_ANS_GET_ACTIVE_USER_FAILED
1086  *  6. GetcurrentActiveUserId success -> return ERR_ANS_INVALID_PARAM
1087  * @tc.type: FUNC
1088  * @tc.require: issue
1089  */
1090 HWTEST_F(AnsPublishServiceTest, IsAllowedNotify_00001, Function | SmallTest | Level1)
1091 {
1092     bool enabled = true;
1093     advancedNotificationService_->notificationSvrQueue_ = nullptr;
1094     MockIsVerfyPermisson(false);
1095     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1096     MockIsSystemApp(false);
1097     auto result = advancedNotificationService_->IsAllowedNotify(enabled);
1098     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
1099 
1100     MockIsSystemApp(true);
1101     result = advancedNotificationService_->IsAllowedNotify(enabled);
1102     ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
1103 
1104     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
1105     MockIsSystemApp(false);
1106     result = advancedNotificationService_->IsAllowedNotify(enabled);
1107     ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
1108 
1109     MockIsSystemApp(true);
1110     result = advancedNotificationService_->IsAllowedNotify(enabled);
1111     ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
1112 
1113     MockIsVerfyPermisson(true);
1114     MockQueryForgroundOsAccountId(false, 1);
1115     result = advancedNotificationService_->IsAllowedNotify(enabled);
1116     ASSERT_EQ(result, ERR_ANS_GET_ACTIVE_USER_FAILED);
1117 
1118     MockQueryForgroundOsAccountId(true, 1);
1119     result = advancedNotificationService_->IsAllowedNotify(enabled);
1120     ASSERT_EQ(result, ERR_ANS_INVALID_PARAM);
1121 }
1122 
1123 /**
1124  * @tc.name: IsAllowedNotifyForBundle_00001
1125  * @tc.desc: Test IsAllowedNotifyForBundle
1126  * @tc.type: FUNC
1127  * @tc.require: issue
1128  */
1129 HWTEST_F(AnsPublishServiceTest, IsAllowedNotifyForBundle_00001, Function | SmallTest | Level1)
1130 {
1131     bool allowed = false;
1132     sptr<NotificationBundleOption> bundle = nullptr;
1133     auto ret = advancedNotificationService_->IsAllowedNotifyForBundle(bundle, allowed);
1134     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
1135 }
1136 
1137 /**
1138  * @tc.name: TriggerLocalLiveView_00001
1139  * @tc.desc: Test TriggerLocalLiveView
1140  * @tc.type: FUNC
1141  * @tc.require: issue
1142  */
1143 HWTEST_F(AnsPublishServiceTest, TriggerLocalLiveView_00001, Function | SmallTest | Level1)
1144 {
1145     int notificationId = 1;
1146     sptr<NotificationBundleOption> bundle = nullptr;
1147     sptr<NotificationButtonOption> buttonOption = nullptr;
1148 
1149     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1150     MockIsSystemApp(false);
1151     MockIsVerfyPermisson(false);
1152 
1153     auto ret = advancedNotificationService_->TriggerLocalLiveView(bundle, notificationId, buttonOption);
1154     ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
1155 
1156     MockIsSystemApp(true);
1157     ret = advancedNotificationService_->TriggerLocalLiveView(bundle, notificationId, buttonOption);
1158     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
1159 
1160     MockIsVerfyPermisson(true);
1161     ret = advancedNotificationService_->TriggerLocalLiveView(bundle, notificationId, buttonOption);
1162     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
1163 }
1164 
1165 /**
1166  * @tc.name: RemoveNotification_00001
1167  * @tc.desc: Test RemoveNotification
1168  * @tc.type: FUNC
1169  * @tc.require: issue
1170  */
1171 HWTEST_F(AnsPublishServiceTest, RemoveNotification_00001, Function | SmallTest | Level1)
1172 {
1173     advancedNotificationService_->notificationSvrQueue_ = nullptr;
1174     auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1175     std::string label = "label";
1176     int notificationId = 1;
1177     auto ret = advancedNotificationService_->RemoveNotification(bundle, notificationId, label, 0);
1178 
1179     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1180 }
1181 
1182 /**
1183  * @tc.name: RemoveNotifications_00001
1184  * @tc.desc: Test RemoveNotifications
1185  * @tc.type: FUNC
1186  * @tc.require: issue
1187  */
1188 HWTEST_F(AnsPublishServiceTest, RemoveNotifications_00001, Function | SmallTest | Level1)
1189 {
1190     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1191     MockIsSystemApp(false);
1192     std::vector<std::string> keys;
1193     int removeReason = 1;
1194     auto ret = advancedNotificationService_->RemoveNotifications(keys, removeReason);
1195     ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
1196 
1197     MockIsSystemApp(true);
1198     MockIsVerfyPermisson(false);
1199     ret = advancedNotificationService_->RemoveNotifications(keys, removeReason);
1200     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
1201 
1202     advancedNotificationService_->notificationSvrQueue_ = nullptr;
1203     MockIsVerfyPermisson(true);
1204     ret = advancedNotificationService_->RemoveNotifications(keys, removeReason);
1205     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1206 }
1207 
1208 /**
1209  * @tc.name: RemoveNotifications_00002
1210  * @tc.desc: Test RemoveNotifications
1211  * @tc.type: FUNC
1212  * @tc.require: issue
1213  */
1214 HWTEST_F(AnsPublishServiceTest, RemoveNotifications_00002, Function | SmallTest | Level1)
1215 {
1216     std::vector<std::string> keys;
1217     int removeReason = 1;
1218 
1219     auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1220     sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest();
1221     req->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1222     req->SetOwnerUserId(1);
1223     req->SetOwnerBundleName(TEST_DEFUALT_BUNDLE);
1224     req->SetNotificationId(1);
1225     auto record = advancedNotificationService_->MakeNotificationRecord(req, bundle);
1226     auto ret = advancedNotificationService_->AssignToNotificationList(record);
1227     keys.emplace_back(record->notification->GetKey());
1228 
1229     ret = advancedNotificationService_->RemoveNotifications(keys, removeReason);
1230     ASSERT_EQ(ret, (int)ERR_OK);
1231 }
1232 
1233 /**
1234  * @tc.name: RemoveNotificationBySlot_00001
1235  * @tc.desc: Test RemoveNotificationBySlot
1236  * @tc.type: FUNC
1237  * @tc.require: issue
1238  */
1239 HWTEST_F(AnsPublishServiceTest, RemoveNotificationBySlot_00001, Function | SmallTest | Level1)
1240 {
1241     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1242     MockIsSystemApp(false);
1243     sptr<NotificationBundleOption> bundle = nullptr;
1244     sptr<NotificationSlot> slot = nullptr;
1245     auto ret = advancedNotificationService_->RemoveNotificationBySlot(bundle, slot,
1246         NotificationConstant::DEFAULT_REASON_DELETE);
1247     ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
1248 
1249     MockIsSystemApp(true);
1250     MockIsVerfyPermisson(true);
1251     ret = advancedNotificationService_->RemoveNotificationBySlot(bundle, slot,
1252         NotificationConstant::DEFAULT_REASON_DELETE);
1253     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
1254 }
1255 
1256 /**
1257  * @tc.name: RemoveNotificationBySlot_00002
1258  * @tc.desc: Test RemoveNotificationBySlot
1259  * @tc.type: FUNC
1260  * @tc.require: issue
1261  */
1262 HWTEST_F(AnsPublishServiceTest, RemoveNotificationBySlot_00002, Function | SmallTest | Level1)
1263 {
1264     auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1265     sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest();
1266     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1267     req->SetOwnerUserId(1);
1268     req->SetOwnerBundleName(TEST_DEFUALT_BUNDLE);
1269     req->SetNotificationId(1);
1270     auto multiLineContent = std::make_shared<NotificationMultiLineContent>();
1271     auto content = std::make_shared<NotificationContent>(multiLineContent);
1272     req->SetContent(content);
1273     auto record = advancedNotificationService_->MakeNotificationRecord(req, bundle);
1274     auto ret = advancedNotificationService_->AssignToNotificationList(record);
1275     auto slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1276 
1277     ret = advancedNotificationService_->RemoveNotificationBySlot(bundle, slot,
1278         NotificationConstant::DEFAULT_REASON_DELETE);
1279     ASSERT_EQ(ret, (int)ERR_OK);
1280 }
1281 
1282 /**
1283  * @tc.name: NotificationSvrQueue_00001
1284  * @tc.desc: Test notificationSvrQueue is nullptr
1285  * @tc.type: FUNC
1286  * @tc.require: issue
1287  */
1288 HWTEST_F(AnsPublishServiceTest, NotificationSvrQueue_00001, Function | SmallTest | Level1)
1289 {
1290     MockIsSystemApp(true);
1291     MockIsVerfyPermisson(true);
1292     advancedNotificationService_->notificationSvrQueue_ = nullptr;
1293     auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1294 
1295     auto ret = advancedNotificationService_->CancelAll("");
1296     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1297 
1298     ret = advancedNotificationService_->Delete("", 1);
1299     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1300 
1301     ret = advancedNotificationService_->CancelGroup("group", "");
1302     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1303 
1304     ret = advancedNotificationService_->RemoveGroupByBundle(bundle, "group");
1305     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1306 
1307     bool allowed = false;
1308     ret = advancedNotificationService_->IsSpecialUserAllowedNotify(1, allowed);
1309     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1310 
1311     ret = advancedNotificationService_->SetNotificationsEnabledByUser(1, false);
1312     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1313 
1314     ret = advancedNotificationService_->SetBadgeNumber(1, "");
1315     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1316 
1317     ret = advancedNotificationService_->SubscribeLocalLiveView(nullptr, nullptr, true);
1318     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1319 }
1320 
1321 /*
1322  * @tc.name: SetDistributedEnabledByBundle_0100
1323  * @tc.desc: test SetDistributedEnabledByBundle with parameters
1324  * @tc.type: FUNC
1325  */
1326 HWTEST_F(AnsPublishServiceTest, SetDistributedEnabledByBundle_0100, TestSize.Level1)
1327 {
1328     MockIsSystemApp(true);
1329     MockIsVerfyPermisson(true);
1330     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption("bundleName", 1));
1331     std::string deviceType = "testDeviceType";
1332 
1333     ErrCode res = advancedNotificationService_->SetDistributedEnabledByBundle(bundleOption, deviceType, true);
1334     ASSERT_EQ(res, ERR_OK);
1335 }
1336 
1337 /*
1338  * @tc.name: SetDistributedEnabledByBundle_0200
1339  * @tc.desc: test SetDistributedEnabledByBundle with parameters, expect errorCode ERR_ANS_NON_SYSTEM_APP.
1340  * @tc.type: FUNC
1341  */
1342 HWTEST_F(AnsPublishServiceTest, SetDistributedEnabledByBundle_0200, TestSize.Level1)
1343 {
1344     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1345     MockIsSystemApp(false);
1346     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption("bundleName", 1));
1347     std::string deviceType = "testDeviceType";
1348 
1349     ErrCode res = advancedNotificationService_->SetDistributedEnabledByBundle(bundleOption, deviceType, true);
1350     ASSERT_EQ(res, ERR_ANS_NON_SYSTEM_APP);
1351 }
1352 
1353 /*
1354  * @tc.name: SetDistributedEnabledByBundle_0300
1355  * @tc.desc: test SetDistributedEnabledByBundle with parameters, expect errorCode ERR_ANS_PERMISSION_DENIED.
1356  * @tc.type: FUNC
1357  */
1358 HWTEST_F(AnsPublishServiceTest, SetDistributedEnabledByBundle_0300, TestSize.Level1)
1359 {
1360     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1361     MockIsSystemApp(true);
1362     MockIsVerfyPermisson(false);
1363     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption("bundleName", 1));
1364     std::string deviceType = "testDeviceType";
1365 
1366     ErrCode res = advancedNotificationService_->SetDistributedEnabledByBundle(bundleOption, deviceType, true);
1367     ASSERT_EQ(res, ERR_ANS_PERMISSION_DENIED);
1368 }
1369 
1370 
1371 /**
1372  * @tc.name: IsDistributedEnabledByBundle_0100
1373  * @tc.desc: test IsDistributedEnabledByBundle with parameters
1374  * @tc.type: FUNC
1375  */
1376 HWTEST_F(AnsPublishServiceTest, IsDistributedEnabledByBundle_0100, TestSize.Level1)
1377 {
1378     MockIsSystemApp(true);
1379     MockIsVerfyPermisson(true);
1380     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption("bundleName", 1));
1381     std::string deviceType = "testDeviceType1111";
1382     bool enable = true;
1383     ErrCode result = advancedNotificationService_->IsDistributedEnabledByBundle(bundleOption, deviceType, enable);
1384     ASSERT_EQ(result, ERR_OK);
1385 }
1386 
1387 /**
1388  * @tc.name: IsDistributedEnabledByBundle_0200
1389  * @tc.desc: test IsDistributedEnabledByBundle with parameters
1390  * @tc.type: FUNC
1391  */
1392 HWTEST_F(AnsPublishServiceTest, IsDistributedEnabledByBundle_0200, TestSize.Level1)
1393 {
1394     MockIsSystemApp(true);
1395     MockIsVerfyPermisson(true);
1396     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption("bundleName", 1));
1397     std::string deviceType = "testDeviceType";
1398 
1399     ErrCode ret = advancedNotificationService_->SetDistributedEnabledByBundle(bundleOption, deviceType, true);
1400     ASSERT_EQ(ret, ERR_OK);
1401     bool enable = false;
1402     ret = advancedNotificationService_->IsDistributedEnabledByBundle(bundleOption, deviceType, enable);
1403     ASSERT_EQ(ret, ERR_OK);
1404     ASSERT_EQ(enable, true);
1405 }
1406 
1407 /**
1408  * @tc.name: IsDistributedEnabledByBundle_0300
1409  * @tc.desc: test IsDistributedEnabledByBundle with parameters, expect errorCode ERR_ANS_NON_SYSTEM_APP.
1410  * @tc.type: FUNC
1411  */
1412 HWTEST_F(AnsPublishServiceTest, IsDistributedEnabledByBundle_0300, TestSize.Level1)
1413 {
1414     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1415     MockIsSystemApp(false);
1416     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption("bundleName", 1));
1417     std::string deviceType = "testDeviceType1111";
1418     bool enable = true;
1419     ErrCode result = advancedNotificationService_->IsDistributedEnabledByBundle(bundleOption, deviceType, enable);
1420     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
1421 }
1422 
1423 /**
1424  * @tc.name: IsDistributedEnabledByBundle_0400
1425  * @tc.desc: test IsDistributedEnabledByBundle with parameters, expect errorCode ERR_ANS_PERMISSION_DENIED.
1426  * @tc.type: FUNC
1427  */
1428 HWTEST_F(AnsPublishServiceTest, IsDistributedEnabledByBundle_0400, TestSize.Level1)
1429 {
1430     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1431     MockIsSystemApp(true);
1432     MockIsVerfyPermisson(false);
1433     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption("bundleName", 1));
1434     std::string deviceType = "testDeviceType1111";
1435     bool enable = true;
1436     ErrCode result = advancedNotificationService_->IsDistributedEnabledByBundle(bundleOption, deviceType, enable);
1437     ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
1438 }
1439 
1440 /**
1441  * @tc.name: DuplicateMsgControl_00001
1442  * @tc.desc: Test DuplicateMsgControl
1443  * @tc.type: FUNC
1444  * @tc.require: issue
1445  */
1446 HWTEST_F(AnsPublishServiceTest, DuplicateMsgControl_00001, Function | SmallTest | Level1)
1447 {
1448     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1449     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1450     auto liveViewContent = std::make_shared<NotificationLiveViewContent>();
1451     auto content = std::make_shared<NotificationContent>(liveViewContent);
1452     request->SetContent(content);
1453 
1454     auto ret = advancedNotificationService_->DuplicateMsgControl(request);
1455     ASSERT_EQ(ret, (int)ERR_OK);
1456 }
1457 
1458 /**
1459  * @tc.name: DuplicateMsgControl_00002
1460  * @tc.desc: Test DuplicateMsgControl
1461  * @tc.type: FUNC
1462  * @tc.require: issue
1463  */
1464 HWTEST_F(AnsPublishServiceTest, DuplicateMsgControl_00002, Function | SmallTest | Level1)
1465 {
1466     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1467     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1468     request->SetAppMessageId("test1");
1469     auto uniqueKey = request->GenerateUniqueKey();
1470     advancedNotificationService_->uniqueKeyList_.emplace_back(
1471         std::make_pair(std::chrono::system_clock::now(), uniqueKey));
1472 
1473     auto ret = advancedNotificationService_->DuplicateMsgControl(request);
1474     ASSERT_EQ(ret, (int)ERR_ANS_DUPLICATE_MSG);
1475 }
1476 
1477 /**
1478  * @tc.name: DuplicateMsgControl_00003
1479  * @tc.desc: Test DuplicateMsgControl
1480  * @tc.type: FUNC
1481  * @tc.require: issue
1482  */
1483 HWTEST_F(AnsPublishServiceTest, DuplicateMsgControl_00003, Function | SmallTest | Level1)
1484 {
1485     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1486     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1487     request->SetAppMessageId("test2");
1488 
1489     auto ret = advancedNotificationService_->DuplicateMsgControl(request);
1490     ASSERT_EQ(ret, (int)ERR_OK);
1491     ASSERT_EQ(advancedNotificationService_->uniqueKeyList_.size(), 1);
1492 }
1493 
1494 /**
1495  * @tc.name: IsDuplicateMsg_00001
1496  * @tc.desc: Test IsDuplicateMsg
1497  * @tc.type: FUNC
1498  * @tc.require: issue
1499  */
1500 HWTEST_F(AnsPublishServiceTest, IsDuplicateMsg_00001, Function | SmallTest | Level1)
1501 {
1502     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1503     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1504     request->SetAppMessageId("test2");
1505     auto uniqueKey = request->GenerateUniqueKey();
1506     auto ret = advancedNotificationService_->IsDuplicateMsg(advancedNotificationService_->uniqueKeyList_, uniqueKey);
1507     ASSERT_EQ(ret, false);
1508 }
1509 
1510 /**
1511  * @tc.name: IsDuplicateMsg_00002
1512  * @tc.desc: Test IsDuplicateMsg
1513  * @tc.type: FUNC
1514  * @tc.require: issue
1515  */
1516 HWTEST_F(AnsPublishServiceTest, IsDuplicateMsg_00002, Function | SmallTest | Level1)
1517 {
1518     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1519     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1520     request->SetAppMessageId("test2");
1521     auto uniqueKey = request->GenerateUniqueKey();
1522     advancedNotificationService_->uniqueKeyList_.emplace_back(
1523         std::make_pair(std::chrono::system_clock::now(), uniqueKey));
1524     auto ret = advancedNotificationService_->IsDuplicateMsg(advancedNotificationService_->uniqueKeyList_, uniqueKey);
1525     ASSERT_EQ(ret, true);
1526 }
1527 
1528 /**
1529  * @tc.name: RemoveExpiredUniqueKey_00001
1530  * @tc.desc: Test RemoveExpiredUniqueKey
1531  * @tc.type: FUNC
1532  * @tc.require: issue
1533  */
1534 HWTEST_F(AnsPublishServiceTest, RemoveExpiredUniqueKey_00001, Function | SmallTest | Level1)
1535 {
1536     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1537     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1538     request->SetAppMessageId("test2");
1539     auto uniqueKey = request->GenerateUniqueKey();
1540     advancedNotificationService_->uniqueKeyList_.emplace_back(
1541         std::make_pair(std::chrono::system_clock::now() - std::chrono::hours(24), uniqueKey));
1542 
1543     sleep(1);
1544     ASSERT_EQ(advancedNotificationService_->uniqueKeyList_.size(), 1);
1545     advancedNotificationService_->RemoveExpiredUniqueKey();
1546     ASSERT_EQ(advancedNotificationService_->uniqueKeyList_.size(), 0);
1547 }
1548 
1549 /**
1550  * @tc.name: RemoveExpiredDistributedUniqueKey_00001
1551  * @tc.desc: Test RemoveExpiredDistributedUniqueKey
1552  * @tc.type: FUNC
1553  * @tc.require: issue
1554  */
1555 HWTEST_F(AnsPublishServiceTest, RemoveExpiredDistributedUniqueKey_00001, Function | SmallTest | Level1)
1556 {
1557     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1558     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1559     request->SetAppMessageId("test3");
1560     auto distributedUniqueKey = request->GenerateDistributedUniqueKey();
1561     advancedNotificationService_->distributedUniqueKeyList_.emplace_back(
1562         std::make_pair(std::chrono::system_clock::now() - std::chrono::hours(24), distributedUniqueKey));
1563 
1564     sleep(1);
1565     ASSERT_EQ(advancedNotificationService_->distributedUniqueKeyList_.size(), 1);
1566     advancedNotificationService_->RemoveExpiredDistributedUniqueKey();
1567     ASSERT_EQ(advancedNotificationService_->distributedUniqueKeyList_.size(), 0);
1568 }
1569 
1570 /**
1571  * @tc.name: RemoveExpiredLocalUniqueKey_00001
1572  * @tc.desc: Test RemoveExpiredLocalUniqueKey
1573  * @tc.type: FUNC
1574  * @tc.require: issue
1575  */
1576 HWTEST_F(AnsPublishServiceTest, RemoveExpiredLocalUniqueKey_00001, Function | SmallTest | Level1)
1577 {
1578     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1579     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1580     request->SetAppMessageId("test4");
1581     auto localUniqueKey = request->GenerateDistributedUniqueKey();
1582     advancedNotificationService_->localUniqueKeyList_.emplace_back(
1583         std::make_pair(std::chrono::system_clock::now() - std::chrono::hours(24), localUniqueKey));
1584 
1585     sleep(1);
1586     ASSERT_EQ(advancedNotificationService_->localUniqueKeyList_.size(), 1);
1587     advancedNotificationService_->RemoveExpiredLocalUniqueKey();
1588     ASSERT_EQ(advancedNotificationService_->localUniqueKeyList_.size(), 0);
1589 }
1590 
1591 /*
1592  * @tc.name: SetSmartReminderEnabled_0100
1593  * @tc.desc: test SetSmartReminderEnabled with parameters
1594  * @tc.type: FUNC
1595  */
1596 HWTEST_F(AnsPublishServiceTest, SetSmartReminderEnabled_0100, TestSize.Level1)
1597 {
1598     MockIsSystemApp(true);
1599     MockIsVerfyPermisson(true);
1600     ErrCode res = advancedNotificationService_->SetSmartReminderEnabled("testDeviceType", true);
1601     ASSERT_EQ(res, ERR_OK);
1602 }
1603 
1604 /*
1605  * @tc.name: SetSmartReminderEnabled_0200
1606  * @tc.desc: test SetSmartReminderEnabled with parameters, expect errorCode ERR_ANS_NON_SYSTEM_APP.
1607  * @tc.type: FUNC
1608  */
1609 HWTEST_F(AnsPublishServiceTest, SetSmartReminderEnabled_0200, TestSize.Level1)
1610 {
1611     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1612     MockIsSystemApp(false);
1613 
1614     ErrCode res = advancedNotificationService_->SetSmartReminderEnabled("testDeviceType", true);
1615     ASSERT_EQ(res, ERR_ANS_NON_SYSTEM_APP);
1616 }
1617 
1618 /*
1619  * @tc.name: SetSmartReminderEnabled_0300
1620  * @tc.desc: test SetSmartReminderEnabled with parameters, expect errorCode ERR_ANS_PERMISSION_DENIED.
1621  * @tc.type: FUNC
1622  */
1623 HWTEST_F(AnsPublishServiceTest, SetSmartReminderEnabled_0300, TestSize.Level1)
1624 {
1625     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1626     MockIsSystemApp(true);
1627     MockIsVerfyPermisson(false);
1628 
1629     ErrCode res = advancedNotificationService_->SetSmartReminderEnabled("testDeviceType", true);
1630     ASSERT_EQ(res, ERR_ANS_PERMISSION_DENIED);
1631 }
1632 
1633 
1634 /**
1635  * @tc.name: IsSmartReminderEnabled_0100
1636  * @tc.desc: test IsSmartReminderEnabled with parameters
1637  * @tc.type: FUNC
1638  */
1639 HWTEST_F(AnsPublishServiceTest, IsSmartReminderEnabled_0100, TestSize.Level1)
1640 {
1641     MockIsSystemApp(true);
1642     MockIsVerfyPermisson(true);
1643     bool enable = true;
1644     ErrCode result = advancedNotificationService_->IsSmartReminderEnabled("testDeviceType1111", enable);
1645     ASSERT_EQ(result, ERR_OK);
1646 }
1647 
1648 /**
1649  * @tc.name: IsSmartReminderEnabled_0200
1650  * @tc.desc: test IsSmartReminderEnabled with parameters
1651  * @tc.type: FUNC
1652  */
1653 HWTEST_F(AnsPublishServiceTest, IsSmartReminderEnabled_0200, TestSize.Level1)
1654 {
1655     MockIsSystemApp(true);
1656     MockIsVerfyPermisson(true);
1657     ErrCode ret = advancedNotificationService_->SetSmartReminderEnabled("testDeviceType", true);
1658     ASSERT_EQ(ret, ERR_OK);
1659     bool enable = false;
1660     ret = advancedNotificationService_->IsSmartReminderEnabled("testDeviceType", enable);
1661     ASSERT_EQ(ret, ERR_OK);
1662     ASSERT_EQ(enable, true);
1663 }
1664 
1665 /**
1666  * @tc.name: IsSmartReminderEnabled_0300
1667  * @tc.desc: test IsSmartReminderEnabled with parameters, expect errorCode ERR_ANS_NON_SYSTEM_APP.
1668  * @tc.type: FUNC
1669  */
1670 HWTEST_F(AnsPublishServiceTest, IsSmartReminderEnabled_0300, TestSize.Level1)
1671 {
1672     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1673     MockIsSystemApp(false);
1674     bool enable = true;
1675     ErrCode result = advancedNotificationService_->IsSmartReminderEnabled("testDeviceType1111", enable);
1676     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
1677 }
1678 
1679 /**
1680  * @tc.name: IsSmartReminderEnabled_0400
1681  * @tc.desc: test IsSmartReminderEnabled with parameters, expect errorCode ERR_ANS_PERMISSION_DENIED.
1682  * @tc.type: FUNC
1683  */
1684 HWTEST_F(AnsPublishServiceTest, IsSmartReminderEnabled_0400, TestSize.Level1)
1685 {
1686     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1687     MockIsSystemApp(true);
1688     MockIsVerfyPermisson(false);
1689     bool enable = true;
1690     ErrCode result = advancedNotificationService_->IsSmartReminderEnabled("testDeviceType1111", enable);
1691     ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
1692 }
1693 
1694 /**
1695  * @tc.name: PublishRemoveDuplicateEvent_00001
1696  * @tc.desc: Test PublishRemoveDuplicateEvent
1697  * @tc.type: FUNC
1698  * @tc.require: issue
1699  */
1700 HWTEST_F(AnsPublishServiceTest, PublishRemoveDuplicateEvent_00001, Function | SmallTest | Level1)
1701 {
1702     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1703     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1704     request->SetAppMessageId("test2");
1705     request->SetNotificationId(1);
1706     auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1707     auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1708 
1709     auto ret = advancedNotificationService_->PublishRemoveDuplicateEvent(record);
1710     ASSERT_EQ(ret, (int)ERR_OK);
1711 }
1712 
1713 /**
1714  * @tc.name: PublishRemoveDuplicateEvent_00002
1715  * @tc.desc: Test PublishRemoveDuplicateEvent
1716  * @tc.type: FUNC
1717  * @tc.require: issue
1718  */
1719 HWTEST_F(AnsPublishServiceTest, PublishRemoveDuplicateEvent_00002, Function | SmallTest | Level1)
1720 {
1721     std::shared_ptr<NotificationRecord> record= nullptr;
1722     auto ret = advancedNotificationService_->PublishRemoveDuplicateEvent(record);
1723     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1724 }
1725 
1726 /**
1727  * @tc.name: PublishRemoveDuplicateEvent_00003
1728  * @tc.desc: Test PublishRemoveDuplicateEvent
1729  * @tc.type: FUNC
1730  * @tc.require: issue
1731  */
1732 HWTEST_F(AnsPublishServiceTest, PublishRemoveDuplicateEvent_00003, Function | SmallTest | Level1)
1733 {
1734     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1735     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1736     request->SetAppMessageId("test2");
1737     request->SetNotificationId(1);
1738     request->SetIsAgentNotification(true);
1739     auto normalContent = std::make_shared<NotificationNormalContent>();
1740     auto content = std::make_shared<NotificationContent>(normalContent);
1741     request->SetContent(content);
1742     auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1743     auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1744 
1745     auto ret = advancedNotificationService_->PublishRemoveDuplicateEvent(record);
1746     ASSERT_EQ(ret, (int)ERR_OK);
1747 }
1748 
1749 /**
1750  * @tc.name: CanPopEnableNotificationDialog_001
1751  * @tc.desc: Test CanPopEnableNotificationDialog
1752  * @tc.type: FUNC
1753  * @tc.require: issue
1754  */
1755 HWTEST_F(AnsPublishServiceTest, CanPopEnableNotificationDialog_001, Function | SmallTest | Level1)
1756 {
1757     sptr<IAnsDialogCallback> callback = nullptr;
1758     bool canPop = false;
1759     std::string bundleName = "";
1760     ErrCode result = advancedNotificationService_->CanPopEnableNotificationDialog(callback, canPop, bundleName);
1761     ASSERT_EQ(result, ERROR_INTERNAL_ERROR);
1762 }
1763 
1764 /**
1765  * @tc.name: IsDisableNotification_001
1766  * @tc.desc: Test IsDisableNotification
1767  * @tc.type: FUNC
1768  * @tc.require: issue
1769  */
1770 HWTEST_F(AnsPublishServiceTest, IsDisableNotification_001, Function | SmallTest | Level1)
1771 {
1772     std::string bundleName = "";
1773     bool result = advancedNotificationService_->IsDisableNotification(bundleName);
1774     ASSERT_FALSE(result);
1775 }
1776 
1777 /**
1778  * @tc.name: IsDisableNotification_002
1779  * @tc.desc: Test IsDisableNotification
1780  * @tc.type: FUNC
1781  * @tc.require: issue
1782  */
1783 HWTEST_F(AnsPublishServiceTest, IsDisableNotification_002, Function | SmallTest | Level1)
1784 {
1785     bool defaultPolicy = system::GetBoolParameter("persist.edm.notification_disable", false);
1786     if (!defaultPolicy) {
1787         system::SetBoolParameter("persist.edm.notification_disable", true);
1788     }
1789     std::string bundleName = "";
1790     bool result = advancedNotificationService_->IsDisableNotification(bundleName);
1791     ASSERT_TRUE(result);
1792     system::SetBoolParameter("persist.edm.notification_disable", defaultPolicy);
1793 }
1794 
1795 /**
1796  * @tc.name: IsNeedToControllerByDisableNotification_001
1797  * @tc.desc: Test IsNeedToControllerByDisableNotification
1798  * @tc.type: FUNC
1799  * @tc.require: issue
1800  */
1801 HWTEST_F(AnsPublishServiceTest, IsNeedToControllerByDisableNotification_001, Function | SmallTest | Level1)
1802 {
1803     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1804     bool result = advancedNotificationService_->IsNeedToControllerByDisableNotification(request);
1805     EXPECT_TRUE(result);
1806 }
1807 
1808 /**
1809  * @tc.name: IsNeedToControllerByDisableNotification_002
1810  * @tc.desc: Test IsNeedToControllerByDisableNotification
1811  * @tc.type: FUNC
1812  * @tc.require: issue
1813  */
1814 HWTEST_F(AnsPublishServiceTest, IsNeedToControllerByDisableNotification_002, Function | SmallTest | Level1)
1815 {
1816     sptr<NotificationRequest> request = nullptr;
1817     bool result = advancedNotificationService_->IsNeedToControllerByDisableNotification(request);
1818     EXPECT_FALSE(result);
1819 }
1820 
1821 /**
1822  * @tc.name: PrePublishRequest_00001
1823  * @tc.desc: Test PrePublishRequest
1824  * @tc.type: FUNC
1825  * @tc.require: issue
1826  */
1827 HWTEST_F(AnsPublishServiceTest, PrePublishRequest_00001, Function | SmallTest | Level1)
1828 {
1829     MockIsOsAccountExists(false);
1830     sptr<NotificationRequest> request = new NotificationRequest();
1831     request->SetReceiverUserId(-99);
1832     ASSERT_EQ(advancedNotificationService_->PrePublishRequest(request), (int)ERROR_USER_NOT_EXIST);
1833     MockIsOsAccountExists(true);
1834     sptr<NotificationRequest> request1 = new NotificationRequest();
1835     request1->SetCreatorUid(0);
1836     request1->SetReceiverUserId(100);
1837     ASSERT_EQ(advancedNotificationService_->PrePublishRequest(request1), (int)ERR_ANS_INVALID_UID);
1838     sptr<NotificationRequest> request2 = new NotificationRequest();
1839     request2->SetDeliveryTime(-1);
1840     request2->SetReceiverUserId(100);
1841     request2->SetCreatorUid(1);
1842     ASSERT_EQ(advancedNotificationService_->PrePublishRequest(request2), (int)ERR_OK);
1843 }
1844 
1845 /**
1846  * @tc.name: CollaboratePublish_00001
1847  * @tc.desc: Test CollaboratePublish
1848  * @tc.type: FUNC
1849  * @tc.require: issue
1850  */
1851 HWTEST_F(AnsPublishServiceTest, CollaboratePublish_00001, Function | SmallTest | Level1)
1852 {
1853     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1854     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1855     std::string label = "";
1856     request->SetAppMessageId("test2");
1857     request->SetNotificationId(1);
1858     request->SetIsAgentNotification(true);
1859     request->SetDistributedCollaborate(true);
1860     MockIsVerfyPermisson(false);
1861     auto ret = advancedNotificationService_->Publish(label, request);
1862     ASSERT_EQ(ret, ERR_ANS_PERMISSION_DENIED);
1863     MockIsVerfyPermisson(true);
1864     ret = advancedNotificationService_->Publish(label, request);
1865     ASSERT_EQ(ret, (int)ERR_OK);
1866 }
1867 
1868 /**
1869  * @tc.name: CollaboratePublish_00002
1870  * @tc.desc: Test CollaboratePublish,
1871  *  common live view without permisson, except is ERR_ANS_PERMISSION_DENIED
1872  *  common live view with permisson, except is ERR_OK
1873  * @tc.type: FUNC
1874  * @tc.require: issue
1875  */
1876 HWTEST_F(AnsPublishServiceTest, CollaboratePublish_00002, Function | SmallTest | Level1)
1877 {
1878     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1879     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1880     std::shared_ptr<NotificationLiveViewContent> liveViewContent = std::make_shared<NotificationLiveViewContent>();
1881     std::shared_ptr<NotificationContent> notificationContent = std::make_shared<NotificationContent>(liveViewContent);
1882     request->SetContent(notificationContent);
1883     std::string label = "";
1884     request->SetAppMessageId("test1");
1885     request->SetNotificationId(1);
1886     request->SetIsAgentNotification(true);
1887     request->SetDistributedCollaborate(true);
1888     MockIsVerfyPermisson(false);
1889     auto ret = advancedNotificationService_->Publish(label, request);
1890     ASSERT_EQ(ret, ERR_ANS_PERMISSION_DENIED);
1891     MockIsVerfyPermisson(true);
1892     ret = advancedNotificationService_->Publish(label, request);
1893     ASSERT_EQ(ret, (int)ERR_OK);
1894 }
1895 
1896 /**
1897  * @tc.name: CollaboratePublish_00003
1898  * @tc.desc: Test permission of CollaboratePublish,
1899  *  Non-subsystem (HAP token), without permisson, except is ERR_ANS_PERMISSION_DENIED
1900  *  Non-subsystem (HAP token), with permisson, except is ERR_ANS_PERMISSION_DENIED
1901  *  Subsystem (Native token), without permisson, except is ERR_ANS_PERMISSION_DENIED
1902  *  Subsystem (Native token), with permisson, except is ERR_OK
1903  * @tc.type: FUNC
1904  * @tc.require: issue
1905  */
1906 HWTEST_F(AnsPublishServiceTest, CollaboratePublish_00003, Function | SmallTest | Level1)
1907 {
1908     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1909     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1910     MockIsVerfyPermisson(false);
1911     auto ret = advancedNotificationService_->CollaboratePublish(request);
1912     ASSERT_EQ(ret, ERR_ANS_PERMISSION_DENIED);
1913 
1914     MockIsVerfyPermisson(true);
1915     ret = advancedNotificationService_->CollaboratePublish(request);
1916     ASSERT_EQ(ret, ERR_ANS_PERMISSION_DENIED);
1917 
1918     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
1919     MockIsVerfyPermisson(false);
1920     ret = advancedNotificationService_->CollaboratePublish(request);
1921     ASSERT_EQ(ret, ERR_ANS_PERMISSION_DENIED);
1922 
1923     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
1924     MockIsVerfyPermisson(true);
1925     ret = advancedNotificationService_->CollaboratePublish(request);
1926     ASSERT_EQ(ret, ERR_OK);
1927 }
1928 
1929 /**
1930  * @tc.name: PublishNotificationForIndirectProxy_00001
1931  * @tc.desc: Test PublishNotificationForIndirectProxy,
1932  *  request is nullptr, return is ERR_ANS_INVALID_PARAM
1933  *  request is valid, uid is invalid, return is ERR_ANS_INVALID_UID
1934  *  sound is empty, return is ERR_OK
1935  *  sound is not empty, return is ERR_OK
1936  * @tc.type: FUNC
1937  * @tc.require: issue
1938  */
1939 HWTEST_F(AnsPublishServiceTest, PublishNotificationForIndirectProxy_00001, Function | SmallTest | Level1)
1940 {
1941     auto ret = advancedNotificationService_->PublishNotificationForIndirectProxy(nullptr);
1942     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1943     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1944     ret = advancedNotificationService_->PublishNotificationForIndirectProxy(request);
1945     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_UID);
1946     request->SetCreatorUid(1);
1947     ret = advancedNotificationService_->PublishNotificationForIndirectProxy(request);
1948     ASSERT_EQ(ret, (int)ERR_OK);
1949     request->SetSound("sound");
1950     request->SetCreatorBundleName("creatorname");
1951     request->SetAppInstanceKey("key");
1952     ret = advancedNotificationService_->PublishNotificationForIndirectProxy(request);
1953     ASSERT_EQ(ret, (int)ERR_OK);
1954 }
1955 
1956 /**
1957  * @tc.name: PublishNotificationForIndirectProxy_00002
1958  * @tc.desc: Test PublishNotificationForIndirectProxy,
1959  *  request is valid, creator uid is 0, PrePublishRequest faild, except is ERR_ANS_INVALID_UID
1960  * @tc.type: FUNC
1961  * @tc.require: issue
1962  */
1963 HWTEST_F(AnsPublishServiceTest, PublishNotificationForIndirectProxy_00002, Function | SmallTest | Level1)
1964 {
1965     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1966     request->SetCreatorUid(0);
1967     auto ret = advancedNotificationService_->PublishNotificationForIndirectProxy(request);
1968     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_UID);
1969 }
1970 
1971 /**
1972  * @tc.name: CancelAsBundle_00001
1973  * @tc.desc: Test CancelAsBundle,
1974  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
1975  *  2. Non-subsystem (HAP token) and system app -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
1976  *  3. Subsystem (Native token) and non-system app -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
1977  *  4. Subsystem (Native token) and system app -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
1978  * @tc.type: FUNC
1979  * @tc.require: issue
1980  */
1981 HWTEST_F(AnsPublishServiceTest, CancelAsBundle_00001, Function | SmallTest | Level1)
1982 {
1983     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("bundleName", 1);
1984 
1985     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1986     MockIsSystemApp(false);
1987     auto result = advancedNotificationService_->CancelAsBundle(bundle, 1, 1);
1988     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
1989 
1990     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1991     MockIsSystemApp(true);
1992     result = advancedNotificationService_->CancelAsBundle(bundle, 1, 1);
1993     ASSERT_EQ(result, ERR_ANS_NOTIFICATION_NOT_EXISTS);
1994 
1995     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
1996     MockIsSystemApp(false);
1997     result = advancedNotificationService_->CancelAsBundle(bundle, 1, 1);
1998     ASSERT_EQ(result, ERR_ANS_NOTIFICATION_NOT_EXISTS);
1999 
2000     MockIsSystemApp(true);
2001     result = advancedNotificationService_->CancelAsBundle(bundle, 1, 1);
2002     ASSERT_EQ(result, ERR_ANS_NOTIFICATION_NOT_EXISTS);
2003 }
2004 
2005 /**
2006  * @tc.name: CancelAsBundle_00002
2007  * @tc.desc: Test CancelAsBundle,
2008  *  bundleOption is valid, userId is -2, checkUserIdParams faild, except is ERR_ANS_INVALID_UID
2009  * @tc.type: FUNC
2010  * @tc.require: issue
2011  */
2012 HWTEST_F(AnsPublishServiceTest, CancelAsBundle_00002, Function | SmallTest | Level1)
2013 {
2014     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("bundleName", 1);
2015     MockIsOsAccountExists(false);
2016     auto result = advancedNotificationService_->CancelAsBundle(bundle, 1, -2);
2017     ASSERT_EQ(result, (int)ERROR_USER_NOT_EXIST);
2018 }
2019 
2020 /**
2021  * @tc.name: CancelAsBundle_00003
2022  * @tc.desc: Test CancelAsBundle,
2023  *  uid is 0, except is ERR_ANS_NOTIFICATION_NOT_EXISTS
2024  * @tc.type: FUNC
2025  * @tc.require: issue
2026  */
2027 HWTEST_F(AnsPublishServiceTest, CancelAsBundle_00003, Function | SmallTest | Level1)
2028 {
2029     MockIsOsAccountExists(true);
2030     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("bundleName", 0);
2031     auto result = advancedNotificationService_->CancelAsBundle(bundle, 1);
2032     ASSERT_EQ(result, (int)ERR_ANS_NOTIFICATION_NOT_EXISTS);
2033 }
2034 
2035 /**
2036  * @tc.name: CancelAsBundleWithAgent_00001
2037  * @tc.desc: Test CancelAsBundleWithAgent,
2038  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
2039  *  2. Non-subsystem (HAP token) and system app -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
2040  *  3. Subsystem (Native token) and non-system app -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
2041  *  4. Subsystem (Native token) and system app -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
2042  *  5. Subsystem (Native token) and system app, uid < 0 -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
2043  * @tc.type: FUNC
2044  * @tc.require: issue
2045  */
2046 HWTEST_F(AnsPublishServiceTest, CancelAsBundleWithAgent_00001, Function | SmallTest | Level1)
2047 {
2048     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("bundleName", 1);
2049 
2050     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2051     MockIsSystemApp(false);
2052     auto result = advancedNotificationService_->CancelAsBundleWithAgent(bundle, 1);
2053     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
2054 
2055     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2056     MockIsSystemApp(true);
2057     result = advancedNotificationService_->CancelAsBundleWithAgent(bundle, 1);
2058     ASSERT_EQ(result, ERR_ANS_NOTIFICATION_NOT_EXISTS);
2059 
2060     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
2061     MockIsSystemApp(false);
2062     result = advancedNotificationService_->CancelAsBundleWithAgent(bundle, 1);
2063     ASSERT_EQ(result, ERR_ANS_NOTIFICATION_NOT_EXISTS);
2064 
2065     MockIsSystemApp(true);
2066     result = advancedNotificationService_->CancelAsBundleWithAgent(bundle, 1);
2067     ASSERT_EQ(result, ERR_ANS_NOTIFICATION_NOT_EXISTS);
2068 
2069     bundle->SetUid(-1);
2070     result = advancedNotificationService_->CancelAsBundleWithAgent(bundle, 1);
2071     ASSERT_EQ(result, ERR_ANS_INVALID_UID);
2072 }
2073 
2074 /**
2075  * @tc.name: Delete_00001
2076  * @tc.desc: Test Delete,
2077  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
2078  *  2. Non-subsystem (HAP token) and system app -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
2079  *  3. Subsystem (Native token) and non-system app -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
2080  *  4. Subsystem (Native token) and system app -> return ERR_ANS_NOTIFICATION_NOT_EXISTS
2081  * @tc.type: FUNC
2082  * @tc.require: issue
2083  */
2084 HWTEST_F(AnsPublishServiceTest, Delete_00001, Function | SmallTest | Level1)
2085 {
2086     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2087     MockIsSystemApp(false);
2088     auto result = advancedNotificationService_->Delete("key", 1);
2089     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
2090 
2091     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2092     MockIsSystemApp(true);
2093     result = advancedNotificationService_->Delete("key", 1);
2094     ASSERT_EQ(result, ERR_ANS_NOTIFICATION_NOT_EXISTS);
2095 
2096     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
2097     MockIsSystemApp(false);
2098     result = advancedNotificationService_->Delete("key", 1);
2099     ASSERT_EQ(result, ERR_ANS_NOTIFICATION_NOT_EXISTS);
2100 
2101     MockIsSystemApp(true);
2102     result = advancedNotificationService_->Delete("key", 1);
2103     ASSERT_EQ(result, ERR_ANS_NOTIFICATION_NOT_EXISTS);
2104 }
2105 
2106 /**
2107  * @tc.name: RemoveEnableNotificationDialog_00001
2108  * @tc.desc: Test RemoveEnableNotificationDialog, except is ERR_OK
2109  * @tc.type: FUNC
2110  * @tc.require: issue
2111  */
2112 HWTEST_F(AnsPublishServiceTest, RemoveEnableNotificationDialog_00001, Function | SmallTest | Level1)
2113 {
2114     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
2115     auto ret = advancedNotificationService_->RemoveEnableNotificationDialog();
2116     ASSERT_EQ(ret, (int)ERR_OK);
2117 }
2118 
2119 /**
2120  * @tc.name: QueryContactByProfileId_00001
2121  * @tc.desc: Test QueryContactByProfileId, except is -1
2122  * @tc.type: FUNC
2123  * @tc.require: issue
2124  */
2125 HWTEST_F(AnsPublishServiceTest, QueryContactByProfileId_00001, Function | SmallTest | Level1)
2126 {
2127     auto datashareHelper = DelayedSingleton<AdvancedDatashareHelper>::GetInstance();
2128     bool isDataShareReady = true;
2129     datashareHelper->SetIsDataShareReady(isDataShareReady);
2130     std::string phoneNumber = "12345678";
2131     std::string policy = "5";
2132     int32_t userId = 100;
2133     auto ret = advancedNotificationService_->QueryContactByProfileId(phoneNumber, policy, userId);
2134     ASSERT_EQ(ret, -1);
2135 }
2136 
2137 /**
2138  * @tc.name: SetDistributedEnabledBySlot_00001
2139  * @tc.desc: Test SetDistributedEnabledBySlot, except is -1
2140  * @tc.type: FUNC
2141  * @tc.require: issue
2142  */
2143 HWTEST_F(AnsPublishServiceTest, SetDistributedEnabledBySlot_00001, Function | SmallTest | Level1)
2144 {
2145     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
2146     std::string deviceType = "testdeviceType";
2147     bool enabled = true;
2148     MockIsVerfyPermisson(false);
2149     auto ret = advancedNotificationService_->SetDistributedEnabledBySlot(slotType, deviceType, enabled);
2150     ASSERT_EQ(ret, ERR_ANS_PERMISSION_DENIED);
2151     MockIsVerfyPermisson(true);
2152     ret = advancedNotificationService_->SetDistributedEnabledBySlot(slotType, deviceType, enabled);
2153     ASSERT_EQ(ret, ERR_OK);
2154 }
2155 
2156 /**
2157  * @tc.name: SetTargetDeviceStatus_00001
2158  * @tc.desc: Test SetTargetDeviceStatus, deviceType is empty, except is ERR_ANS_INVALID_PARAM
2159  * @tc.type: FUNC
2160  * @tc.require: issue
2161  */
2162 HWTEST_F(AnsPublishServiceTest, SetTargetDeviceStatus_00001, Function | SmallTest | Level1)
2163 {
2164     std::string deviceType = "";
2165     uint32_t status = 0;
2166     std::string deviceId = "";
2167     auto ret = advancedNotificationService_->SetTargetDeviceStatus(deviceType, status, deviceId);
2168     ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM);
2169 }
2170 
2171 /**
2172  * @tc.name: GetTargetDeviceStatus_00001
2173  * @tc.desc: Test GetTargetDeviceStatus, deviceType is empty, except is ERR_ANS_INVALID_PARAM
2174  * @tc.desc: Test GetTargetDeviceStatus, deviceType is not empty, status == inputStatus
2175  * @tc.type: FUNC
2176  * @tc.require: issue
2177  */
2178 HWTEST_F(AnsPublishServiceTest, GetTargetDeviceStatus_00001, Function | SmallTest | Level1)
2179 {
2180     std::string deviceType = "";
2181     int32_t inputStatus = 1;
2182     int32_t status = 0;
2183     auto ret = advancedNotificationService_->GetTargetDeviceStatus(deviceType, status);
2184     ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM);
2185     uint32_t controlFlag = 0;
2186     deviceType = "testdeviceType";
2187     std::string deviceId = "";
2188     ret = advancedNotificationService_->SetTargetDeviceStatus(deviceType, inputStatus, deviceId);
2189     ASSERT_EQ(ret, ERR_OK);
2190     ret = advancedNotificationService_->GetTargetDeviceStatus(deviceType, status);
2191     ASSERT_EQ(status, inputStatus);
2192 }
2193 
2194 /**
2195  * @tc.name: ClearAllNotificationGroupInfo_00001
2196  * @tc.desc: Test ClearAllNotificationGroupInfo, deviceType is not empty, except is ERR_OK
2197  * @tc.type: FUNC
2198  * @tc.require: issue
2199  */
2200 HWTEST_F(AnsPublishServiceTest, ClearAllNotificationGroupInfo_00001, Function | SmallTest | Level1)
2201 {
2202     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
2203     sptr<Notification> notification = new (std::nothrow) Notification(request);
2204     auto record = std::make_shared<NotificationRecord>();
2205     record->request = request;
2206     record->notification = notification;
2207     advancedNotificationService_->notificationList_.push_back(record);
2208     std::string localSwitch = "false";
2209     advancedNotificationService_->aggregateLocalSwitch_ = true;
2210     advancedNotificationService_->ClearAllNotificationGroupInfo(localSwitch);
2211     std::this_thread::sleep_for(std::chrono::milliseconds(200));
2212     ASSERT_EQ(advancedNotificationService_->aggregateLocalSwitch_, false);
2213 }
2214 
2215 /**
2216  * @tc.name: RemoveAllNotificationsByBundleName_00001
2217  * @tc.desc: Test RemoveAllNotificationsByBundleName, except is notificationList_ == 1
2218  * @tc.type: FUNC
2219  * @tc.require: issue
2220  */
2221 HWTEST_F(AnsPublishServiceTest, RemoveAllNotificationsByBundleName_00001, Function | SmallTest | Level1)
2222 {
2223     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
2224     sptr<Notification> notification = new (std::nothrow) Notification(request);
2225     auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
2226     auto record1 = std::make_shared<NotificationRecord>();
2227     record1->request = request;
2228     record1->notification = notification;
2229     record1->bundleOption = bundle;
2230     advancedNotificationService_->notificationList_.push_back(record1);
2231     auto record2 = nullptr;
2232     advancedNotificationService_->notificationList_.push_back(record2);
2233     std::string bundleName = TEST_DEFUALT_BUNDLE;
2234     int32_t reason = 0;
2235     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 2);
2236     auto ret = advancedNotificationService_->RemoveAllNotificationsByBundleName(bundleName, reason);
2237     ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 1);
2238 }
2239 
2240 /**
2241  * @tc.name: DistributeOperation_00001
2242  * @tc.desc: Test DistributeOperation,
2243  *  without hashcode, return is ERR_ANS_INVALID_PARAM
2244  *  without permisson, return is ERR_ANS_PERMISSION_DENIED
2245  *  DistributeOperationParamCheck faild, except is ERR_ANS_INVALID_PARAM
2246  * @tc.type: FUNC
2247  * @tc.require: issue
2248  */
2249 HWTEST_F(AnsPublishServiceTest, DistributeOperation_00001, Function | SmallTest | Level1)
2250 {
2251     sptr<NotificationOperationInfo> operationInfo = new (std::nothrow) NotificationOperationInfo();
2252     sptr<IAnsOperationCallback> callback = nullptr;
2253     auto ret = advancedNotificationService_->DistributeOperation(operationInfo, callback);
2254     ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM);
2255     std::string hashCode = "123456";
2256     operationInfo->SetHashCode(hashCode);
2257     MockIsVerfyPermisson(false);
2258     ret = advancedNotificationService_->DistributeOperation(operationInfo, callback);
2259     ASSERT_EQ(ret, ERR_ANS_PERMISSION_DENIED);
2260     MockIsVerfyPermisson(true);
2261     ret = advancedNotificationService_->DistributeOperation(operationInfo, callback);
2262     ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM);
2263 }
2264 
2265 /**
2266  * @tc.name: SetBadgeNumberForDhByBundle_00001
2267  * @tc.desc: Test SetBadgeNumberForDhByBundle,
2268  *  1. Parameter validation:
2269  *      - bundleOption is null -> return ERR_ANS_INVALID_PARAM
2270  *      - bundleName is empty -> return ERR_ANS_INVALID_PARAM
2271  *      - uid <= 0 -> return ERR_ANS_INVALID_PARAM
2272  *      - badgeNumber < 0 -> return ERR_ANS_INVALID_PARAM
2273  *  2. Permission validation:
2274  *      - Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
2275  *      - Subsystem (Native token) and non-system app -> ERR_OK
2276  *      - Non-subsystem (HAP token) and system app -> ERR_OK
2277  *      - Subsystem (Native token) and system app -> ERR_OK
2278  *  3. Success path:
2279  *      -All valid conditions (non-null bundleOption, valid bundleName, uid>0, badgeNumber>0, valid permissions)
2280  *       ->return ERR_OK
2281  * @tc.type: FUNC
2282  * @tc.require: issue
2283  */
2284 HWTEST_F(AnsPublishServiceTest, SetBadgeNumberForDhByBundle_00001, Function | SmallTest | Level1)
2285 {
2286     sptr<NotificationBundleOption> bundle = nullptr;
2287     auto ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, -1);
2288     ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM);
2289 
2290     bundle = new NotificationBundleOption("", 0);
2291     ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, -1);
2292     ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM);
2293 
2294     bundle->SetBundleName("bundle");
2295     ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, -1);
2296     ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM);
2297 
2298     bundle->SetUid(12345);
2299     ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, -1);
2300     ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM);
2301 
2302     bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
2303     MockIsSystemApp(false);
2304     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2305     ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, 1);
2306     ASSERT_EQ(ret, ERR_ANS_NON_SYSTEM_APP);
2307 
2308     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
2309     ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, 1);
2310     ASSERT_EQ(ret, ERR_OK);
2311 
2312     MockIsSystemApp(true);
2313     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2314     ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, 1);
2315     ASSERT_EQ(ret, ERR_OK);
2316 
2317     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
2318     ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, 1);
2319     ASSERT_EQ(ret, ERR_OK);
2320 
2321     bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
2322     MockIsSystemApp(true);
2323     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2324     ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, 1);
2325     ASSERT_EQ(ret, ERR_OK);
2326 
2327     ret = advancedNotificationService_->SetBadgeNumberForDhByBundle(bundle, 0);
2328     ASSERT_EQ(ret, ERR_OK);
2329 }
2330 
2331 /**
2332  * @tc.name: SetHashCodeRule_00001
2333  * @tc.desc: Test SetHashCodeRule,
2334  *  1. Non-subsystem (HAP token) and non-system app -> return ERR_ANS_NON_SYSTEM_APP
2335  *  2. Non-subsystem (HAP token) and system app with invalid UID -> return ERR_ANS_PERMISSION_DENIED
2336  *  3. Non-subsystem (HAP token) and system app with valid UID -> return ERR_OK
2337  *  4. Subsystem (Native token) with invalid UID -> return ERR_ANS_PERMISSION_DENIED
2338  *  5. Subsystem (Native token) with valid UID -> return ERR_OK
2339  * @tc.type: FUNC
2340  * @tc.require: issue
2341  */
2342 HWTEST_F(AnsPublishServiceTest, SetHashCodeRule_00001, Function | SmallTest | Level1)
2343 {
2344     int32_t avseesaionPid = 6700;
2345 
2346     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2347     MockIsSystemApp(false);
2348     IPCSkeleton::SetCallingUid(12345);
2349     auto result = advancedNotificationService_->SetHashCodeRule(1);
2350     ASSERT_EQ(result, ERR_ANS_NON_SYSTEM_APP);
2351 
2352     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2353     MockIsSystemApp(true);
2354     IPCSkeleton::SetCallingUid(12345);
2355     result = advancedNotificationService_->SetHashCodeRule(1);
2356     ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
2357 
2358     IPCSkeleton::SetCallingUid(avseesaionPid);
2359     result = advancedNotificationService_->SetHashCodeRule(1);
2360     ASSERT_EQ(result, ERR_OK);
2361 
2362     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
2363     MockIsSystemApp(false);
2364     IPCSkeleton::SetCallingUid(12345);
2365     result = advancedNotificationService_->SetHashCodeRule(1);
2366     ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
2367 
2368     IPCSkeleton::SetCallingUid(avseesaionPid);
2369     result = advancedNotificationService_->SetHashCodeRule(1);
2370     ASSERT_EQ(result, ERR_OK);
2371 }
2372 
2373 /**
2374  * @tc.name: CollaborateFilter_00001
2375  * @tc.desc: Test CollaborateFilter
2376  * 1.extendInfo is null return ok
2377  * 2.notification_collaboration_check is false, return ok
2378  * @tc.type: FUNC
2379  * @tc.require: issue
2380  */
2381 HWTEST_F(AnsPublishServiceTest, CollaborateFilter_00001, Function | SmallTest | Level1)
2382 {
2383     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
2384     auto ret = advancedNotificationService_->CollaborateFilter(request);
2385     ASSERT_EQ(ret, (int)ERR_OK);
2386 
2387     std::shared_ptr<AAFwk::WantParams> extendInfo = std::make_shared<AAFwk::WantParams>();
2388     extendInfo->SetParam("test", AAFwk::String::Box("test"));
2389     request->SetExtendInfo(extendInfo);
2390 
2391     ret = advancedNotificationService_->CollaborateFilter(request);
2392     ASSERT_EQ(ret, (int)ERR_OK);
2393 
2394     extendInfo->SetParam("notification_collaboration_check", AAFwk::Boolean::Box(false));
2395     ret = advancedNotificationService_->CollaborateFilter(request);
2396     ASSERT_EQ(ret, (int)ERR_OK);
2397 
2398     extendInfo->SetParam("notification_collaboration_check", AAFwk::Boolean::Box(true));
2399     ret = advancedNotificationService_->CollaborateFilter(request);
2400     ASSERT_EQ(ret, (int)ERR_ANS_NOT_ALLOWED);
2401 }
2402 
2403 /**
2404  * @tc.name: CollaborateFilter_00002
2405  * @tc.desc: Test CollaborateFilter
2406  * 1.DistributedAuthStatus closed, return ERR_ANS_NOT_ALLOWED
2407  * 2.liveView:DistributedAuthStatus open, liveView distributed switch close, return ERR_ANS_NOT_ALLOWED
2408  * 3.notification:DistributedAuthStatus open, distributed switch close, return ERR_ANS_NOT_ALLOWED
2409  * @tc.type: FUNC
2410  * @tc.require: issue
2411  */
2412 HWTEST_F(AnsPublishServiceTest, CollaborateFilter_00002, Function | SmallTest | Level1)
2413 {
2414     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
2415 
2416     std::shared_ptr<AAFwk::WantParams> extendInfo = std::make_shared<AAFwk::WantParams>();
2417     extendInfo->SetParam("notification_collaboration_check", AAFwk::Boolean::Box(true));
2418     request->SetExtendInfo(extendInfo);
2419 
2420     std::string deviceType = "deviceType";
2421     std::string deviceId = "deviceId";
2422     std::string localType = "localType";
2423     int32_t userId = 100;
2424     extendInfo->SetParam("notification_collaboration_deviceType", AAFwk::String::Box(deviceType));
2425     extendInfo->SetParam("notification_collaboration_deviceId", AAFwk::String::Box(deviceId));
2426     extendInfo->SetParam("notification_collaboration_localType", AAFwk::String::Box(localType));
2427     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
2428     NotificationPreferences::GetInstance()->SetDistributedAuthStatus(deviceType, deviceId, userId, true);
2429 
2430     NotificationPreferences::GetInstance()->SetDistributedEnabledBySlot(
2431         NotificationConstant::SlotType::LIVE_VIEW, localType, true);
2432     auto ret = advancedNotificationService_->CollaborateFilter(request);
2433     ASSERT_EQ(ret, (int)ERR_OK);
2434 
2435     NotificationPreferences::GetInstance()->SetDistributedEnabledBySlot(
2436         NotificationConstant::SlotType::LIVE_VIEW, localType, false);
2437     ret = advancedNotificationService_->CollaborateFilter(request);
2438     ASSERT_EQ(ret, (int)ERR_ANS_NOT_ALLOWED);
2439 
2440     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2441     NotificationPreferences::GetInstance()->SetDistributedEnabled(
2442         localType, NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON);
2443     ret = advancedNotificationService_->CollaborateFilter(request);
2444     ASSERT_EQ(ret, (int)ERR_OK);
2445 
2446     NotificationPreferences::GetInstance()->SetDistributedEnabled(
2447         localType, NotificationConstant::SWITCH_STATE::USER_MODIFIED_OFF);
2448     ret = advancedNotificationService_->CollaborateFilter(request);
2449     ASSERT_EQ(ret, (int)ERR_ANS_NOT_ALLOWED);
2450 
2451     NotificationPreferences::GetInstance()->SetDistributedAuthStatus(deviceType, deviceId, userId, false);
2452 }
2453 
2454 /**
2455  * @tc.name: ClearSlotTypeData_00001
2456  * @tc.desc: Test ClearSlotTypeData
2457  * 1.sourceType == CLEAR_SLOT_FROM_AVSEESAION condation
2458  * 2.sourceType == CLEAR_SLOT_FROM_RSS condation
2459  * @tc.type: FUNC
2460  * @tc.require: issue
2461  */
2462 HWTEST_F(AnsPublishServiceTest, ClearSlotTypeData_00001, Function | SmallTest | Level1)
2463 {
2464     sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
2465     int32_t callingUid = 0;
2466     int32_t sourceType = 0;
2467     advancedNotificationService_->ClearSlotTypeData(request, callingUid, sourceType);
2468     ASSERT_EQ(sourceType, 0);
2469 
2470     sourceType = 1;
2471     advancedNotificationService_->ClearSlotTypeData(request, callingUid, sourceType);
2472     ASSERT_EQ(sourceType, 1);
2473 
2474     callingUid = 6700;
2475     advancedNotificationService_->ClearSlotTypeData(request, callingUid, sourceType);
2476     ASSERT_EQ(sourceType, 1);
2477 
2478     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
2479     advancedNotificationService_->ClearSlotTypeData(request, callingUid, sourceType);
2480     ASSERT_EQ(sourceType, 1);
2481 
2482     sourceType = 2;
2483     request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2484     advancedNotificationService_->ClearSlotTypeData(request, callingUid, sourceType);
2485     ASSERT_EQ(sourceType, 2);
2486 
2487     advancedNotificationService_->ClearSlotTypeData(request, callingUid, sourceType);
2488     ASSERT_EQ(sourceType, 2);
2489 
2490     request->SetCreatorUid(3051);
2491     advancedNotificationService_->ClearSlotTypeData(request, callingUid, sourceType);
2492     ASSERT_EQ(sourceType, 2);
2493 
2494     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
2495     advancedNotificationService_->ClearSlotTypeData(request, callingUid, sourceType);
2496     ASSERT_EQ(sourceType, 2);
2497 }
2498 
2499 /**
2500  * @tc.name: IsEnableNotificationByKioskAppTrustList_001
2501  * @tc.desc: Test IsEnableNotificationByKioskAppTrustList
2502  * @tc.type: FUNC
2503  */
2504 HWTEST_F(AnsPublishServiceTest, IsEnableNotificationByKioskAppTrustList_001, Function | SmallTest | Level1)
2505 {
2506     std::string bundleName = "";
2507     bool result = advancedNotificationService_->IsEnableNotificationByKioskAppTrustList(bundleName);
2508     EXPECT_FALSE(result);
2509     bundleName = "com.test.example";
2510     NotificationPreferences::GetInstance()->preferencesInfo_.kioskAppTrustList_.emplace_back(bundleName);
2511     result = advancedNotificationService_->IsEnableNotificationByKioskAppTrustList(bundleName);
2512     EXPECT_TRUE(result);
2513 }
2514 
2515 /**
2516  * @tc.name: IsDisableNotificationByKiosk_001
2517  * @tc.desc: Test IsDisableNotificationByKiosk
2518  * @tc.type: FUNC
2519  */
2520 HWTEST_F(AnsPublishServiceTest, IsDisableNotificationByKiosk_001, Function | SmallTest | Level1)
2521 {
2522     std::string bundleName = "";
2523     bool result = advancedNotificationService_->IsDisableNotificationByKiosk(bundleName);
2524     EXPECT_FALSE(result);
2525     NotificationPreferences::GetInstance()->SetKioskModeStatus(true);
2526     result = advancedNotificationService_->IsDisableNotificationByKiosk(bundleName);
2527     EXPECT_TRUE(result);
2528     bundleName = "com.test.example";
2529     NotificationPreferences::GetInstance()->preferencesInfo_.kioskAppTrustList_.emplace_back(bundleName);
2530     result = advancedNotificationService_->IsDisableNotificationByKiosk(bundleName);
2531     EXPECT_FALSE(result);
2532 }
2533 
2534 /**
2535  * @tc.name: IsDisableNotificationForSaByKiosk_001
2536  * @tc.desc: Test IsDisableNotificationForSaByKiosk
2537  * @tc.type: FUNC
2538  */
2539 HWTEST_F(AnsPublishServiceTest, IsDisableNotificationForSaByKiosk_001, Function | SmallTest | Level1)
2540 {
2541     std::string bundleName = "com.test.example";
2542     EXPECT_FALSE(advancedNotificationService_->IsDisableNotificationForSaByKiosk(bundleName, true));
2543 
2544     bundleName = "";
2545     EXPECT_FALSE(advancedNotificationService_->IsDisableNotificationForSaByKiosk(bundleName, false));
2546 }
2547 
2548 /**
2549  * @tc.name: IsDisableNotificationForSaByKiosk_002
2550  * @tc.desc: Test IsDisableNotificationForSaByKiosk
2551  * @tc.type: FUNC
2552  */
2553 HWTEST_F(AnsPublishServiceTest, IsDisableNotificationForSaByKiosk_002, Function | SmallTest | Level1)
2554 {
2555     std::string bundleName = "com.test.example";
2556     NotificationPreferences::GetInstance()->isKioskMode_ = true;
2557     NotificationPreferences::GetInstance()->preferencesInfo_.kioskAppTrustList_.clear();
2558 
2559     EXPECT_TRUE(advancedNotificationService_->IsDisableNotificationForSaByKiosk(bundleName, false));
2560 }
2561 
2562 /**
2563  * @tc.name: IsDisableNotification_003
2564  * @tc.desc: Test IsDisableNotification
2565  * @tc.type: FUNC
2566  * @tc.require: issue
2567  */
2568 HWTEST_F(AnsPublishServiceTest, IsDisableNotification_003, Function | SmallTest | Level1)
2569 {
2570     bool defaultPolicy = system::GetBoolParameter("persist.edm.notification_disable", false);
2571     if (defaultPolicy) {
2572         system::SetBoolParameter("persist.edm.notification_disable", false);
2573     }
2574     int32_t userId = -1;
2575     EXPECT_EQ(OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId), ERR_OK);
2576     std::string bundleName = "com.testDisableNotification.example";
2577     NotificationDisable notificationDisable;
2578     std::vector<std::string> bundleList = {bundleName};
2579     notificationDisable.SetDisabled(true);
2580     notificationDisable.SetBundleList(bundleList);
2581     notificationDisable.SetUserId(userId);
2582     sptr<NotificationDisable> notificationDisablePtr = new (std::nothrow) NotificationDisable(notificationDisable);
2583     NotificationPreferences::GetInstance()->preferencesInfo_.SetDisableNotificationInfo(notificationDisablePtr);
2584     bool result = advancedNotificationService_->IsDisableNotification(bundleName);
2585     EXPECT_TRUE(result);
2586     NotificationPreferences::GetInstance()->preferencesInfo_.userDisableNotificationInfo_.clear();
2587     system::SetBoolParameter("persist.edm.notification_disable", defaultPolicy);
2588 }
2589 
2590 /**
2591  * @tc.name: IsDisableNotification_004
2592  * @tc.desc: Test IsDisableNotification
2593  * @tc.type: FUNC
2594  * @tc.require: issue
2595  */
2596 HWTEST_F(AnsPublishServiceTest, IsDisableNotification_004, Function | SmallTest | Level1)
2597 {
2598     bool defaultPolicy = system::GetBoolParameter("persist.edm.notification_disable", false);
2599     if (defaultPolicy) {
2600         system::SetBoolParameter("persist.edm.notification_disable", false);
2601     }
2602     int32_t userId = -1;
2603     EXPECT_EQ(OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId), ERR_OK);
2604     std::string bundleName = "com.testDisableNotification.example";
2605     NotificationDisable notificationDisable;
2606     std::vector<std::string> bundleList = {bundleName};
2607     notificationDisable.SetDisabled(false);
2608     notificationDisable.SetBundleList(bundleList);
2609     notificationDisable.SetUserId(userId);
2610     sptr<NotificationDisable> notificationDisablePtr = new (std::nothrow) NotificationDisable(notificationDisable);
2611     NotificationPreferences::GetInstance()->preferencesInfo_.SetDisableNotificationInfo(notificationDisablePtr);
2612     bool result = advancedNotificationService_->IsDisableNotification(bundleName);
2613     EXPECT_FALSE(result);
2614     NotificationPreferences::GetInstance()->preferencesInfo_.userDisableNotificationInfo_.clear();
2615     system::SetBoolParameter("persist.edm.notification_disable", defaultPolicy);
2616 }
2617 
2618 /**
2619  * @tc.name: IsDisableNotification_005
2620  * @tc.desc: Test IsDisableNotification
2621  * @tc.type: FUNC
2622  * @tc.require: issue
2623  */
2624 HWTEST_F(AnsPublishServiceTest, IsDisableNotification_005, Function | SmallTest | Level1)
2625 {
2626     bool defaultPolicy = system::GetBoolParameter("persist.edm.notification_disable", false);
2627     if (defaultPolicy) {
2628         system::SetBoolParameter("persist.edm.notification_disable", false);
2629     }
2630     int32_t userId = -1;
2631     EXPECT_EQ(OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId), ERR_OK);
2632     std::string bundleName = "com.testDisableNotification.example";
2633     NotificationDisable notificationDisable;
2634     notificationDisable.SetDisabled(true);
2635     notificationDisable.SetUserId(userId);
2636     sptr<NotificationDisable> notificationDisablePtr = new (std::nothrow) NotificationDisable(notificationDisable);
2637     NotificationPreferences::GetInstance()->preferencesInfo_.SetDisableNotificationInfo(notificationDisablePtr);
2638     bool result = advancedNotificationService_->IsDisableNotification(bundleName);
2639     EXPECT_FALSE(result);
2640     NotificationPreferences::GetInstance()->preferencesInfo_.userDisableNotificationInfo_.clear();
2641     system::SetBoolParameter("persist.edm.notification_disable", defaultPolicy);
2642 }
2643 
2644 /*
2645  * @tc.name: AtomicServicePublish_0100
2646  * @tc.desc: test PublishNotification with common liveView.
2647  * @tc.type: FUNC
2648  * @tc.require: #I62SME
2649  */
2650 HWTEST_F(AnsPublishServiceTest, AtomicServicePublish_0200, Function | MediumTest | Level1)
2651 {
2652     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
2653     MockIsSystemApp(true);
2654     sptr<NotificationRequest> request = new NotificationRequest(1000);
2655     std::shared_ptr<NotificationLiveViewContent> liveViewContent = std::make_shared<NotificationLiveViewContent>();
2656     liveViewContent->SetContentType(static_cast<int32_t>(NotificationContent::Type::LIVE_VIEW));
2657     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(liveViewContent);
2658     request->SetContent(content);
2659     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
2660     request->SetIsAgentNotification(true);
2661     request->SetOwnerBundleName("test.com");
2662     request->SetOwnerUserId(-1);
2663     auto extendInfo = std::make_shared<AAFwk::WantParams>();
2664     extendInfo->SetParam("autoServiceInstallStatus", AAFwk::Integer::Box(0));
2665     request->SetExtendInfo(extendInfo);
2666     auto ret = advancedNotificationService_->Publish("", request);
2667     EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM);
2668 }
2669 }  // namespace Notification
2670 }  // namespace OHOS
2671