1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <thread>
17 #include "gtest/gtest.h"
18
19 #define private public
20
21 #include "advanced_notification_service.h"
22 #include "advanced_datashare_helper.h"
23 #include "notification_check_request.h"
24
25 #include "ans_ut_constant.h"
26 #include "mock_ipc_skeleton.h"
27 #include "mock_bundle_mgr.h"
28 #include "mock_accesstoken_kit.h"
29 #include "mock_time_service_client.h"
30 #include "mock_datashare.h"
31
32 #include "bool_wrapper.h"
33 #include "string_wrapper.h"
34 #include "mock_push_callback_stub.h"
35
36 using namespace testing::ext;
37 using namespace OHOS::Security::AccessToken;
38
39 namespace OHOS {
40 namespace Notification {
41
42 class AdvancedNotificationServiceUnitTest : public testing::Test {
43 public:
44 static void SetUpTestCase();
45 static void TearDownTestCase();
46 void SetUp();
47 void TearDown();
48
49 private:
50 static sptr<AdvancedNotificationService> advancedNotificationService_;
51 };
52
53 sptr<AdvancedNotificationService> AdvancedNotificationServiceUnitTest::advancedNotificationService_ = nullptr;
54
SetUpTestCase()55 void AdvancedNotificationServiceUnitTest::SetUpTestCase() {}
56
TearDownTestCase()57 void AdvancedNotificationServiceUnitTest::TearDownTestCase() {}
58
SetUp()59 void AdvancedNotificationServiceUnitTest::SetUp()
60 {
61 GTEST_LOG_(INFO) << "SetUp start";
62
63 advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService();
64
65 GTEST_LOG_(INFO) << "SetUp end";
66 }
67
TearDown()68 void AdvancedNotificationServiceUnitTest::TearDown()
69 {
70 advancedNotificationService_ = nullptr;
71 GTEST_LOG_(INFO) << "TearDown";
72 }
73
74 /**
75 * @tc.name: PrepareNotificationRequest_100
76 * @tc.desc: Test PrepareNotificationRequest when GetClientBundleName returns an empty string.
77 * @tc.type: FUNC
78 * @tc.require: issue
79 */
80 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_100, Function | SmallTest | Level1)
81 {
82 MockIsNonBundleName(true);
83
84 auto ret = advancedNotificationService_->PrepareNotificationRequest(nullptr);
85
86 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
87 }
88
89 /**
90 * @tc.name: PrepareNotificationRequest_200
91 * @tc.desc: Test PrepareNotificationRequest when request is nullptr.
92 * @tc.type: FUNC
93 * @tc.require: issue
94 */
95 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_200, Function | SmallTest | Level1)
96 {
97 MockIsNonBundleName(false);
98
99 auto ret = advancedNotificationService_->PrepareNotificationRequest(nullptr);
100
101 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
102 }
103
104 /**
105 * @tc.name: PrepareNotificationRequest_300
106 * @tc.desc: Test PrepareNotificationRequest when notification is agent notification and the caller is not subsystem
107 * or system app.
108 * @tc.type: FUNC
109 * @tc.require: issue
110 */
111 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_300, Function | SmallTest | Level1)
112 {
113 MockIsNonBundleName(false);
114 sptr<NotificationRequest> req = new NotificationRequest();
115 req->SetIsAgentNotification(true);
116 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
117
118 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
119
120 ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
121 }
122
123 /**
124 * @tc.name: PrepareNotificationRequest_400
125 * @tc.desc: Test PrepareNotificationRequest when notification is agent notification and the caller is without
126 * permission. This test case must be executed in the context of an application to accurately reflect
127 * the scenario, and cannot be run as a standalone sub-system.
128 * @tc.type: FUNC
129 * @tc.require: issue
130 */
131 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_400, Function | SmallTest | Level1)
132 {
133 MockIsNonBundleName(false);
134 sptr<NotificationRequest> req = new NotificationRequest();
135 req->SetIsAgentNotification(true);
136 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
137 MockIsSystemApp(true);
138 MockIsVerfyPermisson(false);
139
140 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
141
142 ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
143 }
144
145 /**
146 * @tc.name: PrepareNotificationRequest_500
147 * @tc.desc: Test PrepareNotificationRequest when notification is agent notification and request's
148 * owner userId is not SUBSCRIBE_USER_INIT(-1).
149 * @tc.type: FUNC
150 * @tc.require: issue
151 */
152 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_500, Function | SmallTest | Level1)
153 {
154 MockIsNonBundleName(false);
155 sptr<NotificationRequest> req = new NotificationRequest();
156 req->SetIsAgentNotification(true);
157 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
158 MockIsSystemApp(true);
159 MockIsVerfyPermisson(true);
160 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
161 req->SetOwnerUserId(0);
162
163 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
164
165 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_UID);
166 }
167
168 /**
169 * @tc.name: PrepareNotificationRequest_600
170 * @tc.desc: Test PrepareNotificationRequest when notification is agent notification and request's
171 * owner userId is SUBSCRIBE_USER_INIT(-1) and owner uid is less than DEFAULT_UID(0).
172 * @tc.type: FUNC
173 * @tc.require: issue
174 */
175 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_600, Function | SmallTest | Level1)
176 {
177 MockIsNonBundleName(false);
178 sptr<NotificationRequest> req = new NotificationRequest();
179 req->SetIsAgentNotification(true);
180 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
181 MockIsSystemApp(true);
182 MockIsVerfyPermisson(true);
183 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
184 req->SetOwnerUserId(TEST_SUBSCRIBE_USER_INIT);
185 req->SetOwnerUid(-1);
186
187 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
188
189 ASSERT_EQ(ret, (int)ERR_ANS_GET_ACTIVE_USER_FAILED);
190 }
191
192 /**
193 * @tc.name: PrepareNotificationRequest_700
194 * @tc.desc: Test PrepareNotificationRequest when notification is agent notification and request's
195 * owner userId is SUBSCRIBE_USER_INIT(-1) and owner uid equals to DEFAULT_UID(0).
196 * @tc.type: FUNC
197 * @tc.require: issue
198 */
199 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_700, Function | SmallTest | Level1)
200 {
201 MockIsNonBundleName(false);
202 sptr<NotificationRequest> req = new NotificationRequest();
203 req->SetIsAgentNotification(true);
204 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
205 MockIsSystemApp(true);
206 MockIsVerfyPermisson(true);
207 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
208 req->SetOwnerUserId(TEST_SUBSCRIBE_USER_INIT);
209 req->SetOwnerUid(0); // DEFAULT_UID
210
211 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
212
213 ASSERT_EQ(ret, (int)ERR_OK);
214 }
215
216 /**
217 * @tc.name: PrepareNotificationRequest_800
218 * @tc.desc: Test PrepareNotificationRequest when notification is agent notification and request's
219 * owner userId is not SUBSCRIBE_USER_INIT(-1) and owner uid equals to SYSTEM_APP_UID(100).
220 * @tc.type: FUNC
221 * @tc.require: issue
222 */
223 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_800, Function | SmallTest | Level1)
224 {
225 MockIsNonBundleName(false);
226 sptr<NotificationRequest> req = new NotificationRequest();
227 req->SetIsAgentNotification(true);
228 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
229 MockIsSystemApp(true);
230 MockIsVerfyPermisson(true);
231 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
232 req->SetOwnerUserId(TEST_SUBSCRIBE_USER_INIT);
233 req->SetOwnerUid(SYSTEM_APP_UID); // SYSTEM_APP_UID
234
235 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
236
237 ASSERT_EQ(ret, (int)ERR_OK);
238 }
239
240 /**
241 * @tc.name: PrepareNotificationRequest_900
242 * @tc.desc: Test PrepareNotificationRequest when notification is not agent notification and uid in
243 * bundleOption of request is less than DEFAULT_UID(0).
244 * @tc.type: FUNC
245 * @tc.require: issue
246 */
247 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_900, Function | SmallTest | Level1)
248 {
249 MockIsNonBundleName(false);
250 sptr<NotificationRequest> req = new NotificationRequest();
251 req->SetIsAgentNotification(false);
252 std::shared_ptr<NotificationBundleOption> bundleOption =
253 std::make_shared<NotificationBundleOption>("bundle", -1);
254 req->SetBundleOption(bundleOption);
255 MockIsVerfyPermisson(true);
256 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
257
258 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
259
260 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_UID);
261 }
262
263 /**
264 * @tc.name: PrepareNotificationRequest_1000
265 * @tc.desc: Test PrepareNotificationRequest when notification is not agent notification and uid in
266 * bundleOption of request equals to DEFAULT_UID(0).
267 * @tc.type: FUNC
268 * @tc.require: issue
269 */
270 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_1000, Function | SmallTest | Level1)
271 {
272 MockIsNonBundleName(false);
273 sptr<NotificationRequest> req = new NotificationRequest();
274 req->SetIsAgentNotification(false);
275 std::shared_ptr<NotificationBundleOption> bundleOption =
276 std::make_shared<NotificationBundleOption>("bundle", 0);
277 req->SetBundleOption(bundleOption);
278 MockIsVerfyPermisson(true);
279 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
280 req->SetOwnerUserId(0);
281
282 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
283
284 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_UID);
285 }
286
287 /**
288 * @tc.name: PrepareNotificationRequest_1100
289 * @tc.desc: Test PrepareNotificationRequest when notification is not agent notification and uid in
290 * bundleOption of request equals to DEFAULT_UID(0).
291 * @tc.type: FUNC
292 * @tc.require: issue
293 */
294 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_1100, Function | SmallTest | Level1)
295 {
296 MockIsNonBundleName(false);
297 sptr<NotificationRequest> req = new NotificationRequest();
298 req->SetIsAgentNotification(false);
299 std::shared_ptr<NotificationBundleOption> bundleOption =
300 std::make_shared<NotificationBundleOption>("bundle", SYSTEM_APP_UID);
301 req->SetBundleOption(bundleOption);
302 MockIsVerfyPermisson(true);
303 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
304 req->SetOwnerUserId(SYSTEM_APP_UID);
305
306 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
307
308 ASSERT_EQ(ret, (int)ERR_OK);
309 }
310
311 /**
312 * @tc.name: PrepareNotificationRequest_1200
313 * @tc.desc: Test PrepareNotificationRequest when notification is not agent notification and bundle in
314 * bundleOption of request is empty and uid in bundleOption of request equals to DEFAULT_UID(0).
315 * @tc.type: FUNC
316 * @tc.require: issue
317 */
318 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationRequest_1200, Function | SmallTest | Level1)
319 {
320 MockIsNonBundleName(false);
321 sptr<NotificationRequest> req = new NotificationRequest();
322 req->SetIsAgentNotification(false);
323 std::shared_ptr<NotificationBundleOption> bundleOption =
324 std::make_shared<NotificationBundleOption>("", 0);
325 req->SetBundleOption(bundleOption);
326 MockIsVerfyPermisson(true);
327 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
328 req->SetOwnerUserId(0);
329
330 auto ret = advancedNotificationService_->PrepareNotificationRequest(req);
331
332 ASSERT_EQ(ret, (int)ERR_OK);
333 }
334
335 /**
336 * @tc.name: AssignToNotificationList_100
337 * @tc.desc: Test AssignToNotificationList.
338 * @tc.type: FUNC
339 */
340 HWTEST_F(AdvancedNotificationServiceUnitTest, AssignToNotificationList_100, Function | SmallTest | Level1)
341 {
342 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
343 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
344 request->SetNotificationId(1);
345 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
346 auto ret = advancedNotificationService_->AssignToNotificationList(record);
347
348 ASSERT_EQ(ret, (int)ERR_OK);
349 }
350
351 /**
352 * @tc.name: AssignToNotificationList_200
353 * @tc.desc: Test AssignToNotificationList when NotificationRequest's updateOnly is true but notification ID not exists.
354 * @tc.type: FUNC
355 */
356 HWTEST_F(AdvancedNotificationServiceUnitTest, AssignToNotificationList_200, Function | SmallTest | Level1)
357 {
358 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
359 request->SetUpdateOnly(true);
360 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
361 request->SetNotificationId(1);
362 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
363
364 auto ret = advancedNotificationService_->AssignToNotificationList(record);
365
366 ASSERT_EQ(ret, (int)ERR_ANS_NOTIFICATION_NOT_EXISTS);
367 }
368
369 /**
370 * @tc.name: AssignToNotificationList_300
371 * @tc.desc: Test AssignToNotificationList when NotificationRequest's updateOnly is true and notification ID exists.
372 * @tc.type: FUNC
373 */
374 HWTEST_F(AdvancedNotificationServiceUnitTest, AssignToNotificationList_300, Function | SmallTest | Level1)
375 {
376 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
377 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
378 request->SetNotificationId(1);
379 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
380 auto ret = advancedNotificationService_->AssignToNotificationList(record);
381 request->SetUpdateOnly(true);
382
383 ret = advancedNotificationService_->AssignToNotificationList(record);
384
385 ASSERT_EQ(ret, (int)ERR_OK);
386 }
387
388 /**
389 * @tc.name: AssignToNotificationList_400
390 * @tc.desc: Test AssignToNotificationList when notification ID exists and notification alerts once.
391 * @tc.type: FUNC
392 */
393 HWTEST_F(AdvancedNotificationServiceUnitTest, AssignToNotificationList_400, Function | SmallTest | Level1)
394 {
395 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
396 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
397 request->SetNotificationId(1);
398 request->SetAlertOneTime(true);
399 auto flags = std::make_shared<NotificationFlags>();
400 request->SetFlags(flags);
401 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
402 auto ret = advancedNotificationService_->AssignToNotificationList(record);
403
404 ret = advancedNotificationService_->AssignToNotificationList(record);
405 ASSERT_EQ(ret, (int)ERR_OK);
406 }
407
408 /**
409 * @tc.name: PrepareNotificationInfoTest_100
410 * @tc.desc: Test PrepareNotificationInfo when request is nullptr.
411 * @tc.type: FUNC
412 */
413 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationInfoTest_100, Function | SmallTest | Level1)
414 {
415 sptr<NotificationBundleOption> bundleOption = nullptr;
416 auto ret = advancedNotificationService_->PrepareNotificationInfo(nullptr, bundleOption);
417
418 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
419 }
420
421 /**
422 * @tc.name: PrepareNotificationInfoTest_200
423 * @tc.desc: Test PrepareNotificationInfo when caller is not system app
424 * @tc.type: FUNC
425 */
426 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationInfoTest_200, Function | SmallTest | Level1)
427 {
428 sptr<NotificationBundleOption> bundleOption = nullptr;
429 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
430 request->SetSlotType(NotificationConstant::SlotType::CUSTOM);
431 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
432 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
433 MockIsSystemApp(false);
434
435 auto ret = advancedNotificationService_->PrepareNotificationInfo(request, bundleOption);
436
437 ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
438 }
439
440 /**
441 * @tc.name: PrepareNotificationInfoTest_300
442 * @tc.desc: Test PrepareNotificationInfo when PrepareNotificationRequest failed to check permission.
443 * @tc.type: FUNC
444 */
445 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationInfoTest_300, Function | SmallTest | Level1)
446 {
447 sptr<NotificationBundleOption> bundleOption = nullptr;
448 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
449 request->SetIsAgentNotification(true);
450 MockIsNonBundleName(true);
451
452 auto ret = advancedNotificationService_->PrepareNotificationInfo(request, bundleOption);
453
454 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE);
455 }
456
457 /**
458 * @tc.name: PrepareNotificationInfoTest_400
459 * @tc.desc: Test PrepareNotificationInfo when PrepareNotificationRequest failed to check permission.
460 * @tc.type: FUNC
461 */
462 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationInfoTest_400, Function | SmallTest | Level1)
463 {
464 sptr<NotificationBundleOption> bundleOption = nullptr;
465 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
466 request->SetIsAgentNotification(true);
467 MockIsNonBundleName(false);
468 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
469 MockIsSystemApp(true);
470 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
471 request->SetOwnerUserId(TEST_SUBSCRIBE_USER_INIT);
472 request->SetOwnerUid(0); // DEFAULT_UID
473
474 auto ret = advancedNotificationService_->PrepareNotificationInfo(request, bundleOption);
475
476 ASSERT_EQ(ret, (int)ERR_OK);
477 }
478
479 /**
480 * @tc.name: PrepareNotificationInfoTest_500
481 * @tc.desc: Test PrepareNotificationInfo when PrepareNotificationRequest failed to check permission.
482 * @tc.type: FUNC
483 */
484 HWTEST_F(AdvancedNotificationServiceUnitTest, PrepareNotificationInfoTest_500, Function | SmallTest | Level1)
485 {
486 sptr<NotificationBundleOption> bundle = nullptr;
487 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
488 request->SetIsAgentNotification(false);
489 std::shared_ptr<NotificationBundleOption> bundleOption =
490 std::make_shared<NotificationBundleOption>("bundle", SYSTEM_APP_UID);
491 request->SetBundleOption(bundleOption);
492 MockIsVerfyPermisson(true);
493 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
494 request->SetOwnerUserId(SYSTEM_APP_UID);
495
496 auto ret = advancedNotificationService_->PrepareNotificationInfo(request, bundle);
497
498 ASSERT_EQ(ret, (int)ERR_OK);
499 }
500
501 /**
502 * @tc.name: StartFinishTimer_100
503 * @tc.desc: Test StartFinishTimer when Timer failed to create.
504 * @tc.type: FUNC
505 */
506 HWTEST_F(AdvancedNotificationServiceUnitTest, StartFinishTimer_100, Function | SmallTest | Level1)
507 {
508 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
509 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
510 request->SetNotificationId(1);
511 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
512 auto now = std::chrono::system_clock::now();
513 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
514 MockCreateTimerFailed(true);
515
516 auto ret = advancedNotificationService_->StartFinishTimer(record, duration.count(),
517 NotificationConstant::TRIGGER_EIGHT_HOUR_REASON_DELETE);
518
519 ASSERT_EQ(ret, (int)ERR_ANS_TASK_ERR);
520 MockCreateTimerFailed(false);
521 }
522
523 /**
524 * @tc.name: StartFinishTimer_200
525 * @tc.desc: Test StartFinishTimer when Timer succeeded to create.
526 * @tc.type: FUNC
527 */
528 HWTEST_F(AdvancedNotificationServiceUnitTest, StartFinishTimer_200, Function | SmallTest | Level1)
529 {
530 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
531 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
532 request->SetNotificationId(1);
533 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
534 auto now = std::chrono::system_clock::now();
535 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
536 MockCreateTimerFailed(false);
537
538 auto ret = advancedNotificationService_->StartFinishTimer(record, duration.count(),
539 NotificationConstant::TRIGGER_EIGHT_HOUR_REASON_DELETE);
540
541 ASSERT_EQ(ret, (int)ERR_OK);
542 }
543
544 /**
545 * @tc.name: StartUpdateTimer_100
546 * @tc.desc: Test StartUpdateTimer when Timer failed to create.
547 * @tc.type: FUNC
548 */
549 HWTEST_F(AdvancedNotificationServiceUnitTest, StartUpdateTimer_100, Function | SmallTest | Level1)
550 {
551 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
552 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
553 request->SetNotificationId(1);
554 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
555 auto now = std::chrono::system_clock::now();
556 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
557 MockCreateTimerFailed(true);
558
559 auto ret = advancedNotificationService_->StartUpdateTimer(record, duration.count(),
560 NotificationConstant::MAX_UPDATE_TIME);
561
562 ASSERT_EQ(ret, (int)ERR_ANS_TASK_ERR);
563 MockCreateTimerFailed(false);
564 }
565
566 /**
567 * @tc.name: StartUpdateTimer_200
568 * @tc.desc: Test StartUpdateTimer when Timer succeeded to create.
569 * @tc.type: FUNC
570 */
571 HWTEST_F(AdvancedNotificationServiceUnitTest, StartUpdateTimer_200, Function | SmallTest | Level1)
572 {
573 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
574 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
575 request->SetNotificationId(1);
576 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
577 auto now = std::chrono::system_clock::now();
578 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
579 MockCreateTimerFailed(false);
580
581 auto ret = advancedNotificationService_->StartUpdateTimer(record, duration.count(),
582 NotificationConstant::MAX_UPDATE_TIME);
583
584 ASSERT_EQ(ret, (int)ERR_OK);
585 }
586
587 /**
588 * @tc.name: SetUpdateTimer_100
589 * @tc.desc: Test SetUpdateTimer when Timer failed to create.
590 * @tc.type: FUNC
591 */
592 HWTEST_F(AdvancedNotificationServiceUnitTest, SetUpdateTimer_100, Function | SmallTest | Level1)
593 {
594 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
595 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
596 request->SetNotificationId(1);
597 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
598 MockCreateTimerFailed(true);
599
600 auto ret = advancedNotificationService_->SetUpdateTimer(record);
601
602 ASSERT_EQ(ret, (int)ERR_ANS_TASK_ERR);
603 MockCreateTimerFailed(false);
604 }
605
606 /**
607 * @tc.name: SetUpdateTimer_200
608 * @tc.desc: Test SetUpdateTimer when Timer succeeded to create.
609 * @tc.type: FUNC
610 */
611 HWTEST_F(AdvancedNotificationServiceUnitTest, SetUpdateTimer_200, Function | SmallTest | Level1)
612 {
613 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
614 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
615 request->SetNotificationId(1);
616 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
617 MockCreateTimerFailed(false);
618
619 auto ret = advancedNotificationService_->SetUpdateTimer(record);
620
621 ASSERT_EQ(ret, (int)ERR_OK);
622 }
623
624 /**
625 * @tc.name: StartArchiveTimer_100
626 * @tc.desc: Test StartArchiveTimer when trigger auto delete at once.
627 * @tc.type: FUNC
628 */
629 HWTEST_F(AdvancedNotificationServiceUnitTest, StartArchiveTimer_100, Function | SmallTest | Level1)
630 {
631 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
632 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
633 request->SetNotificationId(1);
634 request->SetAutoDeletedTime(0);
635 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
636 advancedNotificationService_->AddToNotificationList(record);
637
638 advancedNotificationService_->StartArchiveTimer(record);
639
640 ASSERT_FALSE(advancedNotificationService_->IsNotificationExists(record->notification->GetKey()));
641 }
642
643 /**
644 * @tc.name: StartArchiveTimer_200
645 * @tc.desc: Test StartArchiveTimer when timer failed to create.
646 * @tc.type: FUNC
647 */
648 HWTEST_F(AdvancedNotificationServiceUnitTest, StartArchiveTimer_200, Function | SmallTest | Level1)
649 {
650 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
651 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
652 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
653 MockCreateTimerFailed(true);
654
655 advancedNotificationService_->StartArchiveTimer(record);
656
657 ASSERT_EQ(record->notification->GetArchiveTimer(), NotificationConstant::INVALID_TIMER_ID);
658 MockCreateTimerFailed(false);
659 }
660
661 /**
662 * @tc.name: StartAutoDeletedTimer_100
663 * @tc.desc: Test StartAutoDeletedTimer when timer failed to create.
664 * @tc.type: FUNC
665 */
666 HWTEST_F(AdvancedNotificationServiceUnitTest, StartAutoDeletedTimer_100, Function | SmallTest | Level1)
667 {
668 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
669 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
670 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
671 MockCreateTimerFailed(true);
672
673 auto ret = advancedNotificationService_->StartAutoDeletedTimer(record);
674
675 ASSERT_EQ(ret, (int)ERR_ANS_TASK_ERR);
676 MockCreateTimerFailed(false);
677 }
678
679 /**
680 * @tc.name: StartAutoDeletedTimer_200
681 * @tc.desc: Test StartAutoDeletedTimer when timer succeeded to create.
682 * @tc.type: FUNC
683 */
684 HWTEST_F(AdvancedNotificationServiceUnitTest, StartAutoDeletedTimer_200, Function | SmallTest | Level1)
685 {
686 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
687 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
688 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
689
690 auto ret = advancedNotificationService_->StartAutoDeletedTimer(record);
691
692 ASSERT_EQ(ret, (int)ERR_OK);
693 }
694
695 /**
696 * @tc.name: StartAutoDeletedTimer_300
697 * @tc.desc: Test StartAutoDeletedTimer when timer succeeded to create and cancel origin timer.
698 * @tc.type: FUNC
699 */
700 HWTEST_F(AdvancedNotificationServiceUnitTest, StartAutoDeletedTimer_300, Function | SmallTest | Level1)
701 {
702 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
703 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
704 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
705 record->notification->SetAutoDeletedTimer(1);
706
707 auto ret = advancedNotificationService_->StartAutoDeletedTimer(record);
708 advancedNotificationService_->TriggerAutoDelete("hashCode", 27);
709
710 ASSERT_EQ(ret, (int)ERR_OK);
711 }
712
713 /**
714 * @tc.name: ReportDoNotDisturbModeChanged_100
715 * @tc.desc: Test ReportDoNotDisturbModeChanged.
716 * @tc.type: FUNC
717 */
718 HWTEST_F(AdvancedNotificationServiceUnitTest, ReportDoNotDisturbModeChanged_100, Function | SmallTest | Level1)
719 {
720 int32_t userId = 100;
721 std::string enable = "1";
722
723 advancedNotificationService_->ReportDoNotDisturbModeChanged(userId, enable);
724 enable = "0";
725 advancedNotificationService_->ReportDoNotDisturbModeChanged(userId, enable);
726
727 ASSERT_EQ(advancedNotificationService_->doNotDisturbEnableRecord_.size(), 1);
728 }
729
730 /**
731 * @tc.name: DoNotDisturbUpdataReminderFlags_100
732 * @tc.desc: Test DoNotDisturbUpdataReminderFlags when notificationFlags is nullptr.
733 * @tc.type: FUNC
734 */
735 HWTEST_F(AdvancedNotificationServiceUnitTest, DoNotDisturbUpdataReminderFlags_100, Function | SmallTest | Level1)
736 {
737 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
738 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
739 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
740
741 advancedNotificationService_->DoNotDisturbUpdataReminderFlags(record);
742
743 ASSERT_EQ(record->request->GetFlags(), nullptr);
744 }
745
746 /**
747 * @tc.name: ChangeNotificationByControlFlags_100
748 * @tc.desc: Test ChangeNotificationByControlFlags when notificationFlags is nullptr.
749 * @tc.type: FUNC
750 */
751 HWTEST_F(AdvancedNotificationServiceUnitTest, ChangeNotificationByControlFlags_100, Function | SmallTest | Level1)
752 {
753 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
754 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
755 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
756
757 advancedNotificationService_->ChangeNotificationByControlFlags(record, false);
758
759 ASSERT_EQ(record->request->GetFlags(), nullptr);
760 }
761
762 /**
763 * @tc.name: CheckPublishPreparedNotification_100
764 * @tc.desc: Test CheckPublishPreparedNotification when notificationSvrQueue_ is nullptr.
765 * @tc.type: FUNC
766 */
767 HWTEST_F(AdvancedNotificationServiceUnitTest, CheckPublishPreparedNotification_100, Function | SmallTest | Level1)
768 {
769 std::shared_ptr<NotificationRecord> record = nullptr;
770 advancedNotificationService_->notificationSvrQueue_ = nullptr;
771
772 auto ret = advancedNotificationService_->CheckPublishPreparedNotification(record, false);
773
774 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
775 }
776
777 /**
778 * @tc.name: CheckPublishPreparedNotification_200
779 * @tc.desc: Test CheckPublishPreparedNotification when record is nullptr.
780 * @tc.type: FUNC
781 */
782 HWTEST_F(AdvancedNotificationServiceUnitTest, CheckPublishPreparedNotification_200, Function | SmallTest | Level1)
783 {
784 std::shared_ptr<NotificationRecord> record = nullptr;
785
786 auto ret = advancedNotificationService_->CheckPublishPreparedNotification(record, false);
787
788 ASSERT_EQ(ret, (int)ERR_ANS_NO_MEMORY);
789 }
790
791 /**
792 * @tc.name: CheckPublishPreparedNotification_300
793 * @tc.desc: Test CheckPublishPreparedNotification when isSystemApp is false and slot type is
794 NotificationConstant::SlotType::EMERGENCY_INFORMATION.
795 * @tc.type: FUNC
796 */
797 HWTEST_F(AdvancedNotificationServiceUnitTest, CheckPublishPreparedNotification_300, Function | SmallTest | Level1)
798 {
799 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
800 request->SetSlotType(NotificationConstant::SlotType::EMERGENCY_INFORMATION);
801 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
802 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
803
804 auto ret = advancedNotificationService_->CheckPublishPreparedNotification(record, false);
805
806 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
807 }
808
809 /**
810 * @tc.name: ReplyDistributeOperation_100
811 * @tc.desc: Test ReplyDistributeOperation when caller is not system app.
812 * @tc.type: FUNC
813 */
814 HWTEST_F(AdvancedNotificationServiceUnitTest, ReplyDistributeOperation_100, Function | SmallTest | Level1)
815 {
816 std::string hashCode;
817 int32_t result = 0;
818 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
819 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
820 MockIsSystemApp(false);
821
822 auto ret = advancedNotificationService_->ReplyDistributeOperation(hashCode, result);
823
824 ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
825 }
826
827 /**
828 * @tc.name: ReplyDistributeOperation_200
829 * @tc.desc: Test ReplyDistributeOperation when caller has no permission.
830 * @tc.type: FUNC
831 */
832 HWTEST_F(AdvancedNotificationServiceUnitTest, ReplyDistributeOperation_200, Function | SmallTest | Level1)
833 {
834 std::string hashCode;
835 int32_t result = 0;
836 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
837 MockIsSystemApp(true);
838 MockIsVerfyPermisson(false);
839
840 auto ret = advancedNotificationService_->ReplyDistributeOperation(hashCode, result);
841
842 ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
843 }
844
845 /**
846 * @tc.name: ReplyDistributeOperation_300
847 * @tc.desc: Test ReplyDistributeOperation when hashCode is empty.
848 * @tc.type: FUNC
849 */
850 HWTEST_F(AdvancedNotificationServiceUnitTest, ReplyDistributeOperation_300, Function | SmallTest | Level1)
851 {
852 std::string hashCode;
853 int32_t result = 0;
854 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
855 MockIsSystemApp(true);
856 MockIsVerfyPermisson(true);
857 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
858
859 auto ret = advancedNotificationService_->ReplyDistributeOperation(hashCode, result);
860
861 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
862 }
863
864 /**
865 * @tc.name: ReplyDistributeOperation_400
866 * @tc.desc: Test ReplyDistributeOperation when hashCode is not empty.
867 * @tc.type: FUNC
868 */
869 HWTEST_F(AdvancedNotificationServiceUnitTest, ReplyDistributeOperation_400, Function | SmallTest | Level1)
870 {
871 std::string hashCode = "hashCode";
872 int32_t result = 0;
873 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
874 MockIsSystemApp(true);
875 MockIsVerfyPermisson(true);
876 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
877
878 auto ret = advancedNotificationService_->ReplyDistributeOperation(hashCode, result);
879
880 ASSERT_EQ(ret, (int)ERR_OK);
881 }
882
883 /**
884 * @tc.name: GetNotificationRequestByHashCode_100
885 * @tc.desc: Test GetNotificationRequestByHashCode when caller is not system app.
886 * @tc.type: FUNC
887 */
888 HWTEST_F(AdvancedNotificationServiceUnitTest, GetNotificationRequestByHashCode_100, Function | SmallTest | Level1)
889 {
890 std::string hashCode = "hashCode";
891 sptr<NotificationRequest> request= nullptr;
892 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
893 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
894 MockIsSystemApp(false);
895
896 auto ret = advancedNotificationService_->GetNotificationRequestByHashCode(hashCode, request);
897
898 ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
899 }
900
901 /**
902 * @tc.name: GetNotificationRequestByHashCode_200
903 * @tc.desc: Test GetNotificationRequestByHashCode when caller has no permission.
904 * @tc.type: FUNC
905 */
906 HWTEST_F(AdvancedNotificationServiceUnitTest, GetNotificationRequestByHashCode_200, Function | SmallTest | Level1)
907 {
908 std::string hashCode = "hashCode";
909 sptr<NotificationRequest> request= nullptr;
910 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
911 MockIsSystemApp(true);
912 MockIsVerfyPermisson(false);
913
914 auto ret = advancedNotificationService_->GetNotificationRequestByHashCode(hashCode, request);
915
916 ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
917 }
918
919 /**
920 * @tc.name: GetNotificationRequestByHashCode_300
921 * @tc.desc: Test GetNotificationRequestByHashCode when notificationSvrQueue_ is nullptr.
922 * @tc.type: FUNC
923 */
924 HWTEST_F(AdvancedNotificationServiceUnitTest, GetNotificationRequestByHashCode_300, Function | SmallTest | Level1)
925 {
926 std::string hashCode = "hashCode";
927 sptr<NotificationRequest> request= nullptr;
928 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
929 MockIsSystemApp(true);
930 MockIsVerfyPermisson(true);
931 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
932 advancedNotificationService_->notificationSvrQueue_ = nullptr;
933
934 auto ret = advancedNotificationService_->GetNotificationRequestByHashCode(hashCode, request);
935
936 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
937 }
938
939 /**
940 * @tc.name: GetNotificationRequestByHashCode_400
941 * @tc.desc: Test GetNotificationRequestByHashCode.
942 * @tc.type: FUNC
943 */
944 HWTEST_F(AdvancedNotificationServiceUnitTest, GetNotificationRequestByHashCode_400, Function | SmallTest | Level1)
945 {
946 std::string hashCode = "hashCode";
947 sptr<NotificationRequest> request= nullptr;
948 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
949 MockIsSystemApp(true);
950 MockIsVerfyPermisson(true);
951 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
952
953 auto ret = advancedNotificationService_->GetNotificationRequestByHashCode(hashCode, request);
954
955 ASSERT_EQ(ret, (int)ERR_OK);
956 }
957
958 /**
959 * @tc.name: QueryDoNotDisturbProfile_100
960 * @tc.desc: Test QueryDoNotDisturbProfile when dataShareHelper failed to be created.
961 * @tc.type: FUNC
962 */
963 HWTEST_F(AdvancedNotificationServiceUnitTest, QueryDoNotDisturbProfile_100, Function | SmallTest | Level1)
964 {
965 int32_t userId = 100;
966 std::string enable;
967 std::string profileId;
968 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(true);
969 MockIsFailedToCreateDataShareHelper(true);
970 MockGetStringValue("1");
971
972 advancedNotificationService_->QueryDoNotDisturbProfile(userId, enable, profileId);
973
974 ASSERT_EQ(enable, "");
975 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(false);
976 }
977
978 /**
979 * @tc.name: QueryDoNotDisturbProfile_200
980 * @tc.desc: Test QueryDoNotDisturbProfile when dataShareHelper failed to Query.
981 * @tc.type: FUNC
982 */
983 HWTEST_F(AdvancedNotificationServiceUnitTest, QueryDoNotDisturbProfile_200, Function | SmallTest | Level1)
984 {
985 int32_t userId = 100;
986 std::string enable = "";
987 std::string profileId;
988 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(true);
989 MockIsFailedToCreateDataShareHelper(false);
990 MockIsFailedToQueryDataShareResultSet(false);
991 MockGetStringValue("");
992 MockIsFailedGoToFirstRow(0);
993
994 advancedNotificationService_->QueryDoNotDisturbProfile(userId, enable, profileId);
995
996 ASSERT_EQ(enable, "");
997 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(false);
998 }
999
1000 /**
1001 * @tc.name: QueryDoNotDisturbProfile_300
1002 * @tc.desc: Test QueryDoNotDisturbProfile when dataShareHelper succeeded to Query.
1003 * @tc.type: FUNC
1004 */
1005 HWTEST_F(AdvancedNotificationServiceUnitTest, QueryDoNotDisturbProfile_300, Function | SmallTest | Level1)
1006 {
1007 int32_t userId = 100;
1008 std::string enable;
1009 std::string profileId;
1010 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(true);
1011 MockIsFailedToCreateDataShareHelper(false);
1012 MockIsFailedToQueryDataShareResultSet(false);
1013 MockIsFailedGoToFirstRow(0);
1014 MockGetStringValue("1");
1015
1016 advancedNotificationService_->QueryDoNotDisturbProfile(userId, enable, profileId);
1017
1018 ASSERT_EQ(enable, "1");
1019 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(false);
1020 }
1021
1022 /**
1023 * @tc.name: QueryIntelligentExperienceEnable_100
1024 * @tc.desc: Test QueryIntelligentExperienceEnable when dataShareHelper failed to be created.
1025 * @tc.type: FUNC
1026 */
1027 HWTEST_F(AdvancedNotificationServiceUnitTest, QueryIntelligentExperienceEnable_100, Function | SmallTest | Level1)
1028 {
1029 int32_t userId = 100;
1030 std::string enable = "";
1031 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(true);
1032 MockIsFailedToCreateDataShareHelper(true);
1033 MockGetStringValue("1");
1034
1035 advancedNotificationService_->QueryIntelligentExperienceEnable(userId, enable);
1036
1037 ASSERT_EQ(enable, "");
1038 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(false);
1039 }
1040
1041 /**
1042 * @tc.name: QueryIntelligentExperienceEnable_200
1043 * @tc.desc: Test QueryIntelligentExperienceEnable when dataShareHelper succeeded to query.
1044 * @tc.type: FUNC
1045 */
1046 HWTEST_F(AdvancedNotificationServiceUnitTest, QueryIntelligentExperienceEnable_200, Function | SmallTest | Level1)
1047 {
1048 int32_t userId = 100;
1049 std::string enable = "";
1050 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(true);
1051 MockIsFailedToCreateDataShareHelper(false);
1052 MockIsFailedToQueryDataShareResultSet(false);
1053 MockIsFailedGoToFirstRow(0);
1054 MockGetStringValue("1"); // INTELLIGENT_EXPERIENCE
1055
1056 advancedNotificationService_->QueryIntelligentExperienceEnable(userId, enable);
1057
1058 ASSERT_EQ(enable, "1");
1059 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(false);
1060 }
1061
1062 /**
1063 * @tc.name: CheckDoNotDisturbProfile_100
1064 * @tc.desc: test CheckDoNotDisturbProfile when under the DoNotDisturbMode and classification is ANS_VERIFICATION_CODE.
1065 * @tc.type: FUNC
1066 */
1067 HWTEST_F(AdvancedNotificationServiceUnitTest, CheckDoNotDisturbProfile_100, Function | SmallTest | Level1)
1068 {
1069 auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1070 auto request = new (std::nothrow) NotificationRequest();
1071 request->SetNotificationControlFlags(0);
1072 request->SetReceiverUserId(0); // FIRST_USERID
1073 request->SetClassification("ANS_VERIFICATION_CODE"); // ANS_VERIFICATION_CODE
1074 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1075 DelayedSingleton<AdvancedDatashareHelper>::GetInstance()->SetIsDataShareReady(true);
1076 MockIsFailedToCreateDataShareHelper(false);
1077 MockIsFailedToQueryDataShareResultSet(false);
1078 MockIsFailedGoToFirstRow(0);
1079 MockGetStringValue("1");
1080
1081 advancedNotificationService_->CheckDoNotDisturbProfile(record);
1082 int32_t expect = (1 << 31) | (1 << 14); // CONTROL_BY_INTELLIGENT_EXPERIENCE | CONTROL_BY_DO_NOT_DISTURB_MODE
1083 ASSERT_EQ(record->request->GetNotificationControlFlags(), expect);
1084 }
1085
1086 /**
1087 * @tc.name: GetNotificationKeys_100
1088 * @tc.desc: test GetNotificationKeys when record exists in delayNotificationList_.
1089 * @tc.type: FUNC
1090 */
1091 HWTEST_F(AdvancedNotificationServiceUnitTest, GetNotificationKeys_100, Function | SmallTest | Level1)
1092 {
1093 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1094 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1095 request->SetOwnerUid(SYSTEM_APP_UID);
1096 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1097 std::vector<std::string> expect;
1098 expect.push_back(record->notification->GetKey());
1099
1100 advancedNotificationService_->AddToDelayNotificationList(record);
1101 auto res = advancedNotificationService_->GetNotificationKeys(bundle);
1102
1103 ASSERT_EQ(res, expect);
1104 }
1105
1106 /**
1107 * @tc.name: GetNotificationKeysByBundle_100
1108 * @tc.desc: test GetNotificationKeysByBundle when bundleOption is nullptr and record exists in delayNotificationList_
1109 * and notificationList_.
1110 * @tc.type: FUNC
1111 */
1112 HWTEST_F(AdvancedNotificationServiceUnitTest, GetNotificationKeysByBundle_100, Function | SmallTest | Level1)
1113 {
1114 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1115 sptr<NotificationRequest> request1 = new (std::nothrow) NotificationRequest();
1116 request1->SetNotificationId(1);
1117 auto record1 = advancedNotificationService_->MakeNotificationRecord(request1, bundle);
1118
1119 std::vector<std::string> expect;
1120
1121 advancedNotificationService_->AddToNotificationList(record1);
1122 auto res = advancedNotificationService_->GetNotificationKeysByBundle(nullptr);
1123
1124 ASSERT_EQ(res, expect);
1125 }
1126
1127 /**
1128 * @tc.name: GetNotificationKeysByBundle_200
1129 * @tc.desc: test GetNotificationKeysByBundle when bundleOption is not nullptr and record exists in
1130 * delayNotificationList_ and notificationList_.
1131 * @tc.type: FUNC
1132 */
1133 HWTEST_F(AdvancedNotificationServiceUnitTest, GetNotificationKeysByBundle_200, Function | SmallTest | Level1)
1134 {
1135 sptr<NotificationBundleOption> bundle1 = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1136 sptr<NotificationRequest> request1 = new (std::nothrow) NotificationRequest();
1137 request1->SetNotificationId(1);
1138 auto record1 = advancedNotificationService_->MakeNotificationRecord(request1, bundle1);
1139
1140 sptr<NotificationBundleOption> bundle2 = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1141 sptr<NotificationRequest> request2 = new (std::nothrow) NotificationRequest();
1142 request2->SetNotificationId(2);
1143 auto record2 = advancedNotificationService_->MakeNotificationRecord(request2, bundle2);
1144
1145 sptr<NotificationBundleOption> bundle3 = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1146 sptr<NotificationRequest> request3 = new (std::nothrow) NotificationRequest();
1147 request3->SetOwnerUid(NON_SYSTEM_APP_UID);
1148 request2->SetNotificationId(3);
1149 auto record3 = advancedNotificationService_->MakeNotificationRecord(request3, bundle3);
1150
1151 std::vector<std::string> expect;
1152 expect.push_back(record2->notification->GetKey());
1153 expect.push_back(record3->notification->GetKey());
1154
1155 advancedNotificationService_->AddToNotificationList(record1);
1156 advancedNotificationService_->AddToNotificationList(record2);
1157 advancedNotificationService_->AddToDelayNotificationList(record3);
1158 auto res = advancedNotificationService_->GetNotificationKeysByBundle(bundle2);
1159
1160 ASSERT_EQ(res, expect);
1161 }
1162
1163 /**
1164 * @tc.name: RemoveFromNotificationList_100
1165 * @tc.desc: test RemoveFromNotificationList when notification in notificationList_ is unremovable.
1166 * @tc.type: FUNC
1167 */
1168 HWTEST_F(AdvancedNotificationServiceUnitTest, RemoveFromNotificationList_100, Function | SmallTest | Level1)
1169 {
1170 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1171 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1172 request->SetNotificationId(1);
1173 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1174 record->notification->SetRemoveAllowed(false);
1175 advancedNotificationService_->AddToNotificationList(record);
1176 auto bundleOption = record->bundleOption;
1177 NotificationKey notificationKey = {.id = request->GetNotificationId(), .label = request->GetLabel()};
1178
1179 auto res = advancedNotificationService_->RemoveFromNotificationList(bundleOption, notificationKey,
1180 record->notification, 8, false);
1181
1182 ASSERT_EQ(res, (int)ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED);
1183 }
1184
1185 /**
1186 * @tc.name: RemoveFromNotificationList_200
1187 * @tc.desc: test RemoveFromNotificationList when notification in notificationList_ is local liveview.
1188 * @tc.type: FUNC
1189 */
1190 HWTEST_F(AdvancedNotificationServiceUnitTest, RemoveFromNotificationList_200, Function | SmallTest | Level1)
1191 {
1192 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1193 request->SetNotificationId(1);
1194 request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1195 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
1196 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
1197 request->SetContent(content);
1198 int creatorUid = 1;
1199 request->SetCreatorUid(creatorUid);
1200 int ownerUid = 2;
1201 request->SetOwnerUid(ownerUid);
1202 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", creatorUid);
1203 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1204 advancedNotificationService_->AddToNotificationList(record);
1205 auto bundleOption = record->bundleOption;
1206 NotificationKey notificationKey = {.id = request->GetNotificationId(), .label = request->GetLabel()};
1207
1208
1209 auto res = advancedNotificationService_->RemoveFromNotificationList(bundleOption, notificationKey,
1210 record->notification, 8, false);
1211
1212 ASSERT_EQ(res, (int)ERR_OK);
1213 }
1214
1215 /**
1216 * @tc.name: RemoveFromNotificationList_300
1217 * @tc.desc: test RemoveFromNotificationList when notification in delayNotificationList_.
1218 * @tc.type: FUNC
1219 */
1220 HWTEST_F(AdvancedNotificationServiceUnitTest, RemoveFromNotificationList_300, Function | SmallTest | Level1)
1221 {
1222 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1223 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1224 request->SetNotificationId(1);
1225 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1226 advancedNotificationService_->AddToDelayNotificationList(record);
1227 auto bundleOption = record->bundleOption;
1228 NotificationKey notificationKey = {.id = request->GetNotificationId(), .label = request->GetLabel()};
1229
1230 auto res = advancedNotificationService_->RemoveFromNotificationList(bundleOption, notificationKey,
1231 record->notification, 8, false);
1232
1233 ASSERT_EQ(res, (int)ERR_OK);
1234 }
1235
1236 /**
1237 * @tc.name: RemoveFromNotificationList_400
1238 * @tc.desc: test RemoveFromNotificationList when notification in notificationList_ is unremovable.
1239 * @tc.type: FUNC
1240 */
1241 HWTEST_F(AdvancedNotificationServiceUnitTest, RemoveFromNotificationList_400, Function | SmallTest | Level1)
1242 {
1243 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1244 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1245 request->SetNotificationId(1);
1246 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1247 record->notification->SetRemoveAllowed(false);
1248 advancedNotificationService_->AddToNotificationList(record);
1249 std::string key = record->notification->GetKey();
1250
1251 auto res = advancedNotificationService_->RemoveFromNotificationList(key, record->notification, false, 8);
1252
1253 ASSERT_EQ(res, (int)ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED);
1254 }
1255
1256 /**
1257 * @tc.name: RemoveFromNotificationListForDeleteAll_100
1258 * @tc.desc: test RemoveFromNotificationListForDeleteAll when notification in notificationList_ is unremovable.
1259 * @tc.type: FUNC
1260 */
1261 HWTEST_F(AdvancedNotificationServiceUnitTest, RemoveFromNotificationListForDeleteAll_100, Function | SmallTest | Level1)
1262 {
1263 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1264 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1265 request->SetNotificationId(1);
1266 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1267 record->notification->SetRemoveAllowed(false);
1268 advancedNotificationService_->AddToNotificationList(record);
1269 std::string key = record->notification->GetKey();
1270 int32_t userId = record->notification->GetUserId();
1271
1272 auto res = advancedNotificationService_->RemoveFromNotificationListForDeleteAll(key, userId, record->notification);
1273
1274 ASSERT_EQ(res, (int)ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED);
1275 }
1276
1277 /**
1278 * @tc.name: RemoveFromNotificationListForDeleteAll_200
1279 * @tc.desc: test RemoveFromNotificationListForDeleteAll when notification in notificationList_ is unremovable.
1280 * @tc.type: FUNC
1281 */
1282 HWTEST_F(AdvancedNotificationServiceUnitTest, RemoveFromNotificationListForDeleteAll_200, Function | SmallTest | Level1)
1283 {
1284 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1285 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1286 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1287 record->notification->SetRemoveAllowed(true);
1288 record->request->SetUnremovable(true);
1289 advancedNotificationService_->AddToNotificationList(record);
1290 std::string key = record->notification->GetKey();
1291 int32_t userId = record->notification->GetUserId();
1292
1293 auto res = advancedNotificationService_->RemoveFromNotificationListForDeleteAll(key, userId, record->notification);
1294
1295 ASSERT_EQ(res, (int)ERR_ANS_NOTIFICATION_IS_UNREMOVABLE);
1296 }
1297
1298 /**
1299 * @tc.name: RemoveFromDelayedNotificationList_100
1300 * @tc.desc: test RemoveFromDelayedNotificationList when notification in delayNotificationList_.
1301 * @tc.type: FUNC
1302 */
1303 HWTEST_F(AdvancedNotificationServiceUnitTest, RemoveFromDelayedNotificationList_100, Function | SmallTest | Level1)
1304 {
1305 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1306 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1307 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1308 advancedNotificationService_->AddToDelayNotificationList(record);
1309 std::string key = record->notification->GetKey();
1310
1311 auto res1 = advancedNotificationService_->RemoveFromDelayedNotificationList(key);
1312 auto res2 = advancedNotificationService_->RemoveFromDelayedNotificationList(key + "1");
1313
1314 ASSERT_TRUE(res1);
1315 ASSERT_FALSE(res2);
1316 }
1317
1318 /**
1319 * @tc.name: GetFromDelayedNotificationList_100
1320 * @tc.desc: test GetFromDelayedNotificationList when notification in delayNotificationList_.
1321 * @tc.type: FUNC
1322 */
1323 HWTEST_F(AdvancedNotificationServiceUnitTest, GetFromDelayedNotificationList_100, Function | SmallTest | Level1)
1324 {
1325 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1326 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1327 request->SetNotificationId(1);
1328 request->SetOwnerUid(SYSTEM_APP_UID);
1329 request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1330 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
1331 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
1332 request->SetContent(content);
1333 request->SetUpdateByOwnerAllowed(true);
1334 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1335 advancedNotificationService_->AddToDelayNotificationList(record);
1336
1337 auto res1 = advancedNotificationService_->GetFromDelayedNotificationList(SYSTEM_APP_UID, 1);
1338 auto res2 = advancedNotificationService_->GetFromDelayedNotificationList(SYSTEM_APP_UID, 2);
1339
1340 ASSERT_EQ(res1, record);
1341 ASSERT_EQ(res2, nullptr);
1342 }
1343
1344 /**
1345 * @tc.name: GetAllNotificationsBySlotType_100
1346 * @tc.desc: test GetAllNotificationsBySlotType when caller is not subsystem and system app.
1347 * @tc.type: FUNC
1348 */
1349 HWTEST_F(AdvancedNotificationServiceUnitTest, GetAllNotificationsBySlotType_100, Function | SmallTest | Level1)
1350 {
1351 std::vector<sptr<Notification>> notifications;
1352 NotificationConstant::SlotType slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
1353 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
1354 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1355 MockIsSystemApp(false);
1356
1357 auto res = advancedNotificationService_->GetAllNotificationsBySlotType(notifications, slotType);
1358
1359 ASSERT_EQ(res, (int)ERR_ANS_NON_SYSTEM_APP);
1360 }
1361
1362 /**
1363 * @tc.name: GetAllNotificationsBySlotType_200
1364 * @tc.desc: test GetAllNotificationsBySlotType when caller has no permission.
1365 * @tc.type: FUNC
1366 */
1367 HWTEST_F(AdvancedNotificationServiceUnitTest, GetAllNotificationsBySlotType_200, Function | SmallTest | Level1)
1368 {
1369 std::vector<sptr<Notification>> notifications;
1370 NotificationConstant::SlotType slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
1371 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
1372 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1373 MockIsSystemApp(true);
1374 MockIsVerfyPermisson(false);
1375
1376 auto res = advancedNotificationService_->GetAllNotificationsBySlotType(notifications, slotType);
1377
1378 ASSERT_EQ(res, (int)ERR_ANS_PERMISSION_DENIED);
1379 }
1380
1381 /**
1382 * @tc.name: GetAllNotificationsBySlotType_300
1383 * @tc.desc: test GetAllNotificationsBySlotType when notificationSvrQueue_ is nullptr.
1384 * @tc.type: FUNC
1385 */
1386 HWTEST_F(AdvancedNotificationServiceUnitTest, GetAllNotificationsBySlotType_300, Function | SmallTest | Level1)
1387 {
1388 std::vector<sptr<Notification>> notifications;
1389 NotificationConstant::SlotType slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
1390 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
1391 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1392 MockIsSystemApp(true);
1393 MockIsVerfyPermisson(true);
1394 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
1395 advancedNotificationService_->notificationSvrQueue_ = nullptr;
1396
1397 auto res = advancedNotificationService_->GetAllNotificationsBySlotType(notifications, slotType);
1398
1399 ASSERT_EQ(res, (int)ERR_ANS_INVALID_PARAM);
1400 }
1401
1402 /**
1403 * @tc.name: GetAllNotificationsBySlotType_400
1404 * @tc.desc: test GetAllNotificationsBySlotType.
1405 * @tc.type: FUNC
1406 */
1407 HWTEST_F(AdvancedNotificationServiceUnitTest, GetAllNotificationsBySlotType_400, Function | SmallTest | Level1)
1408 {
1409 std::vector<sptr<Notification>> notifications;
1410 NotificationConstant::SlotType slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
1411 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
1412 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1413 MockIsSystemApp(true);
1414 MockIsVerfyPermisson(true);
1415 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
1416 sptr<NotificationBundleOption> bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1417 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1418 request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1419 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1420 advancedNotificationService_->AddToNotificationList(record);
1421
1422 auto res = advancedNotificationService_->GetAllNotificationsBySlotType(notifications, slotType);
1423
1424 ASSERT_EQ(res, (int)ERR_OK);
1425 ASSERT_EQ(notifications.size(), 1);
1426 }
1427
1428 /**
1429 * @tc.name: IsSystemUser_100
1430 * @tc.desc: test IsSystemUser when userId belongs to system user.
1431 * @tc.type: FUNC
1432 */
1433 HWTEST_F(AdvancedNotificationServiceUnitTest, IsSystemUser_100, Function | SmallTest | Level1)
1434 {
1435 int32_t userId = 1;
1436
1437 auto res = advancedNotificationService_->IsSystemUser(userId);
1438
1439 ASSERT_TRUE(res);
1440 }
1441
1442 /**
1443 * @tc.name: PublishInNotificationList_100
1444 * @tc.desc: test PublishInNotificationList when notifications in notificationList_ exceed MAX_ACTIVE_NUM(1000).
1445 * @tc.type: FUNC
1446 */
1447 HWTEST_F(AdvancedNotificationServiceUnitTest, PublishInNotificationList_100, Function | SmallTest | Level1)
1448 {
1449 sptr<NotificationBundleOption> bundle = nullptr;
1450 sptr<NotificationRequest> request = nullptr;
1451 for (auto i = 0; i < MAX_ACTIVE_NUM; ++i) {
1452 bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE + std::to_string(i), SYSTEM_APP_UID + i);
1453 request = new (std::nothrow) NotificationRequest();
1454 request->SetNotificationId(i);
1455 request->SetOwnerBundleName(TEST_DEFUALT_BUNDLE + std::to_string(i));
1456 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1457 advancedNotificationService_->AddToNotificationList(record);
1458 }
1459 bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE + "10", SYSTEM_APP_UID + 10);
1460 request = new (std::nothrow) NotificationRequest();
1461 request->SetNotificationId(MAX_ACTIVE_NUM + 1);
1462 request->SetOwnerBundleName(TEST_DEFUALT_BUNDLE + "10");
1463 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1464
1465 auto res = advancedNotificationService_->PublishInNotificationList(record);
1466
1467 request = new (std::nothrow) NotificationRequest();
1468 request->SetNotificationId(10);
1469 ASSERT_FALSE(advancedNotificationService_->IsNotificationExists(request->GetKey()));
1470 }
1471
1472 /**
1473 * @tc.name: PublishInNotificationList_200
1474 * @tc.desc: test PublishInNotificationList when notifications in notificationList_ exceed MAX_ACTIVE_NUM(1000).
1475 * @tc.type: FUNC
1476 */
1477 HWTEST_F(AdvancedNotificationServiceUnitTest, PublishInNotificationList_200, Function | SmallTest | Level1)
1478 {
1479 sptr<NotificationBundleOption> bundle = nullptr;
1480 sptr<NotificationRequest> request = nullptr;
1481 for (auto i = 0; i < MAX_ACTIVE_NUM; ++i) {
1482 bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE + std::to_string(i), SYSTEM_APP_UID + i);
1483 request = new (std::nothrow) NotificationRequest();
1484 request->SetNotificationId(i);
1485 request->SetOwnerBundleName(TEST_DEFUALT_BUNDLE + std::to_string(i));
1486 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1487 advancedNotificationService_->AddToNotificationList(record);
1488 }
1489 bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE + std::to_string(MAX_ACTIVE_NUM),
1490 SYSTEM_APP_UID + MAX_ACTIVE_NUM);
1491 request = new (std::nothrow) NotificationRequest();
1492 request->SetNotificationId(MAX_ACTIVE_NUM + 1);
1493 request->SetOwnerBundleName(TEST_DEFUALT_BUNDLE + std::to_string(MAX_ACTIVE_NUM));
1494 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
1495
1496 auto res = advancedNotificationService_->PublishInNotificationList(record);
1497
1498 request = new (std::nothrow) NotificationRequest();
1499 request->SetNotificationId(0);
1500 ASSERT_FALSE(advancedNotificationService_->IsNotificationExists(request->GetKey()));
1501 }
1502
1503 /**
1504 * @tc.name: RegisterPushCallback_100
1505 * @tc.desc: test RegisterPushCallback when notifications in notificationList_ exceed MAX_ACTIVE_NUM(1000).
1506 * @tc.type: FUNC
1507 */
1508 HWTEST_F(AdvancedNotificationServiceUnitTest, RegisterPushCallback_100, Function | SmallTest | Level1)
1509 {
1510 sptr<IRemoteObject> pushCallback;
1511 sptr<NotificationCheckRequest> notificationCheckRequest;
1512 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1513 MockIsSystemApp(false);
1514 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
1515
1516 auto res = advancedNotificationService_->RegisterPushCallback(pushCallback, notificationCheckRequest);
1517
1518 ASSERT_EQ(res, (int)ERR_ANS_NON_SYSTEM_APP);
1519 }
1520
1521 /**
1522 * @tc.name: FillExtraInfoToJson_100
1523 * @tc.desc: test FillExtraInfoToJson when extraInfo is nullptr.
1524 * @tc.type: FUNC
1525 */
1526 HWTEST_F(AdvancedNotificationServiceUnitTest, FillExtraInfoToJson_100, Function | SmallTest | Level1)
1527 {
1528 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest(1);
1529 auto liveViewContent = std::make_shared<NotificationLiveViewContent>();
1530 liveViewContent->SetExtraInfo(nullptr);
1531 auto content = std::make_shared<NotificationContent>(liveViewContent);
1532 request->SetContent(content);
1533 sptr<NotificationCheckRequest> checkRequest = new (std::nothrow) NotificationCheckRequest(
1534 NotificationContent::Type::BASIC_TEXT,
1535 NotificationConstant::SlotType::SOCIAL_COMMUNICATION,
1536 {});
1537 nlohmann::json jsonObject;
1538
1539 advancedNotificationService_->FillExtraInfoToJson(request, checkRequest, jsonObject);
1540
1541 ASSERT_FALSE(jsonObject.contains("extraInfo"));
1542 }
1543
1544 /**
1545 * @tc.name: FillExtraInfoToJson_200
1546 * @tc.desc: test FillExtraInfoToJson when extraInfo is not nullptr.
1547 * @tc.type: FUNC
1548 */
1549 HWTEST_F(AdvancedNotificationServiceUnitTest, FillExtraInfoToJson_200, Function | SmallTest | Level1)
1550 {
1551 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest(1);
1552 auto liveViewContent = std::make_shared<NotificationLiveViewContent>();
1553 auto content = std::make_shared<NotificationContent>(liveViewContent);
1554 auto extraInfo = std::make_shared<AAFwk::WantParams>();
1555 extraInfo->SetParam("key1", nullptr);
1556 liveViewContent->SetExtraInfo(extraInfo);
1557 request->SetContent(content);
1558 sptr<NotificationCheckRequest> checkRequest = new (std::nothrow) NotificationCheckRequest(
1559 NotificationContent::Type::BASIC_TEXT,
1560 NotificationConstant::SlotType::SOCIAL_COMMUNICATION,
1561 {"key1", "key2"});
1562 nlohmann::json jsonObject;
1563
1564 advancedNotificationService_->FillExtraInfoToJson(request, checkRequest, jsonObject);
1565
1566 ASSERT_TRUE(jsonObject.contains("extraInfo"));
1567 }
1568
1569 /**
1570 * @tc.name: CreatePushCheckJson_100
1571 * @tc.desc: test CreatePushCheckJson when notification is an agent notification.
1572 * @tc.type: FUNC
1573 */
1574 HWTEST_F(AdvancedNotificationServiceUnitTest, CreatePushCheckJson_100, Function | SmallTest | Level1)
1575 {
1576 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest(1);
1577 std::string bundle = "bundle";
1578 request->SetIsAgentNotification(true);
1579 request->SetOwnerBundleName(bundle);
1580 sptr<NotificationCheckRequest> checkRequest = nullptr;
1581 nlohmann::json jsonObject;
1582
1583 advancedNotificationService_->CreatePushCheckJson(request, checkRequest, jsonObject);
1584
1585 ASSERT_EQ(jsonObject["pkgName"], bundle);
1586 }
1587
1588 /**
1589 * @tc.name: CreatePushCheckJson_200
1590 * @tc.desc: test CreatePushCheckJson when notification is not an agent notification.
1591 * @tc.type: FUNC
1592 */
1593 HWTEST_F(AdvancedNotificationServiceUnitTest, CreatePushCheckJson_200, Function | SmallTest | Level1)
1594 {
1595 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest(1);
1596 std::string bundle = "bundle";
1597 request->SetIsAgentNotification(false);
1598 request->SetCreatorBundleName(bundle);
1599 sptr<NotificationCheckRequest> checkRequest = nullptr;
1600 nlohmann::json jsonObject;
1601
1602 advancedNotificationService_->CreatePushCheckJson(request, checkRequest, jsonObject);
1603
1604 ASSERT_EQ(jsonObject["pkgName"], bundle);
1605 }
1606
1607 /**
1608 * @tc.name: CreatePushCheckJson_300
1609 * @tc.desc: test CreatePushCheckJson when notification is common liveview but
1610 * not an agent notification.
1611 * @tc.type: FUNC
1612 */
1613 HWTEST_F(AdvancedNotificationServiceUnitTest, CreatePushCheckJson_300, Function | SmallTest | Level1)
1614 {
1615 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest(1);
1616 std::string bundle = "bundle";
1617 request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1618 request->SetIsAgentNotification(false);
1619 request->SetCreatorBundleName(bundle);
1620 auto liveViewContent = std::make_shared<NotificationLiveViewContent>();
1621 auto content = std::make_shared<NotificationContent>(liveViewContent);
1622 auto extraInfo = std::make_shared<AAFwk::WantParams>();
1623 extraInfo->SetParam("key1", nullptr);
1624 liveViewContent->SetExtraInfo(extraInfo);
1625 request->SetContent(content);
1626 sptr<NotificationCheckRequest> checkRequest = new (std::nothrow) NotificationCheckRequest(
1627 NotificationContent::Type::BASIC_TEXT,
1628 NotificationConstant::SlotType::SOCIAL_COMMUNICATION,
1629 {"key1", "key2"});
1630 nlohmann::json jsonObject;
1631
1632 advancedNotificationService_->CreatePushCheckJson(request, checkRequest, jsonObject);
1633
1634 ASSERT_EQ(jsonObject["pkgName"], bundle);
1635 ASSERT_TRUE(jsonObject.contains("extraInfo"));
1636 }
1637
1638 /**
1639 * @tc.name: PushCheck_100
1640 * @tc.desc: test PushCheck when notification is common liveview.
1641 * @tc.type: FUNC
1642 */
1643 HWTEST_F(AdvancedNotificationServiceUnitTest, PushCheck_100, Function | SmallTest | Level1)
1644 {
1645 auto pushCallbackProxy = new (std::nothrow)MockPushCallBackStub();
1646 EXPECT_NE(pushCallbackProxy, nullptr);
1647 sptr<IRemoteObject> pushCallback = pushCallbackProxy->AsObject();
1648 sptr<NotificationCheckRequest> notificationCheckRequest = new (std::nothrow)NotificationCheckRequest();
1649 notificationCheckRequest->SetUid(SYSTEM_APP_UID);
1650 notificationCheckRequest->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1651 sptr<IPushCallBack> pushCallBack = iface_cast<IPushCallBack>(pushCallback);
1652 advancedNotificationService_->pushCallBacks_.insert_or_assign(
1653 notificationCheckRequest->GetSlotType(), pushCallBack);
1654 advancedNotificationService_->checkRequests_.insert_or_assign(
1655 notificationCheckRequest->GetSlotType(), notificationCheckRequest);
1656 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1657 request->SetNotificationId(1);
1658 request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1659 auto liveViewContent = std::make_shared<NotificationLiveViewContent>();
1660 auto extraInfo = std::make_shared<AAFwk::WantParams>();
1661 sptr<AAFwk::IInterface> value = AAFwk::String::Box("EVENTS");
1662 extraInfo->SetParam("event", value);
1663 liveViewContent->SetExtraInfo(extraInfo);
1664 auto content = std::make_shared<NotificationContent>(liveViewContent);
1665 request->SetContent(content);
1666 request->SetCreatorUid(NON_SYSTEM_APP_UID);
1667 MockIsVerfyPermisson(true);
1668 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
1669 MockOnCheckNotification(ERR_INVALID_STATE);
1670
1671 auto ret = advancedNotificationService_->PushCheck(request);
1672 ASSERT_EQ(ret, (int)ERR_OK);
1673
1674 MockIsVerfyPermisson(false);
1675 ret = advancedNotificationService_->PushCheck(request);
1676 ASSERT_EQ(ret, (int)ERR_INVALID_STATE);
1677 MockOnCheckNotification(ERR_OK);
1678 }
1679
1680 /**
1681 * @tc.name: CheckSoundPermission_100
1682 * @tc.desc: test CheckSoundPermission when sound length exceeds maximum.
1683 * @tc.type: FUNC
1684 */
1685 HWTEST_F(AdvancedNotificationServiceUnitTest, CheckSoundPermission_100, Function | SmallTest | Level1)
1686 {
1687 sptr<NotificationRequest> request = new NotificationRequest();
1688 std::string sound = "1";
1689 for (int i = 0; i < 20; i++) {
1690 sound += sound;
1691 }
1692 sound += "."; // sound length larger than 2048
1693 request->SetSound(sound);
1694 std::string bundle = "bundle";
1695 int32_t uid = 10;
1696 sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundle, uid);
1697
1698 auto ret = advancedNotificationService_->CheckSoundPermission(request, bundleOption);
1699
1700 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1701 }
1702
1703 /**
1704 * @tc.name: CheckSoundPermission_200
1705 * @tc.desc: test CheckSoundPermission.
1706 * @tc.type: FUNC
1707 */
1708 HWTEST_F(AdvancedNotificationServiceUnitTest, CheckSoundPermission_200, Function | SmallTest | Level1)
1709 {
1710 sptr<NotificationRequest> request = new NotificationRequest();
1711 std::string sound = "1";
1712 request->SetSound(sound);
1713 std::string bundle = "bundle";
1714 int32_t uid = 10;
1715 sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundle, uid);
1716
1717 auto ret = advancedNotificationService_->CheckSoundPermission(request, bundleOption);
1718
1719 ASSERT_EQ(ret, (int)ERR_OK);
1720 }
1721
1722 /**
1723 * @tc.name: CheckLongTermLiveView_100
1724 * @tc.desc: test CheckLongTermLiveView when system update only is nullptr.
1725 * @tc.type: FUNC
1726 */
1727 HWTEST_F(AdvancedNotificationServiceUnitTest, CheckLongTermLiveView_100, Function | SmallTest | Level1)
1728 {
1729 sptr<NotificationRequest> request = new NotificationRequest();
1730 auto additionalData = std::make_shared<AAFwk::WantParams>();
1731 additionalData->SetParam("SYSTEM_UPDATE_ONLY", nullptr);
1732 request->SetAdditionalData(additionalData);
1733
1734 auto ret = advancedNotificationService_->CheckLongTermLiveView(request, "");
1735
1736 ASSERT_EQ(ret, (int)ERR_OK);
1737 }
1738
1739 /**
1740 * @tc.name: CheckLongTermLiveView_200
1741 * @tc.desc: test CheckLongTermLiveView when notification doesn't exist.
1742 * @tc.type: FUNC
1743 */
1744 HWTEST_F(AdvancedNotificationServiceUnitTest, CheckLongTermLiveView_200, Function | SmallTest | Level1)
1745 {
1746 sptr<NotificationRequest> request = new NotificationRequest();
1747 auto additionalData = std::make_shared<AAFwk::WantParams>();
1748 sptr<AAFwk::IInterface> value = AAFwk::Boolean::Box(true);
1749 additionalData->SetParam("SYSTEM_UPDATE_ONLY", value);
1750 request->SetAdditionalData(additionalData);
1751
1752 auto ret = advancedNotificationService_->CheckLongTermLiveView(request, "");
1753
1754 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1755 }
1756
1757 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
1758 /**
1759 * @tc.name: RegisterSwingCallback_100
1760 * @tc.desc: test RegisterSwingCallback when caller is not subsystem and system app.
1761 * @tc.type: FUNC
1762 */
1763 HWTEST_F(AdvancedNotificationServiceUnitTest, RegisterSwingCallback_100, Function | SmallTest | Level1)
1764 {
1765 sptr<IRemoteObject> swingCallback = nullptr;
1766 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
1767 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1768 MockIsSystemApp(false);
1769
1770 auto ret = advancedNotificationService_->RegisterSwingCallback(swingCallback);
1771
1772 ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
1773 }
1774
1775 /**
1776 * @tc.name: RegisterSwingCallback_200
1777 * @tc.desc: test RegisterSwingCallback when caller has no permission.
1778 * @tc.type: FUNC
1779 */
1780 HWTEST_F(AdvancedNotificationServiceUnitTest, RegisterSwingCallback_200, Function | SmallTest | Level1)
1781 {
1782 sptr<IRemoteObject> swingCallback = nullptr;
1783 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
1784 MockIsVerfyPermisson(false);
1785
1786 auto ret = advancedNotificationService_->RegisterSwingCallback(swingCallback);
1787
1788 ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
1789 }
1790
1791 /**
1792 * @tc.name: RegisterSwingCallback_300
1793 * @tc.desc: test RegisterSwingCallback when caller has no permission.
1794 * @tc.type: FUNC
1795 */
1796 HWTEST_F(AdvancedNotificationServiceUnitTest, RegisterSwingCallback_300, Function | SmallTest | Level1)
1797 {
1798 sptr<IRemoteObject> swingCallback = nullptr;
1799 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
1800 MockIsVerfyPermisson(true);
1801 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
1802
1803 auto ret = advancedNotificationService_->RegisterSwingCallback(swingCallback);
1804
1805 ASSERT_EQ(ret, (int)ERR_INVALID_VALUE);
1806 }
1807 #endif
1808
1809 /**
1810 * @tc.name: UpdateNotificationTimerByUid_100
1811 * @tc.desc: test UpdateNotificationTimerByUid when caller is not subsystem and system app.
1812 * @tc.type: FUNC
1813 */
1814 HWTEST_F(AdvancedNotificationServiceUnitTest, UpdateNotificationTimerByUid_100, Function | SmallTest | Level1)
1815 {
1816 int32_t uid = SYSTEM_APP_UID;
1817 bool isPaused = false;
1818 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
1819 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1820 MockIsSystemApp(false);
1821
1822 auto ret = advancedNotificationService_->UpdateNotificationTimerByUid(uid, isPaused);
1823
1824 ASSERT_EQ(ret, (int)ERR_ANS_NOT_SYSTEM_SERVICE);
1825 }
1826
1827 /**
1828 * @tc.name: UpdateNotificationTimerByUid_200
1829 * @tc.desc: test UpdateNotificationTimerByUid when notificationSvrQueue_ is nullptr.
1830 * @tc.type: FUNC
1831 */
1832 HWTEST_F(AdvancedNotificationServiceUnitTest, UpdateNotificationTimerByUid_200, Function | SmallTest | Level1)
1833 {
1834 int32_t uid = SYSTEM_APP_UID;
1835 bool isPaused = false;
1836 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
1837 IPCSkeleton::SetCallingUid(1096); // RESSCHED_UID
1838 advancedNotificationService_->notificationSvrQueue_ = nullptr;
1839
1840 auto ret = advancedNotificationService_->UpdateNotificationTimerByUid(uid, isPaused);
1841
1842 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1843 }
1844
1845 /**
1846 * @tc.name: UpdateNotificationTimerByUid_300
1847 * @tc.desc: test UpdateNotificationTimerByUid.
1848 * @tc.type: FUNC
1849 */
1850 HWTEST_F(AdvancedNotificationServiceUnitTest, UpdateNotificationTimerByUid_300, Function | SmallTest | Level1)
1851 {
1852 int32_t uid = SYSTEM_APP_UID;
1853 bool isPaused = false;
1854 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
1855 IPCSkeleton::SetCallingUid(1096); // RESSCHED_UID
1856
1857 auto ret = advancedNotificationService_->UpdateNotificationTimerByUid(uid, isPaused);
1858
1859 ASSERT_EQ(ret, (int)ERR_OK);
1860 }
1861
1862 /**
1863 * @tc.name: DisableNotificationFeature_100
1864 * @tc.desc: test DisableNotificationFeature when caller is not subsystem and system app.
1865 * @tc.type: FUNC
1866 */
1867 HWTEST_F(AdvancedNotificationServiceUnitTest, DisableNotificationFeature_100, Function | SmallTest | Level1)
1868 {
1869 sptr<NotificationDisable> notificationDisable = new (std::nothrow) NotificationDisable();
1870 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID);
1871 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1872 MockIsSystemApp(false);
1873
1874 auto ret = advancedNotificationService_->DisableNotificationFeature(notificationDisable);
1875
1876 ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP);
1877 }
1878
1879 /**
1880 * @tc.name: DisableNotificationFeature_200
1881 * @tc.desc: test DisableNotificationFeature when caller has no permission.
1882 * @tc.type: FUNC
1883 */
1884 HWTEST_F(AdvancedNotificationServiceUnitTest, DisableNotificationFeature_200, Function | SmallTest | Level1)
1885 {
1886 sptr<NotificationDisable> notificationDisable = new (std::nothrow) NotificationDisable();
1887 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1888 MockIsSystemApp(true);
1889 MockIsVerfyPermisson(false);
1890
1891 auto ret = advancedNotificationService_->DisableNotificationFeature(notificationDisable);
1892
1893 ASSERT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED);
1894 }
1895
1896 /**
1897 * @tc.name: DisableNotificationFeature_300
1898 * @tc.desc: test DisableNotificationFeature when notificationSvrQueue_ is nullptr.
1899 * @tc.type: FUNC
1900 */
1901 HWTEST_F(AdvancedNotificationServiceUnitTest, DisableNotificationFeature_300, Function | SmallTest | Level1)
1902 {
1903 sptr<NotificationDisable> notificationDisable = new (std::nothrow) NotificationDisable();
1904 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1905 MockIsSystemApp(true);
1906 MockIsVerfyPermisson(true);
1907 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
1908 advancedNotificationService_->notificationSvrQueue_ = nullptr;
1909
1910 auto ret = advancedNotificationService_->DisableNotificationFeature(notificationDisable);
1911
1912 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
1913 }
1914
1915 /**
1916 * @tc.name: DisableNotificationFeature_400
1917 * @tc.desc: test DisableNotificationFeature when disabiled is true.
1918 * @tc.type: FUNC
1919 */
1920 HWTEST_F(AdvancedNotificationServiceUnitTest, DisableNotificationFeature_400, Function | SmallTest | Level1)
1921 {
1922 sptr<NotificationDisable> notificationDisable = new (std::nothrow) NotificationDisable();
1923 notificationDisable->SetDisabled(true);
1924 std::vector<std::string> bundleList = {"bundle1", "bundle2"};
1925 notificationDisable->SetBundleList(bundleList);
1926 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1927 MockIsSystemApp(true);
1928 MockIsVerfyPermisson(true);
1929 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
1930
1931 auto ret = advancedNotificationService_->DisableNotificationFeature(notificationDisable);
1932
1933 ASSERT_EQ(ret, (int)ERR_OK);
1934 }
1935 }
1936 }