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 "pixel_map.h"
31 #include "int_wrapper.h"
32
33 using namespace testing::ext;
34 using namespace OHOS::Security::AccessToken;
35 using namespace OHOS::Media;
36
37 namespace OHOS {
38 namespace Notification {
39 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
40 extern void MockIsSystemApp(bool isSystemApp);
41
42 class AnsLiveViewServiceTest : public testing::Test {
43 public:
SetUpTestCase()44 static void SetUpTestCase() {};
TearDownTestCase()45 static void TearDownTestCase() {};
46 void SetUp();
47 void TearDown();
48 std::shared_ptr<PixelMap> MakePixelMap(int32_t width, int32_t height);
49
50 private:
51 static sptr<AdvancedNotificationService> advancedNotificationService_;
52 };
53
54 sptr<AdvancedNotificationService> AnsLiveViewServiceTest::advancedNotificationService_ = nullptr;
55
MakePixelMap(int32_t width,int32_t height)56 std::shared_ptr<PixelMap> AnsLiveViewServiceTest::MakePixelMap(int32_t width, int32_t height)
57 {
58 const int32_t PIXEL_BYTES = 4;
59 std::shared_ptr<PixelMap> pixelMap = std::make_shared<PixelMap>();
60 if (pixelMap == nullptr) {
61 return pixelMap;
62 }
63 ImageInfo info;
64 info.size.width = width;
65 info.size.height = height;
66 info.pixelFormat = PixelFormat::ARGB_8888;
67 info.colorSpace = ColorSpace::SRGB;
68 pixelMap->SetImageInfo(info);
69 int32_t rowDataSize = width * PIXEL_BYTES;
70 uint32_t bufferSize = rowDataSize * height;
71 void *buffer = malloc(bufferSize);
72 if (buffer != nullptr) {
73 pixelMap->SetPixelsAddr(buffer, nullptr, bufferSize, AllocatorType::HEAP_ALLOC, nullptr);
74 }
75 EXPECT_NE(buffer, nullptr);
76 return pixelMap;
77 }
78
SetUp()79 void AnsLiveViewServiceTest::SetUp()
80 {
81 GTEST_LOG_(INFO) << "SetUp start";
82
83 advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService();
84 NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
85 advancedNotificationService_->CancelAll("");
86 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
87 MockIsSystemApp(true);
88 GTEST_LOG_(INFO) << "SetUp end";
89 }
90
TearDown()91 void AnsLiveViewServiceTest::TearDown()
92 {
93 delete advancedNotificationService_;
94 advancedNotificationService_ = nullptr;
95 GTEST_LOG_(INFO) << "TearDown";
96 }
97
98 /**
99 * @tc.name: ProcForDeleteLiveView_00001
100 * @tc.desc: Test ProcForDeleteLiveView
101 * @tc.type: FUNC
102 * @tc.require: issue
103 */
104 HWTEST_F(AnsLiveViewServiceTest, ProcForDeleteLiveView_00001, Function | SmallTest | Level1)
105 {
106 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
107 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
108 request->SetSlotType(slotType);
109 request->SetNotificationId(1);
110 auto liveContent = std::make_shared<NotificationLiveViewContent>();
111 auto content = std::make_shared<NotificationContent>(liveContent);
112 request->SetContent(content);
113 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
114
115 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
116 AdvancedNotificationService::NotificationRequestDb requestDb =
117 { .request = record->request, .bundleOption = bundle};
118 auto ret = advancedNotificationService_->SetNotificationRequestToDb(requestDb);
119 ASSERT_EQ(ret, (int)ERR_OK);
120
121 std::vector<AdvancedNotificationService::NotificationRequestDb> requestsdb;
122 ret = advancedNotificationService_->GetBatchNotificationRequestsFromDb(requestsdb);
123 ASSERT_EQ(requestsdb.size(), 1);
124
125 advancedNotificationService_->ProcForDeleteLiveView(record);
126 requestsdb.clear();
127 ret = advancedNotificationService_->GetBatchNotificationRequestsFromDb(requestsdb);
128 ASSERT_EQ(requestsdb.size(), 0);
129 }
130
131 /**
132 * @tc.name: SetNotificationRequestToDb_00001
133 * @tc.desc: Test SetNotificationRequestToDb
134 * @tc.type: FUNC
135 * @tc.require: issue
136 */
137 HWTEST_F(AnsLiveViewServiceTest, SetNotificationRequestToDb_00001, Function | SmallTest | Level1)
138 {
139 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
140 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
141 request->SetSlotType(slotType);
142 request->SetNotificationId(1);
143 request->SetAutoDeletedTime(NotificationConstant::NO_DELAY_DELETE_TIME);
144 auto liveContent = std::make_shared<NotificationLiveViewContent>();
145 liveContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_END);
146 auto content = std::make_shared<NotificationContent>(liveContent);
147 request->SetContent(content);
148 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
149
150 AdvancedNotificationService::NotificationRequestDb requestDb =
151 { .request = request, .bundleOption = bundle};
152 auto ret = advancedNotificationService_->SetNotificationRequestToDb(requestDb);
153 ASSERT_EQ(ret, (int)ERR_OK);
154
155 std::vector<AdvancedNotificationService::NotificationRequestDb> requestsdb;
156 ret = advancedNotificationService_->GetBatchNotificationRequestsFromDb(requestsdb);
157 ASSERT_EQ(requestsdb.size(), 0);
158 }
159
160 /**
161 * @tc.name: SetNotificationRequestToDb_00002
162 * @tc.desc: Test SetNotificationRequestToDb
163 * @tc.type: FUNC
164 * @tc.require: issue
165 */
166 HWTEST_F(AnsLiveViewServiceTest, SetNotificationRequestToDb_00002, Function | SmallTest | Level1)
167 {
168 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
169 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
170 request->SetSlotType(slotType);
171 request->SetNotificationId(1);
172 request->SetReceiverUserId(100);
173 auto liveContent = std::make_shared<NotificationLiveViewContent>();
174 auto content = std::make_shared<NotificationContent>(liveContent);
175 request->SetContent(content);
176 auto bundle = new NotificationBundleOption("test", 1);
177 AdvancedNotificationService::NotificationRequestDb requestDb =
178 { .request = request, .bundleOption = bundle};
179 auto ret = advancedNotificationService_->SetNotificationRequestToDb(requestDb);
180 ASSERT_EQ(ret, (int)ERR_OK);
181 }
182
183 /**
184 * @tc.name: FillLockScreenPicture_00001
185 * @tc.desc: Test FillLockScreenPicture
186 * @tc.type: FUNC
187 * @tc.require: issue
188 */
189 HWTEST_F(AnsLiveViewServiceTest, FillLockScreenPicture_00001, Function | SmallTest | Level1)
190 {
191 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
192 sptr<NotificationRequest> newRequest = new (std::nothrow) NotificationRequest();
193 newRequest->SetSlotType(slotType);
194 newRequest->SetNotificationId(1);
195 auto newLiveContent = std::make_shared<NotificationLiveViewContent>();
196 auto newContent = std::make_shared<NotificationContent>(newLiveContent);
197 newRequest->SetContent(newContent);
198
199 sptr<NotificationRequest> oldRequest = new (std::nothrow) NotificationRequest();
200 oldRequest->SetSlotType(slotType);
201 oldRequest->SetNotificationId(1);
202 auto oldLiveContent = std::make_shared<NotificationLiveViewContent>();
203 auto oldContent = std::make_shared<NotificationContent>(oldLiveContent);
204
205 std::shared_ptr<PixelMap> pixelMap = MakePixelMap(1, 1);
206 oldLiveContent->SetLockScreenPicture(pixelMap);
207 oldRequest->SetContent(oldContent);
208
209 advancedNotificationService_->FillLockScreenPicture(newRequest, oldRequest);
210 EXPECT_NE(newRequest->GetContent()->GetNotificationContent()->GetLockScreenPicture(), nullptr);
211 }
212
213 /**
214 * @tc.name: SetLockScreenPictureToDb_001
215 * @tc.desc: Test SetLockScreenPictureToDb
216 * @tc.type: FUNC
217 * @tc.require: issue
218 */
219 HWTEST_F(AnsLiveViewServiceTest, SetLockScreenPictureToDb_001, Function | SmallTest | Level1)
220 {
221 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
222 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
223 request->SetSlotType(slotType);
224 request->SetNotificationId(1);
225 request->SetAutoDeletedTime(NotificationConstant::NO_DELAY_DELETE_TIME);
226 auto liveContent = std::make_shared<NotificationLiveViewContent>();
227 liveContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE);
228 auto content = std::make_shared<NotificationContent>(liveContent);
229 std::shared_ptr<PixelMap> pixelMap = MakePixelMap(1024, 1);
230 liveContent->SetLockScreenPicture(pixelMap);
231 request->SetContent(content);
232
233 auto ret = advancedNotificationService_->SetLockScreenPictureToDb(request);
234 ASSERT_EQ(ret, (int)ERR_OK);
235 }
236
237 /**
238 * @tc.name: IsSaCreateSystemLiveViewAsBundle_001
239 * @tc.desc: Test IsSaCreateSystemLiveViewAsBundle
240 * @tc.type: FUNC
241 * @tc.require: issue
242 */
243 HWTEST_F(AnsLiveViewServiceTest, IsSaCreateSystemLiveViewAsBundle_001, Function | SmallTest | Level1)
244 {
245 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
246 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
247 request->SetSlotType(slotType);
248 request->SetNotificationId(1);
249 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
250 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
251 request->SetContent(content);
252 int creatorUid = 1;
253 request->SetCreatorUid(creatorUid);
254 int ownerUid = 2;
255 request->SetOwnerUid(ownerUid);
256 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", creatorUid);
257 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
258 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
259 bool flag = advancedNotificationService_->IsSaCreateSystemLiveViewAsBundle(record, creatorUid);
260 ASSERT_EQ(flag, true);
261 }
262
263 /**
264 * @tc.name: IsSaCreateSystemLiveViewAsBundle_002
265 * @tc.desc: Test IsSaCreateSystemLiveViewAsBundle return false
266 * @tc.type: FUNC
267 * @tc.require: issue
268 */
269 HWTEST_F(AnsLiveViewServiceTest, IsSaCreateSystemLiveViewAsBundle_002, Function | SmallTest | Level1)
270 {
271 auto slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
272 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
273 request->SetSlotType(slotType);
274 request->SetNotificationId(1);
275 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
276 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
277 request->SetContent(content);
278 int creatorUid = 1;
279 request->SetCreatorUid(creatorUid);
280 int ownerUid = 2;
281 request->SetOwnerUid(ownerUid);
282 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
283 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
284
285 bool flag = advancedNotificationService_->IsSaCreateSystemLiveViewAsBundle(nullptr, creatorUid);
286 ASSERT_EQ(flag, false);
287
288 flag = advancedNotificationService_->IsSaCreateSystemLiveViewAsBundle(record, creatorUid);
289 ASSERT_EQ(flag, false);
290
291 record->notification->GetNotificationRequest().SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
292 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
293 flag = advancedNotificationService_->IsSaCreateSystemLiveViewAsBundle(record, creatorUid);
294 ASSERT_EQ(flag, false);
295 }
296
297 /**
298 * @tc.name: HandleUpdateLiveViewNotificationTimer_001
299 * @tc.desc: Test HandleUpdateLiveViewNotificationTimer
300 * @tc.type: FUNC
301 * @tc.require: issue
302 */
303 HWTEST_F(AnsLiveViewServiceTest, HandleUpdateLiveViewNotificationTimer_001, Function | SmallTest | Level1)
304 {
305 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
306 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
307 request->SetSlotType(slotType);
308 request->SetNotificationId(1);
309 int32_t TYPE_CODE_DOWNLOAD = 8;
310 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
311 localLiveViewContent->SetType(TYPE_CODE_DOWNLOAD);
312 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
313 request->SetContent(content);
314 int creatorUid = 3051;
315 request->SetCreatorUid(creatorUid);
316 int ownerUid = 20099999;
317 request->SetOwnerUid(ownerUid);
318 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", ownerUid);
319 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
320 advancedNotificationService_->AddToNotificationList(record);
321 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
322 auto timer = record->notification->GetFinishTimer();
323 advancedNotificationService_->HandleUpdateLiveViewNotificationTimer(ownerUid, true);
324 ASSERT_EQ(timer, record->notification->GetFinishTimer());
325 advancedNotificationService_->HandleUpdateLiveViewNotificationTimer(ownerUid, false);
326 ASSERT_NE(timer, record->notification->GetFinishTimer());
327 }
328
329 /**
330 * @tc.name: AddToDelayNotificationList_001
331 * @tc.desc: Test AddToDelayNotificationList return 1
332 * @tc.type: FUNC
333 * @tc.require: issue
334 */
335 HWTEST_F(AnsLiveViewServiceTest, AddToDelayNotificationList_001, Function | SmallTest | Level1)
336 {
337 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
338 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
339 request->SetSlotType(slotType);
340 request->SetNotificationId(1);
341 auto liveContent = std::make_shared<NotificationLiveViewContent>();
342 auto content = std::make_shared<NotificationContent>(liveContent);
343 request->SetContent(content);
344 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
345
346 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
347 advancedNotificationService_->AddToDelayNotificationList(record);
348 ASSERT_EQ(advancedNotificationService_->delayNotificationList_.size(), 1);
349 }
350
351 /**
352 * @tc.name: OnSubscriberAdd_100
353 * @tc.desc: Test OnSubscriberAdd when record is nullptr
354 * @tc.type: FUNC
355 */
356 HWTEST_F(AnsLiveViewServiceTest, OnSubscriberAdd_100, Function | SmallTest | Level1)
357 {
358 advancedNotificationService_->currentUserId.clear();
359 auto ret = advancedNotificationService_->OnSubscriberAdd(nullptr, 100);
360
361 ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM);
362 }
363
364 /**
365 * @tc.name: OnSubscriberAdd_200
366 * @tc.desc: Test OnSubscriberAdd when notification doesn't exist
367 * @tc.type: FUNC
368 */
369 HWTEST_F(AnsLiveViewServiceTest, OnSubscriberAdd_200, Function | SmallTest | Level1)
370 {
371 auto record = NotificationSubscriberManager::GetInstance()->CreateSubscriberRecord(nullptr);
372
373 advancedNotificationService_->currentUserId.clear();
374 auto ret = advancedNotificationService_->OnSubscriberAdd(record, 100);
375
376 ASSERT_EQ(ret, (int)ERR_ANS_NOTIFICATION_NOT_EXISTS);
377 }
378
379 /**
380 * @tc.name: OnSubscriberAdd_300
381 * @tc.desc: Test OnSubscriberAdd when notification exists
382 * @tc.type: FUNC
383 */
384 HWTEST_F(AnsLiveViewServiceTest, OnSubscriberAdd_300, Function | SmallTest | Level1)
385 {
386 advancedNotificationService_->currentUserId.clear();
387 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
388 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
389 request->SetSlotType(slotType);
390 request->SetNotificationId(1);
391 auto liveContent = std::make_shared<NotificationLiveViewContent>();
392 auto content = std::make_shared<NotificationContent>(liveContent);
393 request->SetContent(content);
394 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
395 auto notificationRecord = advancedNotificationService_->MakeNotificationRecord(request, bundle);
396 advancedNotificationService_->AddToNotificationList(notificationRecord);
397
398 auto record = NotificationSubscriberManager::GetInstance()->CreateSubscriberRecord(nullptr);
399
400 auto ret = advancedNotificationService_->OnSubscriberAdd(record, 100);
401
402 ASSERT_EQ(ret, (int)ERR_OK);
403 }
404
405 /**
406 * @tc.name: SetNotificationRequestToDb_100
407 * @tc.desc: Test SetNotificationRequestToDb when isOnlyLocalUpdate is true
408 * @tc.type: FUNC
409 */
410 HWTEST_F(AnsLiveViewServiceTest, SetNotificationRequestToDb_100, Function | SmallTest | Level1)
411 {
412 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
413 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
414 request->SetSlotType(slotType);
415 request->SetNotificationId(1);
416 auto liveContent = std::make_shared<NotificationLiveViewContent>();
417 liveContent->SetIsOnlyLocalUpdate(true);
418 auto content = std::make_shared<NotificationContent>(liveContent);
419 request->SetContent(content);
420 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
421 AdvancedNotificationService::NotificationRequestDb requestDb =
422 { .request = request, .bundleOption = bundle};
423
424 auto ret = advancedNotificationService_->SetNotificationRequestToDb(requestDb);
425
426 ASSERT_EQ(ret, (int)ERR_OK);
427 }
428
429 /**
430 * @tc.name: UpdateInDelayNotificationList_100
431 * @tc.desc: Test UpdateInDelayNotificationList when publish immediately
432 * @tc.type: FUNC
433 */
434 HWTEST_F(AnsLiveViewServiceTest, UpdateInDelayNotificationList_100, Function | SmallTest | Level1)
435 {
436 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
437 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
438 request->SetSlotType(slotType);
439 request->SetNotificationId(1);
440 request->SetPublishDelayTime(0);
441 auto liveContent = std::make_shared<NotificationLiveViewContent>();
442 auto content = std::make_shared<NotificationContent>(liveContent);
443 request->SetContent(content);
444 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
445 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
446 advancedNotificationService_->AddToDelayNotificationList(record);
447
448 sptr<NotificationRequest> request1 = new (std::nothrow) NotificationRequest();
449 request1->SetSlotType(slotType);
450 request1->SetNotificationId(1);
451 request1->SetPublishDelayTime(1000);
452 auto liveContent1 = std::make_shared<NotificationLiveViewContent>();
453 auto content1 = std::make_shared<NotificationContent>(liveContent1);
454 request1->SetContent(content1);
455 sptr<NotificationBundleOption> bundle1 = new NotificationBundleOption("test", 1);
456 auto record1 = advancedNotificationService_->MakeNotificationRecord(request1, bundle1);
457 advancedNotificationService_->UpdateInDelayNotificationList(record1);
458
459 auto iter = advancedNotificationService_->delayNotificationList_.begin();
460 ASSERT_EQ((*iter).first->request->GetPublishDelayTime(), 1000);
461 }
462
463 /**
464 * @tc.name: SaPublishSystemLiveViewAsBundle_100
465 * @tc.desc: Test SaPublishSystemLiveViewAsBundle when publish immediately
466 * @tc.type: FUNC
467 */
468 HWTEST_F(AnsLiveViewServiceTest, SaPublishSystemLiveViewAsBundle_100, Function | SmallTest | Level1)
469 {
470 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
471 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
472 request->SetSlotType(slotType);
473 request->SetNotificationId(1);
474 request->SetPublishDelayTime(0);
475 auto liveContent = std::make_shared<NotificationLiveViewContent>();
476 auto content = std::make_shared<NotificationContent>(liveContent);
477 request->SetContent(content);
478 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
479 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
480
481 auto ret = advancedNotificationService_->SaPublishSystemLiveViewAsBundle(record);
482
483 ASSERT_EQ(ret, (int)ERR_OK);
484 }
485
486 /**
487 * @tc.name: SaPublishSystemLiveViewAsBundle_200
488 * @tc.desc: Test SaPublishSystemLiveViewAsBundle when notification exists
489 * @tc.type: FUNC
490 */
491 HWTEST_F(AnsLiveViewServiceTest, SaPublishSystemLiveViewAsBundle_200, Function | SmallTest | Level1)
492 {
493 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
494 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
495 request->SetSlotType(slotType);
496 request->SetNotificationId(1);
497 request->SetPublishDelayTime(1000);
498 auto liveContent = std::make_shared<NotificationLiveViewContent>();
499 auto content = std::make_shared<NotificationContent>(liveContent);
500 request->SetContent(content);
501 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
502 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
503 advancedNotificationService_->AddToDelayNotificationList(record);
504
505 auto ret = advancedNotificationService_->SaPublishSystemLiveViewAsBundle(record);
506
507 ASSERT_EQ(ret, (int)ERR_OK);
508 }
509
510 /**
511 * @tc.name: SaPublishSystemLiveViewAsBundle_300
512 * @tc.desc: Test SaPublishSystemLiveViewAsBundle when notification doesn't exist
513 * @tc.type: FUNC
514 */
515 HWTEST_F(AnsLiveViewServiceTest, SaPublishSystemLiveViewAsBundle_300, Function | SmallTest | Level1)
516 {
517 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
518 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
519 request->SetSlotType(slotType);
520 request->SetNotificationId(1);
521 request->SetPublishDelayTime(1000);
522 auto liveContent = std::make_shared<NotificationLiveViewContent>();
523 auto content = std::make_shared<NotificationContent>(liveContent);
524 request->SetContent(content);
525 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
526 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
527
528 auto ret = advancedNotificationService_->SaPublishSystemLiveViewAsBundle(record);
529
530 ASSERT_EQ(ret, (int)ERR_OK);
531 }
532
533 /**
534 * @tc.name: IsNotificationExistsInDelayList_100
535 * @tc.desc: Test IsNotificationExistsInDelayList when notification exists
536 * @tc.type: FUNC
537 */
538 HWTEST_F(AnsLiveViewServiceTest, IsNotificationExistsInDelayList_100, Function | SmallTest | Level1)
539 {
540 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
541 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
542 request->SetSlotType(slotType);
543 request->SetNotificationId(1);
544 auto liveContent = std::make_shared<NotificationLiveViewContent>();
545 auto content = std::make_shared<NotificationContent>(liveContent);
546 request->SetContent(content);
547 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
548 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
549 advancedNotificationService_->AddToDelayNotificationList(record);
550 std::string key = record->notification->GetKey();
551
552 auto ret = advancedNotificationService_->IsNotificationExistsInDelayList(key);
553
554 ASSERT_TRUE(ret);
555 }
556
557 /**
558 * @tc.name: IsNotificationExistsInDelayList_200
559 * @tc.desc: Test IsNotificationExistsInDelayList when notification doesn't exist
560 * @tc.type: FUNC
561 */
562 HWTEST_F(AnsLiveViewServiceTest, IsNotificationExistsInDelayList_200, Function | SmallTest | Level1)
563 {
564 std::string key = "bunlde";
565
566 auto ret = advancedNotificationService_->IsNotificationExistsInDelayList(key);
567
568 ASSERT_FALSE(ret);
569 }
570
571 /**
572 * @tc.name: StartPublishDelayedNotificationTimeOut_100
573 * @tc.desc: Test StartPublishDelayedNotificationTimeOut when publish with updateOnly is true
574 * @tc.type: FUNC
575 */
576 HWTEST_F(AnsLiveViewServiceTest, StartPublishDelayedNotificationTimeOut_100, Function | SmallTest | Level1)
577 {
578 int32_t ownerUid = 1;
579 int32_t notificationId = 1;
580
581 advancedNotificationService_->StartPublishDelayedNotificationTimeOut(ownerUid, notificationId);
582
583 auto ret = advancedNotificationService_->GetFromDelayedNotificationList(ownerUid, notificationId);
584 ASSERT_EQ(ret, nullptr);
585 }
586
587 /**
588 * @tc.name: StartPublishDelayedNotification_100
589 * @tc.desc: Test StartPublishDelayedNotification when publish with updateOnly is true
590 * @tc.type: FUNC
591 */
592 HWTEST_F(AnsLiveViewServiceTest, StartPublishDelayedNotification_100, Function | SmallTest | Level1)
593 {
594 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
595 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
596 request->SetSlotType(slotType);
597 request->SetUpdateOnly(true);
598 request->SetNotificationId(1);
599 auto liveContent = std::make_shared<NotificationLiveViewContent>();
600 auto content = std::make_shared<NotificationContent>(liveContent);
601 request->SetContent(content);
602 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
603 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
604
605 auto ret = advancedNotificationService_->StartPublishDelayedNotification(record);
606
607 ASSERT_EQ(ret, (int)ERR_ANS_NOTIFICATION_NOT_EXISTS);
608 }
609
610 /**
611 * @tc.name: StartPublishDelayedNotification_200
612 * @tc.desc: Test StartPublishDelayedNotification when notificationList_ is empty
613 * @tc.type: FUNC
614 */
615 HWTEST_F(AnsLiveViewServiceTest, StartPublishDelayedNotification_200, Function | SmallTest | Level1)
616 {
617 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
618 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
619 request->SetSlotType(slotType);
620 request->SetNotificationId(1);
621 request->SetAutoDeletedTime(INT64_MAX);
622 auto liveContent = std::make_shared<NotificationLiveViewContent>();
623 auto content = std::make_shared<NotificationContent>(liveContent);
624 request->SetContent(content);
625 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
626 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
627
628 auto ret = advancedNotificationService_->StartPublishDelayedNotification(record);
629
630 ASSERT_EQ(ret, (int)ERR_OK);
631 }
632
633 /**
634 * @tc.name: UpdateRecordByOwner_100
635 * @tc.desc: Test UpdateRecordByOwner when notificationList_ is empty
636 * @tc.type: FUNC
637 */
638 HWTEST_F(AnsLiveViewServiceTest, UpdateRecordByOwner_100, Function | SmallTest | Level1)
639 {
640 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
641 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
642 request->SetSlotType(slotType);
643 request->SetNotificationId(1);
644 auto liveContent = std::make_shared<NotificationLiveViewContent>();
645 auto content = std::make_shared<NotificationContent>(liveContent);
646 request->SetContent(content);
647 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
648 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
649 bool isSystem = false;
650
651 advancedNotificationService_->UpdateRecordByOwner(record, isSystem);
652 auto creatorUid = request->GetCreatorUid();
653 auto notificationId = request->GetNotificationId();
654 auto ret = advancedNotificationService_->GetFromNotificationList(creatorUid, notificationId);
655
656 ASSERT_EQ(ret, nullptr);
657 }
658
659 /**
660 * @tc.name: UpdateRecordByOwner_200
661 * @tc.desc: Test UpdateRecordByOwner when isSystem is true and timerId is 0
662 * @tc.type: FUNC
663 */
664 HWTEST_F(AnsLiveViewServiceTest, UpdateRecordByOwner_200, Function | SmallTest | Level1)
665 {
666 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
667 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
668 request->SetSlotType(slotType);
669 request->SetNotificationId(1);
670 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
671 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
672 request->SetContent(content);
673 request->SetUpdateByOwnerAllowed(true);
674 std::shared_ptr<NotificationTemplate> notiTemplate = std::make_shared<NotificationTemplate>();
675 std::shared_ptr<AAFwk::WantParams> data = std::make_shared<AAFwk::WantParams>();
676 data->SetParam("progressValue", AAFwk::Integer::Box(1));
677 notiTemplate->SetTemplateData(data);
678 request->SetTemplate(notiTemplate);
679 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent =
680 std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
681 request->SetWantAgent(agent);
682 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
683 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
684 record->notification->SetFinishTimer(1);
685 advancedNotificationService_->AddToNotificationList(record);
686 bool isSystem = true;
687
688 advancedNotificationService_->UpdateRecordByOwner(record, isSystem);
689 auto ret = record->notification->GetFinishTimer();
690
691 ASSERT_EQ(ret, 0);
692 }
693
694 /**
695 * @tc.name: UpdateRecordByOwner_300
696 * @tc.desc: Test UpdateRecordByOwner when isSystem is false and timerId is not 0
697 * @tc.type: FUNC
698 */
699 HWTEST_F(AnsLiveViewServiceTest, UpdateRecordByOwner_300, Function | SmallTest | Level1)
700 {
701 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
702 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
703 request->SetSlotType(slotType);
704 request->SetNotificationId(1);
705 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
706 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
707 request->SetContent(content);
708 request->SetUpdateByOwnerAllowed(true);
709 std::shared_ptr<NotificationTemplate> notiTemplate = std::make_shared<NotificationTemplate>();
710 std::shared_ptr<AAFwk::WantParams> data = std::make_shared<AAFwk::WantParams>();
711 data->SetParam("progressValue", AAFwk::Integer::Box(1));
712 notiTemplate->SetTemplateData(data);
713 request->SetTemplate(notiTemplate);
714 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent =
715 std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
716 request->SetWantAgent(agent);
717 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
718 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
719 record->notification->SetFinishTimer(1);
720 advancedNotificationService_->AddToNotificationList(record);
721 bool isSystem = false;
722
723 advancedNotificationService_->UpdateRecordByOwner(record, isSystem);
724 auto ret = record->notification->GetFinishTimer();
725
726 ASSERT_NE(ret, 0);
727 }
728
729 /**
730 * @tc.name: StartFinishTimerForUpdate_100
731 * @tc.desc: Test StartFinishTimerForUpdate when process is FINISH_PER
732 * @tc.type: FUNC
733 */
734 HWTEST_F(AnsLiveViewServiceTest, StartFinishTimerForUpdate_100, Function | SmallTest | Level1)
735 {
736 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
737 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
738 request->SetSlotType(slotType);
739 request->SetNotificationId(1);
740 auto liveContent = std::make_shared<NotificationLiveViewContent>();
741 auto content = std::make_shared<NotificationContent>(liveContent);
742 request->SetContent(content);
743 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
744 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
745 uint64_t process = NotificationConstant::FINISH_PER;
746
747 advancedNotificationService_->StartFinishTimerForUpdate(record, process);
748
749 ASSERT_EQ(record->finish_status, AdvancedNotificationService::UploadStatus::FINISH);
750 }
751
752 /**
753 * @tc.name: StartFinishTimerForUpdate_200
754 * @tc.desc: Test StartFinishTimerForUpdate when process is not FINISH_PER
755 * @tc.type: FUNC
756 */
757 HWTEST_F(AnsLiveViewServiceTest, StartFinishTimerForUpdate_200, Function | SmallTest | Level1)
758 {
759 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
760 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
761 request->SetSlotType(slotType);
762 request->SetNotificationId(1);
763 auto liveContent = std::make_shared<NotificationLiveViewContent>();
764 auto content = std::make_shared<NotificationContent>(liveContent);
765 request->SetContent(content);
766 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
767 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
768 uint64_t process = NotificationConstant::DEFAULT_FINISH_STATUS;
769
770 advancedNotificationService_->StartFinishTimerForUpdate(record, process);
771
772 ASSERT_EQ(record->finish_status, AdvancedNotificationService::UploadStatus::CONTINUOUS_UPDATE_TIME_OUT);
773 }
774 } // namespace Notification
775 } // namespace OHOS
776