• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <chrono>
17 #include <functional>
18 #include <memory>
19 #include <thread>
20 
21 #include "gtest/gtest.h"
22 
23 #define private public
24 #include "advanced_notification_service.h"
25 #include "ans_inner_errors.h"
26 #include "ans_log_wrapper.h"
27 #include "accesstoken_kit.h"
28 #include "notification_preferences.h"
29 #include "notification_constant.h"
30 #include "notification_config_parse.h"
31 #include "ipc_skeleton.h"
32 
33 using namespace testing::ext;
34 using namespace OHOS::Security::AccessToken;
35 
36 namespace OHOS {
37 namespace Notification {
38 extern void MockIsVerfyPermisson(bool isVerify);
39 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
40 extern void MockIsSystemApp(bool isSystemApp);
41 extern void MockIsNonBundleName(bool isNonBundleName);
42 
43 class AnsSlotServiceTest : public testing::Test {
44 public:
45     static void SetUpTestCase();
46     static void TearDownTestCase();
47     void SetUp();
48     void TearDown();
49 
50 private:
51     void TestAddSlot(NotificationConstant::SlotType type);
52 
53 private:
54     static sptr<AdvancedNotificationService> advancedNotificationService_;
55 };
56 
57 sptr<AdvancedNotificationService> AnsSlotServiceTest::advancedNotificationService_ = nullptr;
58 
SetUpTestCase()59 void AnsSlotServiceTest::SetUpTestCase() {}
60 
TearDownTestCase()61 void AnsSlotServiceTest::TearDownTestCase() {}
62 
SetUp()63 void AnsSlotServiceTest::SetUp()
64 {
65     GTEST_LOG_(INFO) << "SetUp start";
66 
67     advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService();
68     NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
69     advancedNotificationService_->CancelAll("");
70     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
71     MockIsSystemApp(true);
72     GTEST_LOG_(INFO) << "SetUp end";
73 }
74 
TearDown()75 void AnsSlotServiceTest::TearDown()
76 {
77     delete advancedNotificationService_;
78     advancedNotificationService_ = nullptr;
79     GTEST_LOG_(INFO) << "TearDown";
80 }
81 
TestAddSlot(NotificationConstant::SlotType type)82 void AnsSlotServiceTest::TestAddSlot(NotificationConstant::SlotType type)
83 {
84     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
85     MockIsSystemApp(true);
86     MockIsVerfyPermisson(true);
87     std::vector<sptr<NotificationSlot>> slots;
88     sptr<NotificationSlot> slot = new NotificationSlot(type);
89     slot->SetEnable(true);
90     slots.push_back(slot);
91     ASSERT_EQ(advancedNotificationService_->AddSlots(slots), (int)ERR_OK);
92 }
93 
94 /**
95  * @tc.name: AddSlots_00001
96  * @tc.desc: Test AddSlots
97  * @tc.type: FUNC
98  * @tc.require: issue
99  */
100 HWTEST_F(AnsSlotServiceTest, AddSlots_00001, Function | SmallTest | Level1)
101 {
102     MockIsSystemApp(true);
103     MockIsVerfyPermisson(true);
104     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::CUSTOMER_SERVICE;
105     std::vector<sptr<NotificationSlot>> slots;
106     sptr<NotificationSlot> slot = new NotificationSlot(slotType);
107     slots.push_back(slot);
108     advancedNotificationService_->notificationSvrQueue_ = nullptr;
109     ASSERT_EQ(advancedNotificationService_->AddSlots(slots), (int)ERR_ANS_INVALID_PARAM);
110 }
111 
112 /**
113  * @tc.name: GetSlots_00001
114  * @tc.desc: Test GetSlots
115  * @tc.type: FUNC
116  * @tc.require: issue
117  */
118 HWTEST_F(AnsSlotServiceTest, GetSlots_00001, Function | SmallTest | Level1)
119 {
120     std::vector<sptr<NotificationSlot>> slots;
121     advancedNotificationService_->notificationSvrQueue_ = nullptr;
122     ASSERT_EQ(advancedNotificationService_->GetSlots(slots), (int)ERR_ANS_INVALID_PARAM);
123 }
124 
125 /**
126  * @tc.name: GetSlotsByBundle_00001
127  * @tc.desc: Test GetSlotsByBundle
128  * @tc.type: FUNC
129  * @tc.require: issue
130  */
131 HWTEST_F(AnsSlotServiceTest, GetSlotsByBundle_00001, Function | SmallTest | Level1)
132 {
133     MockIsSystemApp(true);
134     MockIsVerfyPermisson(true);
135     std::vector<sptr<NotificationSlot>> slots;
136     sptr<NotificationBundleOption> bundle = nullptr;
137     ASSERT_EQ(advancedNotificationService_->GetSlotsByBundle(bundle, slots), (int)ERR_ANS_INVALID_BUNDLE);
138 }
139 
140 /**
141  * @tc.name: GetSlotsByBundle_00002
142  * @tc.desc: Test GetSlotsByBundle
143  * @tc.type: FUNC
144  * @tc.require: issue
145  */
146 HWTEST_F(AnsSlotServiceTest, GetSlotsByBundle_00002, Function | SmallTest | Level1)
147 {
148     MockIsSystemApp(true);
149     MockIsVerfyPermisson(true);
150     std::vector<sptr<NotificationSlot>> slots;
151     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
152     advancedNotificationService_->notificationSvrQueue_ = nullptr;
153     ASSERT_EQ(advancedNotificationService_->GetSlotsByBundle(bundle, slots), (int)ERR_ANS_INVALID_PARAM);
154 }
155 
156 /**
157  * @tc.name: GetSlotsByBundle_00003
158  * @tc.desc: Test GetSlotsByBundle
159  * @tc.type: FUNC
160  * @tc.require: issue
161  */
162 HWTEST_F(AnsSlotServiceTest, GetSlotsByBundle_00003, Function | SmallTest | Level1)
163 {
164     MockIsSystemApp(true);
165     MockIsVerfyPermisson(true);
166     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption("bundle", 100);
167     advancedNotificationService_->SetSilentReminderEnabled(bundleOption, true);
168     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::CUSTOMER_SERVICE;
169     std::vector<sptr<NotificationSlot>> slots;
170     sptr<NotificationSlot> slot = new NotificationSlot(slotType);
171     slots.push_back(slot);
172     std::vector<sptr<NotificationSlot>> slotRes;
173     ASSERT_EQ(advancedNotificationService_->GetSlotsByBundle(bundleOption, slotRes), (int)ERR_OK);
174 }
175 
176 /**
177  * @tc.name: UpdateSlots_00001
178  * @tc.desc: Test UpdateSlots
179  * @tc.type: FUNC
180  * @tc.require: issue
181  */
182 HWTEST_F(AnsSlotServiceTest, UpdateSlots_00001, Function | SmallTest | Level1)
183 {
184     MockIsSystemApp(true);
185     MockIsVerfyPermisson(true);
186     std::vector<sptr<NotificationSlot>> slots;
187     sptr<NotificationBundleOption> bundle = nullptr;
188     ASSERT_EQ(advancedNotificationService_->UpdateSlots(bundle, slots), (int)ERR_ANS_INVALID_BUNDLE);
189 
190     bundle = new NotificationBundleOption("test", 1);
191     advancedNotificationService_->notificationSvrQueue_ = nullptr;
192     ASSERT_EQ(advancedNotificationService_->UpdateSlots(bundle, slots), (int)ERR_ANS_INVALID_PARAM);
193 }
194 
195 /**
196  * @tc.name: UpdateSlots_00002
197  * @tc.desc: Test UpdateSlots
198  * @tc.type: FUNC
199  * @tc.require: issue
200  */
201 HWTEST_F(AnsSlotServiceTest, UpdateSlots_00002, Function | SmallTest | Level1)
202 {
203     MockIsSystemApp(true);
204     MockIsVerfyPermisson(true);
205     std::vector<sptr<NotificationSlot>> slots;
206     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::LIVE_VIEW);
207     slot->SetEnable(true);
208     slots.push_back(slot);
209 
210     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
211     auto ret = advancedNotificationService_->UpdateSlots(bundle, slots);
212     ASSERT_EQ(ret, (int)ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST);
213 }
214 
215 /**
216  * @tc.name: RemoveAllSlots_00001
217  * @tc.desc: Test RemoveAllSlots
218  * @tc.type: FUNC
219  * @tc.require: issue
220  */
221 HWTEST_F(AnsSlotServiceTest, RemoveAllSlots_00001, Function | SmallTest | Level1)
222 {
223     advancedNotificationService_->notificationSvrQueue_ = nullptr;
224     auto ret = advancedNotificationService_->RemoveAllSlots();
225     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
226 }
227 
228 /**
229  * @tc.name: RemoveAllSlots_00002
230  * @tc.desc: Test RemoveAllSlots
231  * @tc.type: FUNC
232  * @tc.require: issue
233  */
234 HWTEST_F(AnsSlotServiceTest, RemoveAllSlots_00002, Function | SmallTest | Level1)
235 {
236     TestAddSlot(NotificationConstant::SlotType::LIVE_VIEW);
237     auto ret = advancedNotificationService_->RemoveAllSlots();
238     ASSERT_EQ(ret, (int)ERR_OK);
239 }
240 
241 /**
242  * @tc.name: GetEnabledForBundleSlotSelf_00001
243  * @tc.desc: Test GetEnabledForBundleSlotSelf
244  * @tc.type: FUNC
245  * @tc.require: issue
246  */
247 HWTEST_F(AnsSlotServiceTest, GetEnabledForBundleSlotSelf_00001, Function | SmallTest | Level1)
248 {
249     auto slotType = NotificationConstant::SlotType::CONTENT_INFORMATION;
250     TestAddSlot(slotType);
251 
252     bool enable = false;
253     auto res = advancedNotificationService_->GetEnabledForBundleSlotSelf(slotType, enable);
254     ASSERT_EQ(res, (int)ERR_OK);
255     ASSERT_TRUE(enable);
256 
257     advancedNotificationService_->notificationSvrQueue_ = nullptr;
258     enable = false;
259     res = advancedNotificationService_->GetEnabledForBundleSlotSelf(slotType, enable);
260     ASSERT_EQ(res, (int)ERR_ANS_INVALID_PARAM);
261     ASSERT_FALSE(enable);
262 }
263 
264 /**
265  * @tc.name: SetAdditionConfig_00001
266  * @tc.desc: Test SetAdditionConfig_00001
267  * @tc.type: FUNC
268  */
269 HWTEST_F(AnsSlotServiceTest, SetAdditionConfig_00001, Function | SmallTest | Level1)
270 {
271     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
272     MockIsSystemApp(false);
273     MockIsVerfyPermisson(false);
274     std::string key = RING_TRUST_PKG_KEY;
275     std::string value = "";
276     auto ret = advancedNotificationService_->SetAdditionConfig(key, value);
277     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
278 
279     MockIsSystemApp(true);
280     MockIsVerfyPermisson(true);
281     ret = advancedNotificationService_->SetAdditionConfig(key, value);
282     ASSERT_EQ(ret, (int)ERR_OK);
283 
284     advancedNotificationService_->notificationSvrQueue_ = nullptr;
285     ret = advancedNotificationService_->SetAdditionConfig(key, value);
286     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
287 }
288 
289 /**
290  * @tc.name: SetAdditionConfig_00002
291  * @tc.desc: Test SetAdditionConfig_00002
292  * @tc.type: FUNC
293  */
294 HWTEST_F(AnsSlotServiceTest, SetAdditionConfig_00002, Function | SmallTest | Level1)
295 {
296     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
297     MockIsSystemApp(true);
298     MockIsVerfyPermisson(true);
299     std::string key = "NOTIFICATION_CTL_LIST_PKG";
300     std::string value = "";
301 
302     auto ret = advancedNotificationService_->SetAdditionConfig(key, value);
303     ASSERT_EQ(ret, (int)ERR_OK);
304 }
305 
306 /**
307  * @tc.name: GetAllLiveViewEnabledBundles_00001
308  * @tc.desc: Test GetAllLiveViewEnabledBundles_00001
309  * @tc.type: FUNC
310  */
311 HWTEST_F(AnsSlotServiceTest, GetAllLiveViewEnabledBundles_00001, Function | SmallTest | Level1)
312 {
313     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
314     MockIsSystemApp(false);
315     MockIsVerfyPermisson(false);
316     std::vector<NotificationBundleOption> bundleOptions;
317     NotificationBundleOption bundleOption("GetAllLiveViewEnabledBundles_00001", 999);
318     bundleOptions.push_back(bundleOption);
319     auto ret = advancedNotificationService_->GetAllLiveViewEnabledBundles(bundleOptions);
320     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
321 
322     MockIsSystemApp(true);
323     MockIsVerfyPermisson(true);
324     ret = advancedNotificationService_->GetAllLiveViewEnabledBundles(bundleOptions);
325     ASSERT_EQ(ret, (int)ERR_OK);
326 
327     advancedNotificationService_->notificationSvrQueue_ = nullptr;
328     ret = advancedNotificationService_->GetAllLiveViewEnabledBundles(bundleOptions);
329     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
330 }
331 
332 /**
333  * @tc.name: GetEnabledForBundleSlot_00001
334  * @tc.desc: Test UpdateSlots
335  * @tc.type: FUNC
336  * @tc.require: issue
337  */
338 HWTEST_F(AnsSlotServiceTest, GetEnabledForBundleSlot_00001, Function | SmallTest | Level1)
339 {
340     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
341     MockIsSystemApp(true);
342     MockIsVerfyPermisson(true);
343     sptr<NotificationBundleOption> bundleOption(
344         new NotificationBundleOption("GetEnabledForBundleSlot_00001", 777));
345     auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
346     bool enanle = true;
347     advancedNotificationService_->notificationSvrQueue_= nullptr;
348     auto ret = advancedNotificationService_->GetEnabledForBundleSlot(
349         bundleOption, slotType, enanle);
350     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
351 }
352 
353 
354 /**
355  * @tc.name: GetSlotFlagsAsBundle_00001
356  * @tc.desc: Test GetSlotFlagsAsBundle
357  * @tc.type: FUNC
358  * @tc.require: issue
359  */
360 HWTEST_F(AnsSlotServiceTest, GetSlotFlagsAsBundle_00001, Function | SmallTest | Level1)
361 {
362     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
363     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
364     MockIsSystemApp(false);
365     auto ret = advancedNotificationService_->SetSlotFlagsAsBundle(bundle, 1);
366     ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
367 
368     uint32_t flag = 0;
369     ret = advancedNotificationService_->GetSlotFlagsAsBundle(bundle, flag);
370     ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
371 }
372 
373 /**
374  * @tc.name: GetSlotFlagsAsBundle_00002
375  * @tc.desc: Test GetSlotFlagsAsBundle
376  * @tc.type: FUNC
377  * @tc.require: issue
378  */
379 HWTEST_F(AnsSlotServiceTest, GetSlotFlagsAsBundle_00002, Function | SmallTest | Level1)
380 {
381     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
382     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
383     MockIsSystemApp(true);
384     MockIsVerfyPermisson(false);
385     auto ret = advancedNotificationService_->SetSlotFlagsAsBundle(bundle, 1);
386     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
387 
388     uint32_t flag = 0;
389     ret = advancedNotificationService_->GetSlotFlagsAsBundle(bundle, flag);
390     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
391 }
392 
393 /**
394  * @tc.name: GetSlotFlagsAsBundle_00003
395  * @tc.desc: Test GetSlotFlagsAsBundle
396  * @tc.type: FUNC
397  * @tc.require: issue
398  */
399 HWTEST_F(AnsSlotServiceTest, GetSlotFlagsAsBundle_00003, Function | SmallTest | Level1)
400 {
401     sptr<NotificationBundleOption> bundle = nullptr;
402     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
403     MockIsSystemApp(true);
404     MockIsVerfyPermisson(true);
405     auto ret = advancedNotificationService_->SetSlotFlagsAsBundle(bundle, 1);
406     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
407 
408     uint32_t flag = 0;
409     ret = advancedNotificationService_->GetSlotFlagsAsBundle(bundle, flag);
410     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
411 }
412 
413 /**
414  * @tc.name: GetSlotFlagsAsBundle_00004
415  * @tc.desc: Test GetSlotFlagsAsBundle
416  * @tc.type: FUNC
417  * @tc.require: issue
418  */
419 HWTEST_F(AnsSlotServiceTest, GetSlotFlagsAsBundle_00004, Function | SmallTest | Level1)
420 {
421     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
422     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
423     MockIsSystemApp(true);
424     MockIsVerfyPermisson(true);
425     advancedNotificationService_->notificationSvrQueue_ = nullptr;
426     auto ret = advancedNotificationService_->SetSlotFlagsAsBundle(bundle, 1);
427     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
428 
429     uint32_t flag = 0;
430     ret = advancedNotificationService_->GetSlotFlagsAsBundle(bundle, flag);
431     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
432 }
433 
434 /**
435  * @tc.name: AddSlotByType_00001
436  * @tc.desc: Test AddSlotByType
437  * @tc.type: FUNC
438  * @tc.require: issue
439  */
440 HWTEST_F(AnsSlotServiceTest, AddSlotByType_00001, Function | SmallTest | Level1)
441 {
442     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
443     MockIsSystemApp(false);
444     MockIsVerfyPermisson(false);
445 
446     auto ret = advancedNotificationService_->AddSlotByType(
447         NotificationConstant::SlotType::EMERGENCY_INFORMATION);
448     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
449 
450     ret = advancedNotificationService_->AddSlotByType(
451         NotificationConstant::SlotType::LIVE_VIEW);
452     ASSERT_EQ(ret, (int)ERR_OK);
453 
454     ret = advancedNotificationService_->AddSlotByType(
455         NotificationConstant::SlotType::LIVE_VIEW);
456     ASSERT_EQ(ret, (int)ERR_OK);
457 }
458 
459 /**
460  * @tc.name: GetSlotFlagsAsBundle_00005
461  * @tc.desc: Test GetSlotFlagsAsBundle
462  * @tc.type: FUNC
463  * @tc.require: issue
464  */
465 HWTEST_F(AnsSlotServiceTest, GetSlotFlagsAsBundle_00005, Function | SmallTest | Level1)
466 {
467     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
468     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
469     MockIsSystemApp(true);
470     MockIsVerfyPermisson(true);
471     auto ret = advancedNotificationService_->SetSlotFlagsAsBundle(bundle, 1);
472     ASSERT_EQ(ret, (int)ERR_OK);
473 
474     uint32_t flag = 0;
475     ret = advancedNotificationService_->GetSlotFlagsAsBundle(bundle, flag);
476     ASSERT_EQ(ret, (int)ERR_OK);
477     ASSERT_EQ(flag, 1);
478 }
479 
480 /**
481  * @tc.name: GetSlotFlagsAsBundle_00004
482  * @tc.desc: Test GetSlotFlagsAsBundle
483  * @tc.type: FUNC
484  * @tc.require: issue
485  */
486 HWTEST_F(AnsSlotServiceTest, GetSlotFlagsAsBundle_00006, Function | SmallTest | Level1)
487 {
488     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test888", 888);
489     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
490     MockIsSystemApp(true);
491     MockIsVerfyPermisson(true);
492 
493     uint32_t flag = 0;
494     auto ret = advancedNotificationService_->GetSlotFlagsAsBundle(bundle, flag);
495     ASSERT_EQ(ret, (int)ERR_OK);
496     ASSERT_EQ(flag, DEFAULT_SLOT_FLAGS);
497 }
498 
499 /**
500  * @tc.name: SetRequestBySlotType_00001
501  * @tc.desc: Test SetRequestBySlotType
502  * @tc.type: FUNC
503  * @tc.require: issue
504  */
505 HWTEST_F(AnsSlotServiceTest, SetRequestBySlotType_00001, Function | SmallTest | Level1)
506 {
507     sptr<NotificationRequest> request = new NotificationRequest();
508     request->SetSlotType(NotificationConstant::SlotType::CUSTOMER_SERVICE);
509     sptr<NotificationBundleOption> bundle = nullptr;
510     advancedNotificationService_->SetRequestBySlotType(request, bundle);
511     EXPECT_NE(request->GetFlags(), nullptr);
512 }
513 
514 /**
515  * @tc.name: SetRequestBySlotType_00002
516  * @tc.desc: Test SetRequestBySlotType
517  * @tc.type: FUNC
518  * @tc.require: issue
519  */
520 HWTEST_F(AnsSlotServiceTest, SetRequestBySlotType_00002, Function | SmallTest | Level1)
521 {
522     sptr<NotificationBundleOption> bundle(new NotificationBundleOption("test009", 9));
523     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::SERVICE_REMINDER;
524     sptr<NotificationSlot> slot(new (std::nothrow) NotificationSlot(slotType));
525     slot->SetReminderMode(63);
526     std::vector<sptr<NotificationSlot>> slots;
527     slots.push_back(slot);
528     auto ret = NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots);
529     ASSERT_EQ(ret, (int)ERR_OK);
530 
531     sptr<NotificationRequest> request = new NotificationRequest();
532     request->SetSlotType(NotificationConstant::SlotType::SERVICE_REMINDER);
533     advancedNotificationService_->SetRequestBySlotType(request, bundle);
534 
535     auto flagSptr = request->GetFlags();
536     ASSERT_NE(flagSptr, nullptr);
537     ASSERT_EQ(flagSptr->GetReminderFlags(), 63);
538 }
539 
540 
541 /**
542  * @tc.name: GetSlotByType_00001
543  * @tc.desc: Test GetSlotByType
544  * @tc.type: FUNC
545  * @tc.require: issue
546  */
547 HWTEST_F(AnsSlotServiceTest, GetSlotByType_00001, Function | SmallTest | Level1)
548 {
549     sptr<NotificationSlot> slot;
550     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::CUSTOMER_SERVICE;
551     advancedNotificationService_->notificationSvrQueue_ = nullptr;
552     auto ret = advancedNotificationService_->AddSlotByType(slotType);
553     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
554 
555     ret = advancedNotificationService_->GetSlotByType(slotType, slot);
556     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
557 }
558 
559 /**
560  * @tc.name: RemoveSlotByType_00001
561  * @tc.desc: Test RemoveSlotByType
562  * @tc.type: FUNC
563  * @tc.require: issue
564  */
565 HWTEST_F(AnsSlotServiceTest, RemoveSlotByType_00001, Function | SmallTest | Level1)
566 {
567     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::CUSTOMER_SERVICE;
568     advancedNotificationService_->notificationSvrQueue_ = nullptr;
569     auto ret = advancedNotificationService_->RemoveSlotByType(slotType);
570     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
571 }
572 
573 /**
574  * @tc.name: RemoveSlotByType_00002
575  * @tc.desc: Test RemoveSlotByType
576  * @tc.type: FUNC
577  * @tc.require: issue
578  */
579 HWTEST_F(AnsSlotServiceTest, RemoveSlotByType_00002, Function | SmallTest | Level1)
580 {
581     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
582     MockIsSystemApp(true);
583     MockIsVerfyPermisson(true);
584     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::LIVE_VIEW;
585     std::vector<sptr<NotificationSlot>> slots;
586     sptr<NotificationSlot> slot = new NotificationSlot(slotType);
587     slot->SetForceControl(true);
588     slots.push_back(slot);
589     ASSERT_EQ(advancedNotificationService_->AddSlots(slots), (int)ERR_OK);
590 
591     MockIsSystemApp(false);
592     auto ret = advancedNotificationService_->RemoveSlotByType(slotType);
593     ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
594 }
595 
596 /**
597  * @tc.name: GetSlotNumAsBundle_00001
598  * @tc.desc: Test GetSlotNumAsBundle
599  * @tc.type: FUNC
600  * @tc.require: issue
601  */
602 HWTEST_F(AnsSlotServiceTest, GetSlotNumAsBundle_00001, Function | SmallTest | Level1)
603 {
604     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
605     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
606     MockIsSystemApp(true);
607     MockIsVerfyPermisson(true);
608     advancedNotificationService_->notificationSvrQueue_ = nullptr;
609     uint64_t num = 0;
610     auto ret = advancedNotificationService_->GetSlotNumAsBundle(bundle, num);
611     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
612 }
613 
614 /**
615  * @tc.name: GetSlotByBundle_00001
616  * @tc.desc: Test GetSlotByBundle
617  * @tc.type: FUNC
618  * @tc.require: issue
619  */
620 HWTEST_F(AnsSlotServiceTest, GetSlotByBundle_00001, Function | SmallTest | Level1)
621 {
622     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::CUSTOMER_SERVICE;
623     sptr<NotificationBundleOption> bundle = nullptr;
624     sptr<NotificationSlot> slot = nullptr;
625     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
626     MockIsSystemApp(false);
627     auto ret = advancedNotificationService_->GetSlotByBundle(bundle, slotType, slot);
628     ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
629 }
630 
631 /**
632  * @tc.name: GetSlotByBundle_00002
633  * @tc.desc: Test GetSlotByBundle
634  * @tc.type: FUNC
635  * @tc.require: issue
636  */
637 HWTEST_F(AnsSlotServiceTest, GetSlotByBundle_00002, Function | SmallTest | Level1)
638 {
639     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::CUSTOMER_SERVICE;
640     sptr<NotificationBundleOption> bundle = nullptr;
641     sptr<NotificationSlot> slot = nullptr;
642     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
643     MockIsSystemApp(true);
644     MockIsVerfyPermisson(false);
645 
646     auto ret = advancedNotificationService_->GetSlotByBundle(bundle, slotType, slot);
647     ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
648 
649     MockIsVerfyPermisson(true);
650     ret = advancedNotificationService_->GetSlotByBundle(bundle, slotType, slot);
651     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
652 
653     bundle = new NotificationBundleOption("test", 1);
654     ret = advancedNotificationService_->GetSlotByBundle(bundle, slotType, slot);
655     ASSERT_EQ(ret, (int)ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST);
656 }
657 
658 /**
659  * @tc.name: GetSlotByBundle_00003
660  * @tc.desc: Test GetSlotByBundle
661  * @tc.type: FUNC
662  * @tc.require: issue
663  */
664 HWTEST_F(AnsSlotServiceTest, GetSlotByBundle_00003, Function | SmallTest | Level1)
665 {
666     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
667     MockIsSystemApp(true);
668     MockIsVerfyPermisson(true);
669 
670     sptr<NotificationBundleOption> bundle(new NotificationBundleOption("test99", 999));
671     sptr<NotificationSlot> slot(
672         new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::CUSTOMER_SERVICE));
673     std::vector<sptr<NotificationSlot>> slots;
674     slots.push_back(slot);
675     auto ret = NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots);
676     ASSERT_EQ(ret, (int)ERR_OK);
677 
678     sptr<NotificationSlot> slotOut;
679     NotificationConstant::SlotType slotTypeTemp = NotificationConstant::SlotType::ILLEGAL_TYPE;
680     ret = advancedNotificationService_->GetSlotByBundle(bundle, slotTypeTemp, slotOut);
681     ASSERT_EQ(ret, (int)ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST);
682     ASSERT_EQ(slotOut, nullptr);
683 
684     advancedNotificationService_->notificationSvrQueue_ = nullptr;
685     ret = advancedNotificationService_->GetSlotByBundle(bundle, slotTypeTemp, slot);
686     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
687 }
688 
689 
690 /**
691  * @tc.name: UpdateSlotReminderModeBySlotFlags_00001
692  * @tc.desc: Test UpdateSlotReminderModeBySlotFlags
693  * @tc.type: FUNC
694  * @tc.require: issue
695  */
696 HWTEST_F(AnsSlotServiceTest, UpdateSlotReminderModeBySlotFlags_00001, Function | SmallTest | Level1)
697 {
698     uint32_t slotFlags = 0b111011;
699     sptr<NotificationBundleOption> bundle = nullptr;
700     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
701     MockIsSystemApp(false);
702     auto ret = advancedNotificationService_->UpdateSlotReminderModeBySlotFlags(bundle, slotFlags);
703     ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
704 }
705 
706 /**
707  * @tc.name: UpdateSlotReminderModeBySlotFlags_00002
708  * @tc.desc: Test UpdateSlotReminderModeBySlotFlags
709  * @tc.type: FUNC
710  * @tc.require: issue
711  */
712 HWTEST_F(AnsSlotServiceTest, UpdateSlotReminderModeBySlotFlags_00002, Function | SmallTest | Level1)
713 {
714     uint32_t slotFlags = 0b000011;
715     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
716     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
717     MockIsSystemApp(true);
718     MockIsVerfyPermisson(true);
719 
720     sptr<NotificationSlot> slot = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::CUSTOMER_SERVICE);
721     advancedNotificationService_->GenerateSlotReminderMode(slot, bundle);
722     std::vector<sptr<NotificationSlot>> slots;
723     slots.push_back(slot);
724     NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots);
725 
726     auto ret = advancedNotificationService_->UpdateSlotReminderModeBySlotFlags(bundle, slotFlags);
727     ASSERT_EQ(ret, (int)ERR_OK);
728 }
729 
730 /**
731  * @tc.name: UpdateSlotReminderModeBySlotFlags_00003
732  * @tc.desc: Test UpdateSlotReminderModeBySlotFlags
733  * @tc.type: FUNC
734  * @tc.require: issue
735  */
736 HWTEST_F(AnsSlotServiceTest, UpdateSlotReminderModeBySlotFlags_00003, Function | SmallTest | Level1)
737 {
738     uint32_t slotFlags = 0b000011;
739     sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test555", 555);
740 
741     auto ret = advancedNotificationService_->UpdateSlotReminderModeBySlotFlags(bundle, slotFlags);
742     ASSERT_EQ(ret, (int)ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST);
743 }
744 
745 /**
746  * @tc.name: GetDefaultSlotFlags_00001
747  * @tc.desc: Test GetDefaultSlotFlags_00001
748  * @tc.type: FUNC
749  * @tc.require: issue
750  */
751 HWTEST_F(AnsSlotServiceTest, GetDefaultSlotFlags_00001, Function | SmallTest | Level1)
752 {
753     sptr<NotificationRequest> request(new NotificationRequest(1));
754     uint32_t notificationControlFlags = NotificationConstant::ReminderFlag::SA_SELF_BANNER_FLAG;
755     request->SetNotificationControlFlags(notificationControlFlags);
756     request->SetCreatorUid(IPCSkeleton::GetCallingUid());
757     auto ret = advancedNotificationService_->GetDefaultSlotFlags(request);
758     ASSERT_EQ(ret, 63);
759 }
760 
761 /**
762  * @tc.name: GenerateSlotReminderMode_00001
763  * @tc.desc: Test GenerateSlotReminderMode
764  * @tc.type: FUNC
765  * @tc.require: issue
766  */
767 HWTEST_F(AnsSlotServiceTest, GenerateSlotReminderMode_00001, Function | SmallTest | Level1)
768 {
769     sptr<NotificationBundleOption> bundle = nullptr;
770     sptr<NotificationSlot> slot = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
771     advancedNotificationService_->GenerateSlotReminderMode(slot, bundle);
772     ASSERT_EQ(slot->GetReminderMode(), (int)0b111011);
773 }
774 
775 /**
776  * @tc.name: GenerateSlotReminderMode_00002
777  * @tc.desc: Test GenerateSlotReminderMode
778  * @tc.type: FUNC
779  * @tc.require: issue
780  */
781 HWTEST_F(AnsSlotServiceTest, GenerateSlotReminderMode_00002, Function | SmallTest | Level1)
782 {
783     sptr<NotificationBundleOption> bundle = nullptr;
784     sptr<NotificationSlot> slot = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
785     advancedNotificationService_->GenerateSlotReminderMode(slot, bundle, true);
786     ASSERT_EQ(slot->GetReminderMode(), (int)0b111011);
787 }
788 
789 /**
790  * @tc.name: GetConfigSlotReminderModeByType_00001
791  * @tc.desc: Test GenerateSlotReminderMode
792  * @tc.type: FUNC
793  * @tc.require: issue
794  */
795 HWTEST_F(AnsSlotServiceTest, GetConfigSlotReminderModeByType_00001, Function | SmallTest | Level1)
796 {
797     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::SERVICE_REMINDER;
798     auto reminderMode =
799         DelayedSingleton<NotificationConfigParse>::GetInstance()->GetConfigSlotReminderModeByType(slotType);
800     ASSERT_EQ(reminderMode, (int)0b111111);
801 }
802 
803 /**
804  * @tc.name: GetNotificationSettings_00001
805  * @tc.desc: Verify that bits except for the 0th and 4th are 0
806  * @tc.type: FUNC
807  * @tc.require: issue
808  */
809 HWTEST_F(AnsSlotServiceTest, GetNotificationSettings_00001, Function | SmallTest | Level1)
810 {
811     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
812     MockIsSystemApp(false);
813 
814     uint32_t flag = 0xff;
815     auto ret = advancedNotificationService_->GetNotificationSettings(flag);
816     ASSERT_EQ(ret, ERR_OK);
817     // invalid outside of the 0th and 4th position
818     uint32_t result = flag & 0xee;
819     EXPECT_EQ(result, 0);
820 }
821 
822 /**
823  * @tc.name: GetNotificationSettings_00002
824  * @tc.desc: Test GetNotificationSettings
825  * @tc.type: FUNC
826  */
827 HWTEST_F(AnsSlotServiceTest, GetNotificationSettings_00002, Function | SmallTest | Level1)
828 {
829     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
830     MockIsSystemApp(true);
831     MockIsVerfyPermisson(true);
832     sptr<NotificationBundleOption> bundleOption = advancedNotificationService_->GenerateBundleOption();
833     ASSERT_NE(bundleOption, nullptr);
834 
835     uint32_t flag = 0xff;
836     auto ret = advancedNotificationService_->SetSlotFlagsAsBundle(bundleOption, flag);
837     ASSERT_EQ(ret, ERR_OK);
838 
839     ret = advancedNotificationService_->GetNotificationSettings(flag);
840     ASSERT_EQ(ret, ERR_OK);
841     EXPECT_EQ(flag, 0x11);
842 }
843 
844 /**
845  * @tc.name: GetNotificationSettings_00003
846  * @tc.desc: Test GetNotificationSettings when notificationSvrQueue is nullptr
847  * @tc.type: FUNC
848  */
849 HWTEST_F(AnsSlotServiceTest, GetNotificationSettings_00003, Function | SmallTest | Level1)
850 {
851     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
852     MockIsSystemApp(false);
853     MockIsVerfyPermisson(false);
854     auto advancedNotificationService = std::make_shared<AdvancedNotificationService>();
855     ASSERT_NE(advancedNotificationService, nullptr);
856     advancedNotificationService->notificationSvrQueue_ = nullptr;
857     uint32_t flag;
858     auto ret = advancedNotificationService->GetNotificationSettings(flag);
859     MockIsSystemApp(true);
860     MockIsVerfyPermisson(true);
861     EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM);
862 }
863 
864 /**
865  * @tc.name: GetNotificationSettings_00004
866  * @tc.desc: Test GetNotificationSettings when bundleName is empty
867  * @tc.type: FUNC
868  */
869 HWTEST_F(AnsSlotServiceTest, GetNotificationSettings_00004, Function | SmallTest | Level1)
870 {
871     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_INVALID);
872     MockIsNonBundleName(true);
873     uint32_t flag;
874     auto ret = advancedNotificationService_->GetNotificationSettings(flag);
875     MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
876     MockIsNonBundleName(false);
877     EXPECT_EQ(ret, ERR_ANS_INVALID_BUNDLE);
878 }
879 
880 /**
881  * @tc.name: AssignValidNotificationSlot_00001
882  * @tc.desc: Test AssignValidNotificationSlot_00001
883  * @tc.type: FUNC
884  */
885 HWTEST_F(AnsSlotServiceTest, AssignValidNotificationSlot_00001, Function | SmallTest | Level1)
886 {
887     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption("test666", 666));
888     std::shared_ptr<NotificationRecord> record = std::make_shared<NotificationRecord>();
889     sptr<NotificationRequest> request(new NotificationRequest(1));
890     request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
891     auto liveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
892     auto content = std::make_shared<NotificationContent>(liveViewContent);
893     request->SetContent(content);
894     record->request = request;
895 
896     uint32_t flag;
897     auto ret = advancedNotificationService_->AssignValidNotificationSlot(record, bundleOption);
898     ASSERT_EQ(ret, ERR_OK);
899 }
900 
901 /**
902  * @tc.name: SetEnabledForBundleSlotInner_00001
903  * @tc.desc: Test SetEnabledForBundleSlotInner_00001
904  * @tc.type: FUNC
905  */
906 HWTEST_F(AnsSlotServiceTest, SetEnabledForBundleSlotInner_00001, Function | SmallTest | Level1)
907 {
908     sptr<NotificationSlot> slot(new NotificationSlot());
909     slot->SetEnable(true);
910     slot->SetForceControl(true);
911     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption("test6666", 6666));
912     sptr<NotificationBundleOption> bundle(new NotificationBundleOption("test7777", 7777));
913     NotificationConstant::SlotType slotType = NotificationConstant::SlotType::SERVICE_REMINDER;
914 
915     auto ret = advancedNotificationService_->SetEnabledForBundleSlotInner(
916         bundleOption, bundle, slotType, true, true);
917     ASSERT_EQ(ret, ERR_OK);
918 
919     ret = advancedNotificationService_->SetEnabledForBundleSlotInner(
920         bundleOption, bundle, slotType, true, true);
921     ASSERT_EQ(ret, ERR_OK);
922 }
923 
924 /**
925  * @tc.name: PublishSlotChangeCommonEvent_00001
926  * @tc.desc: Test PublishSlotChangeCommonEvent_00001
927  * @tc.type: FUNC
928  */
929 HWTEST_F(AnsSlotServiceTest, PublishSlotChangeCommonEvent_00001, Function | SmallTest | Level1)
930 {
931     auto ret = advancedNotificationService_->PublishSlotChangeCommonEvent(nullptr);
932     ASSERT_FALSE(ret);
933 }
934 }  // namespace Notification
935 }  // namespace OHOS
936