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 "errors.h"
17 #include "notification_content.h"
18 #include "notification_record.h"
19 #include "notification_request.h"
20 #include <chrono>
21 #include <functional>
22 #include <memory>
23 #include <thread>
24
25 #include "gtest/gtest.h"
26 #include <vector>
27
28 #define private public
29
30 #include "advanced_notification_service.h"
31 #include "ans_const_define.h"
32 #include "ans_inner_errors.h"
33 #include "ans_log_wrapper.h"
34 #include "ans_notification.h"
35 #include "ans_ut_constant.h"
36 #include "common_event_manager.h"
37 #include "common_event_support.h"
38 #include "iremote_object.h"
39 #include "mock_ipc_skeleton.h"
40 #include "notification_preferences.h"
41 #include "notification_subscriber.h"
42 #include "notification_subscriber_manager.h"
43 #include "mock_push_callback_stub.h"
44 #include "system_event_observer.h"
45 #include "notification_constant.h"
46 #include "want_agent_info.h"
47 #include "want_agent_helper.h"
48 #include "want_params.h"
49 #include "bundle_manager_helper.h"
50
51 extern void MockIsOsAccountExists(bool mockRet);
52
53 using namespace testing::ext;
54 using namespace OHOS::Media;
55
56 namespace OHOS {
57 namespace Notification {
58 extern void MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum mockRet);
59 extern void MockIsSystemApp(bool isSystemApp);
60 extern void MockIsNonBundleName(bool isNonBundleName);
61 extern void MockIsVerfyPermisson(bool isVerify);
62
63 class AdvancedNotificationServiceTest : public testing::Test {
64 public:
65 static void SetUpTestCase();
66 static void TearDownTestCase();
67 void SetUp();
68 void TearDown();
69
70 private:
71 void TestAddSlot(NotificationConstant::SlotType type);
72 void TestAddLiveViewSlot(bool isForceControl);
73 void MockSystemApp();
74
75 private:
76 static sptr<AdvancedNotificationService> advancedNotificationService_;
77 };
78
79 sptr<AdvancedNotificationService> AdvancedNotificationServiceTest::advancedNotificationService_ = nullptr;
80
SetUpTestCase()81 void AdvancedNotificationServiceTest::SetUpTestCase()
82 {
83 MockIsOsAccountExists(true);
84 }
85
TearDownTestCase()86 void AdvancedNotificationServiceTest::TearDownTestCase() {}
87
SetUp()88 void AdvancedNotificationServiceTest::SetUp()
89 {
90 GTEST_LOG_(INFO) << "SetUp start";
91
92 advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService();
93 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
94 NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
95 IPCSkeleton::SetCallingUid(SYSTEM_APP_UID);
96 advancedNotificationService_->CancelAll(0);
97 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
98 GTEST_LOG_(INFO) << "SetUp end";
99 }
100
TearDown()101 void AdvancedNotificationServiceTest::TearDown()
102 {
103 IPCSkeleton::SetCallingUid(SYSTEM_APP_UID);
104 advancedNotificationService_ = nullptr;
105 GTEST_LOG_(INFO) << "TearDown";
106 }
107
SleepForFC()108 inline void SleepForFC()
109 {
110 // For ANS Flow Control
111 std::this_thread::sleep_for(std::chrono::seconds(1));
112 }
113
114 class TestAnsSubscriber : public NotificationSubscriber {
115 public:
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)116 void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
117 {}
OnConnected()118 void OnConnected() override
119 {}
OnDisconnected()120 void OnDisconnected() override
121 {}
OnDied()122 void OnDied() override
123 {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)124 void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
125 {}
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)126 void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override
127 {}
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)128 void OnBadgeEnabledChanged(
129 const sptr<EnabledNotificationCallbackData> &callbackData) override
130 {}
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)131 void OnEnabledNotificationChanged(
132 const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
133 {}
OnCanceled(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)134 void OnCanceled(const std::shared_ptr<Notification> &request,
135 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
136 {}
OnConsumed(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)137 void OnConsumed(const std::shared_ptr<Notification> &request,
138 const std::shared_ptr<NotificationSortingMap> &sortingMap) override
139 {}
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)140 void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>>
141 &requestList, const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
142 {}
143 };
144
TestAddSlot(NotificationConstant::SlotType type)145 void AdvancedNotificationServiceTest::TestAddSlot(NotificationConstant::SlotType type)
146 {
147 MockSystemApp();
148 std::vector<sptr<NotificationSlot>> slots;
149 sptr<NotificationSlot> slot = new NotificationSlot(type);
150 slots.push_back(slot);
151 ASSERT_EQ(advancedNotificationService_->AddSlots(slots), (int)ERR_OK);
152 }
153
MockSystemApp()154 void AdvancedNotificationServiceTest::MockSystemApp()
155 {
156 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
157 MockIsSystemApp(true);
158 MockIsVerfyPermisson(true);
159 }
160
TestAddLiveViewSlot(bool isForceControl)161 void AdvancedNotificationServiceTest::TestAddLiveViewSlot(bool isForceControl)
162 {
163 MockSystemApp();
164 std::vector<sptr<NotificationSlot>> slots;
165 sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::LIVE_VIEW);
166 slot->SetForceControl(isForceControl);
167 slots.push_back(slot);
168 ASSERT_EQ(advancedNotificationService_->AddSlots(slots), (int)ERR_OK);
169 }
170
MakePixelMap(int32_t width,int32_t height)171 inline std::shared_ptr<PixelMap> MakePixelMap(int32_t width, int32_t height)
172 {
173 const int32_t PIXEL_BYTES = 4;
174 std::shared_ptr<PixelMap> pixelMap = std::make_shared<PixelMap>();
175 if (pixelMap == nullptr) {
176 return nullptr;
177 }
178 ImageInfo info;
179 info.size.width = width;
180 info.size.height = height;
181 info.pixelFormat = PixelFormat::ARGB_8888;
182 info.colorSpace = ColorSpace::SRGB;
183 pixelMap->SetImageInfo(info);
184 int32_t rowDataSize = width * PIXEL_BYTES;
185 uint32_t bufferSize = rowDataSize * height;
186 void *buffer = malloc(bufferSize);
187 if (buffer != nullptr) {
188 pixelMap->SetPixelsAddr(buffer, nullptr, bufferSize, AllocatorType::HEAP_ALLOC, nullptr);
189 }
190 return pixelMap;
191 }
192
193 /**
194 * @tc.number : ANS_Publish_00100
195 * @tc.name : ANSPublish00100
196 * @tc.desc : Publish a normal text type notification.
197 */
198 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00100, Function | SmallTest | Level1)
199 {
200 TestAddSlot(NotificationConstant::SlotType::OTHER);
201 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
202 MockIsSystemApp(true);
203 sptr<NotificationRequest> req = new NotificationRequest(1);
204 EXPECT_NE(req, nullptr);
205 req->SetSlotType(NotificationConstant::SlotType::OTHER);
206 req->SetLabel("req's label");
207 req->SetCreatorUid(1);
208 std::string label = "publish's label";
209 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
210 EXPECT_NE(normalContent, nullptr);
211 normalContent->SetText("normalContent's text");
212 normalContent->SetTitle("normalContent's title");
213 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
214 EXPECT_NE(content, nullptr);
215 req->SetContent(content);
216 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
217 SleepForFC();
218 }
219
220 /**
221 * @tc.number : ANS_Publish_00200
222 * @tc.name : ANSPublish00200
223 * @tc.desc : Publish a normal text type notification twice.
224 */
225 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00200, Function | SmallTest | Level1)
226 {
227 TestAddSlot(NotificationConstant::SlotType::OTHER);
228 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
229 MockIsSystemApp(true);
230 sptr<NotificationRequest> req = new NotificationRequest(1);
231 EXPECT_NE(req, nullptr);
232 req->SetSlotType(NotificationConstant::SlotType::OTHER);
233 req->SetLabel("req's label");
234 req->SetCreatorUid(1);
235 std::string label = "publish's label";
236 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
237 EXPECT_NE(normalContent, nullptr);
238 normalContent->SetText("normalContent's text");
239 normalContent->SetTitle("normalContent's title");
240 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
241 EXPECT_NE(content, nullptr);
242 req->SetContent(content);
243 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
244 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
245 SleepForFC();
246 }
247
248 /**
249 * @tc.number : ANS_Publish_00300
250 * @tc.name : ANSPublish00300
251 * @tc.desc : When slotType is CUSTOM and not systemApp, the notification publish fails,
252 * and the notification publish interface returns ERR_ANS_NON_SYSTEM_APP.
253 */
254 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00300, Function | SmallTest | Level1)
255 {
256 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
257 MockIsSystemApp(false);
258 sptr<NotificationRequest> req = new NotificationRequest();
259 EXPECT_NE(req, nullptr);
260 req->SetSlotType(NotificationConstant::SlotType::CUSTOM);
261 req->SetLabel("req's label");
262 req->SetCreatorUid(1);
263 std::string label = "publish's label";
264 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
265 EXPECT_NE(normalContent, nullptr);
266 normalContent->SetText("normalContent's text");
267 normalContent->SetTitle("normalContent's title");
268 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
269 EXPECT_NE(content, nullptr);
270 req->SetContent(content);
271 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_NON_SYSTEM_APP);
272 SleepForFC();
273 }
274
275 /**
276 * @tc.number : ANS_Publish_00400
277 * @tc.name : ANSPublish00400
278 * @tc.desc : When the obtained bundleName is empty, the notification publish interface returns
279 * ERR_ANS_INVALID_BUNDLE.
280 */
281 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00400, Function | SmallTest | Level1)
282 {
283 MockIsNonBundleName(true);
284 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
285 MockIsSystemApp(true);
286 sptr<NotificationRequest> req = new NotificationRequest();
287 EXPECT_NE(req, nullptr);
288 req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
289 req->SetLabel("req's label");
290 req->SetCreatorUid(1);
291 std::string label = "publish's label";
292 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
293 EXPECT_NE(normalContent, nullptr);
294 normalContent->SetText("normalContent's text");
295 normalContent->SetTitle("normalContent's title");
296 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
297 EXPECT_NE(content, nullptr);
298 req->SetContent(content);
299 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_INVALID_BUNDLE);
300 MockIsNonBundleName(false);
301 SleepForFC();
302 }
303
304 /**
305 * @tc.number : ANS_Publish_00500
306 * @tc.name : ANSPublish00500
307 * @tc.desc : When the obtained bundleName does not have a corresponding slot in the database,
308 * create the corresponding slot and publish a notification.
309 */
310 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00500, Function | SmallTest | Level1)
311 {
312 TestAddSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
313 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
314 MockIsSystemApp(true);
315 sptr<NotificationRequest> req = new NotificationRequest();
316 EXPECT_NE(req, nullptr);
317 req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
318 req->SetLabel("req's label");
319 req->SetCreatorUid(1);
320 std::string label = "publish's label";
321 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
322 EXPECT_NE(normalContent, nullptr);
323 normalContent->SetText("normalContent's text");
324 normalContent->SetTitle("normalContent's title");
325 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
326 EXPECT_NE(content, nullptr);
327 req->SetContent(content);
328 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
329 SleepForFC();
330 }
331
332 /**
333 * @tc.number : ANS_Publish_00600
334 * @tc.name : ANSPublish00600
335 * @tc.desc : When the obtained bundleName have a corresponding slot in the database,
336 * the test publish interface can successfully publish a notification of normal text type.
337 */
338 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00600, Function | SmallTest | Level1)
339 {
340 TestAddSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
341 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
342 MockIsSystemApp(true);
343 sptr<NotificationRequest> req = new NotificationRequest();
344 EXPECT_NE(req, nullptr);
345 req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
346 req->SetLabel("req's label");
347 req->SetCreatorUid(1);
348 std::string label = "publish's label";
349 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
350 EXPECT_NE(normalContent, nullptr);
351 normalContent->SetText("normalContent's text");
352 normalContent->SetTitle("normalContent's title");
353 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
354 EXPECT_NE(content, nullptr);
355 req->SetContent(content);
356 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
357 SleepForFC();
358 }
359
360 /**
361 * @tc.number : ANS_Publish_00700
362 * @tc.name : ANSPublish00700
363 * @tc.desc : When the obtained bundleName have a corresponding slot in the database,
364 * create the corresponding slot and publish a notification.
365 */
366 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00700, Function | SmallTest | Level1)
367 {
368 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
369 MockIsSystemApp(true);
370 sptr<NotificationRequest> req = new NotificationRequest();
371 EXPECT_NE(req, nullptr);
372 req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
373 req->SetLabel("req's label");
374 req->SetCreatorUid(1);
375 std::string label = "publish's label";
376 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
377 EXPECT_NE(normalContent, nullptr);
378 normalContent->SetText("normalContent's text");
379 normalContent->SetTitle("normalContent's title");
380 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
381 EXPECT_NE(content, nullptr);
382 req->SetContent(content);
383 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
384 SleepForFC();
385 }
386
387 /**
388 * @tc.number : ANS_Publish_00800
389 * @tc.name : ANSPublish00800
390 * @tc.desc : Create a slot of type SOCIAL_COMMUNICATION and successfully publish a notification
391 */
392 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00800, Function | SmallTest | Level1)
393 {
394 TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
395 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
396 MockIsSystemApp(true);
397 sptr<NotificationRequest> req = new NotificationRequest();
398 EXPECT_NE(req, nullptr);
399 req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
400 req->SetLabel("req's label");
401 req->SetCreatorUid(1);
402 std::string label = "publish's label";
403 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
404 EXPECT_NE(normalContent, nullptr);
405 normalContent->SetText("normalContent's text");
406 normalContent->SetTitle("normalContent's title");
407 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
408 EXPECT_NE(content, nullptr);
409 req->SetContent(content);
410 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
411 SleepForFC();
412 }
413
414 /**
415 * @tc.number : ANS_Publish_00900
416 * @tc.name : ANSPublish00900
417 * @tc.desc : Create a slot of type SERVICE_REMINDER and successfully publish a notification
418 */
419 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00900, Function | SmallTest | Level1)
420 {
421 TestAddSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
422 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
423 MockIsSystemApp(true);
424 sptr<NotificationRequest> req = new NotificationRequest();
425 EXPECT_NE(req, nullptr);
426 req->SetSlotType(NotificationConstant::SlotType::SERVICE_REMINDER);
427 req->SetLabel("req's label");
428 req->SetCreatorUid(1);
429 std::string label = "publish's label";
430 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
431 EXPECT_NE(normalContent, nullptr);
432 normalContent->SetText("normalContent's text");
433 normalContent->SetTitle("normalContent's title");
434 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
435 EXPECT_NE(content, nullptr);
436 req->SetContent(content);
437 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
438 SleepForFC();
439 }
440
441 /**
442 * @tc.number : ANS_Publish_01000
443 * @tc.name : ANSPublish01000
444 * @tc.desc : Create a slot of type CONTENT_INFORMATION and successfully publish a notification
445 */
446 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01000, Function | SmallTest | Level1)
447 {
448 TestAddSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
449 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
450 MockIsSystemApp(true);
451 sptr<NotificationRequest> req = new NotificationRequest();
452 EXPECT_NE(req, nullptr);
453 req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
454 req->SetLabel("req's label");
455 req->SetCreatorUid(1);
456 std::string label = "publish's label";
457 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
458 EXPECT_NE(normalContent, nullptr);
459 normalContent->SetText("normalContent's text");
460 normalContent->SetTitle("normalContent's title");
461 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
462 EXPECT_NE(content, nullptr);
463 req->SetContent(content);
464 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
465 SleepForFC();
466 }
467
468 /**
469 * @tc.number : ANS_Publish_01100
470 * @tc.name : ANSPublish01100
471 * @tc.desc : Create a slot of type OTHER and successfully publish a notification
472 */
473 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01100, Function | SmallTest | Level1)
474 {
475 TestAddSlot(NotificationConstant::SlotType::OTHER);
476 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
477 MockIsSystemApp(true);
478 sptr<NotificationRequest> req = new NotificationRequest();
479 EXPECT_NE(req, nullptr);
480 req->SetSlotType(NotificationConstant::SlotType::OTHER);
481 req->SetLabel("req's label");
482 req->SetCreatorUid(1);
483 std::string label = "publish's label";
484 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
485 EXPECT_NE(normalContent, nullptr);
486 normalContent->SetText("normalContent's text");
487 normalContent->SetTitle("normalContent's title");
488 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
489 EXPECT_NE(content, nullptr);
490 req->SetContent(content);
491 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
492 SleepForFC();
493 }
494
495 /**
496 * @tc.number : ANS_Publish_01200
497 * @tc.name : ANSPublish01200
498 * @tc.desc : Create a slot of type CUSTOM and successfully publish a notification
499 */
500 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01200, Function | SmallTest | Level1)
501 {
502 TestAddSlot(NotificationConstant::SlotType::CUSTOM);
503 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
504 MockIsSystemApp(true);
505 sptr<NotificationRequest> req = new NotificationRequest();
506 EXPECT_NE(req, nullptr);
507 req->SetSlotType(NotificationConstant::SlotType::CUSTOM);
508 req->SetLabel("req's label");
509 req->SetCreatorUid(1);
510 std::string label = "publish's label";
511 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
512 EXPECT_NE(normalContent, nullptr);
513 normalContent->SetText("normalContent's text");
514 normalContent->SetTitle("normalContent's title");
515 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
516 EXPECT_NE(content, nullptr);
517 req->SetContent(content);
518 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
519 MockIsSystemApp(false);
520 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_NON_SYSTEM_APP);
521 SleepForFC();
522 }
523
524 /**
525 * @tc.number : ANS_Publish_01300
526 * @tc.name : ANSPublish01300
527 * @tc.desc : When a bundle is not allowed to publish a notification, the notification publishing interface
528 returns
529 * ERR_ANS_NOT_ALLOWED
530 */
531 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01300, Function | SmallTest | Level1)
532 {
533 TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
534 sptr<NotificationRequest> req = new NotificationRequest();
535 EXPECT_NE(req, nullptr);
536 req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
537 req->SetLabel("req's label");
538 req->SetCreatorUid(1);
539 std::string label = "publish's label";
540 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
541 EXPECT_NE(normalContent, nullptr);
542 normalContent->SetText("normalContent's text");
543 normalContent->SetTitle("normalContent's title");
544 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
545 EXPECT_NE(content, nullptr);
546 req->SetContent(content);
547 ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(
548 std::string(), new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), false),
549 (int)ERR_OK);
550 IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN);
551 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
552 MockIsSystemApp(true);
553 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_NOT_ALLOWED);
554 SleepForFC();
555 }
556
557
558 /**
559 * @tc.number : AdvancedNotificationServiceTest_04600
560 * @tc.name : ANS_Publish_0500
561 * @tc.desc : publish function when NotificationsEnabled is false
562 */
563 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04600, Function | SmallTest | Level1)
564 {
565 sptr<NotificationRequest> req = new NotificationRequest(1);
566 req->SetCreatorUid(1);
567 req->SetSlotType(NotificationConstant::OTHER);
568 TestAddSlot(NotificationConstant::SlotType::OTHER);
569 ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(
570 std::string(), new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), false),
571 (int)ERR_OK);
572 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
573 MockIsSystemApp(true);
574 ASSERT_EQ(advancedNotificationService_->Publish(std::string(), req), (int)ERR_ANS_NOT_ALLOWED);
575 }
576
577 /**
578 * @tc.number : AdvancedNotificationServiceTest_04700
579 * @tc.name : ANS_Cancel_0100
580 * @tc.desc : public two notification to cancel one of them
581 */
582 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04700, Function | SmallTest | Level1)
583 {
584 TestAddSlot(NotificationConstant::SlotType::OTHER);
585 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
586 MockIsSystemApp(true);
587 std::string label = "testLabel";
588 {
589 sptr<NotificationRequest> req = new NotificationRequest(1);
590 req->SetSlotType(NotificationConstant::OTHER);
591 req->SetLabel(label);
592 req->SetCreatorUid(1);
593 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
594 }
595 {
596 sptr<NotificationRequest> req = new NotificationRequest(2);
597 req->SetSlotType(NotificationConstant::OTHER);
598 req->SetLabel(label);
599 req->SetCreatorUid(1);
600 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
601 }
602 ASSERT_EQ(advancedNotificationService_->Cancel(1, label, 0), (int)ERR_OK);
603 }
604
605 /**
606 * @tc.number : AdvancedNotificationServiceTest_04800
607 * @tc.name : ANS_Cancel_0200
608 * @tc.desc : Test Cancel function when notification no exists
609 */
610 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04800, Function | SmallTest | Level1)
611 {
612 int32_t notificationId = 0;
613 std::string label = "testLabel";
614 ASSERT_EQ((int)advancedNotificationService_->Cancel(
615 notificationId, label, 0), (int)ERR_ANS_NOTIFICATION_NOT_EXISTS);
616 }
617
618 /**
619 * @tc.number : AdvancedNotificationServiceTest_04900
620 * @tc.name : ANS_CancelAll_0100
621 * @tc.desc : Test CancelAll function
622 */
623 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04900, Function | SmallTest | Level1)
624 {
625 TestAddSlot(NotificationConstant::SlotType::OTHER);
626 sptr<NotificationRequest> req = new NotificationRequest(1);
627 req->SetSlotType(NotificationConstant::OTHER);
628 ASSERT_EQ(advancedNotificationService_->CancelAll(0), (int)ERR_OK);
629 }
630
631 /**
632 * @tc.number : AdvancedNotificationServiceTest_05000
633 * @tc.name : ANS_Cancel_0100
634 * @tc.desc : Test Cancel function when unremovable is true
635 */
636 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_05000, Function | SmallTest | Level1)
637 {
638 TestAddSlot(NotificationConstant::SlotType::OTHER);
639 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
640 MockIsSystemApp(true);
641 int32_t notificationId = 2;
642 std::string label = "testLabel";
643 sptr<NotificationRequest> req = new NotificationRequest(notificationId);
644 req->SetSlotType(NotificationConstant::OTHER);
645 req->SetLabel(label);
646 req->SetUnremovable(true);
647 req->SetCreatorUid(1);
648 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
649 ASSERT_EQ(advancedNotificationService_->Cancel(notificationId, label, 0), (int)ERR_OK);
650 }
651
652
653 /**
654 * @tc.number : AdvancedNotificationServiceTest_10000
655 * @tc.name : ANS_Publish_With_PixelMap
656 * @tc.desc : Publish a notification with pixelMap.
657 */
658 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10000, Function | SmallTest | Level1)
659 {
660 const int BIG_PICTURE_WIDTH = 400;
661 const int BIG_PICTURE_HEIGHT = 300;
662 const int ICON_SIZE = 36;
663
664 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
665 MockIsSystemApp(true);
666 sptr<NotificationRequest> req = new NotificationRequest(1);
667 EXPECT_NE(req, nullptr);
668 req->SetSlotType(NotificationConstant::SlotType::OTHER);
669 req->SetLabel("label");
670 std::shared_ptr<NotificationPictureContent> pictureContent = std::make_shared<NotificationPictureContent>();
671 EXPECT_NE(pictureContent, nullptr);
672 pictureContent->SetText("notification text");
673 pictureContent->SetTitle("notification title");
674 std::shared_ptr<PixelMap> bigPicture = MakePixelMap(BIG_PICTURE_WIDTH, BIG_PICTURE_HEIGHT);
675 EXPECT_NE(bigPicture, nullptr);
676 pictureContent->SetBigPicture(bigPicture);
677 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(pictureContent);
678 EXPECT_NE(content, nullptr);
679 req->SetContent(content);
680 std::shared_ptr<PixelMap> littleIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
681 req->SetLittleIcon(littleIcon);
682 std::shared_ptr<PixelMap> bigIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
683 req->SetBigIcon(bigIcon);
684 ASSERT_EQ(advancedNotificationService_->Publish("label", req), (int)ERR_OK);
685 }
686
687 /**
688 * @tc.number : AdvancedNotificationServiceTest_10100
689 * @tc.name : ANS_Publish_With_PixelMap_Oversize_00100
690 * @tc.desc : Publish a notification with oversize pixelMap.
691 */
692 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10100, Function | SmallTest | Level1)
693 {
694 const int BIG_PICTURE_WIDTH = 1024;
695 const int BIG_PICTURE_HEIGHT = 1024;
696 const int ICON_SIZE = 36;
697
698 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
699 MockIsSystemApp(true);
700 sptr<NotificationRequest> req = new NotificationRequest(1);
701 EXPECT_NE(req, nullptr);
702 req->SetSlotType(NotificationConstant::SlotType::OTHER);
703 req->SetLabel("label");
704 std::shared_ptr<NotificationPictureContent> pictureContent = std::make_shared<NotificationPictureContent>();
705 EXPECT_NE(pictureContent, nullptr);
706 pictureContent->SetText("notification text");
707 pictureContent->SetTitle("notification title");
708 std::shared_ptr<PixelMap> bigPicture = MakePixelMap(BIG_PICTURE_WIDTH, BIG_PICTURE_HEIGHT);
709 EXPECT_NE(bigPicture, nullptr);
710 pictureContent->SetBigPicture(bigPicture);
711 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(pictureContent);
712 EXPECT_NE(content, nullptr);
713 req->SetContent(content);
714 std::shared_ptr<PixelMap> littleIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
715 req->SetLittleIcon(littleIcon);
716 std::shared_ptr<PixelMap> bigIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
717 req->SetBigIcon(bigIcon);
718 ASSERT_EQ(advancedNotificationService_->Publish("label", req), (int)ERR_ANS_PICTURE_OVER_SIZE);
719 }
720
721 /**
722 * @tc.number : AdvancedNotificationServiceTest_10200
723 * @tc.name : ANS_Publish_With_PixelMap_Oversize_00200
724 * @tc.desc : Publish a notification with oversize pixelMap.
725 */
726 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10200, Function | SmallTest | Level1)
727 {
728 const int BIG_PICTURE_WIDTH = 400;
729 const int BIG_PICTURE_HEIGHT = 300;
730 const int ICON_SIZE = 256;
731
732 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
733 MockIsSystemApp(true);
734 sptr<NotificationRequest> req = new NotificationRequest(1);
735 EXPECT_NE(req, nullptr);
736 req->SetSlotType(NotificationConstant::SlotType::OTHER);
737 req->SetLabel("label");
738 std::shared_ptr<NotificationPictureContent> pictureContent = std::make_shared<NotificationPictureContent>();
739 EXPECT_NE(pictureContent, nullptr);
740 pictureContent->SetText("notification text");
741 pictureContent->SetTitle("notification title");
742 std::shared_ptr<PixelMap> bigPicture = MakePixelMap(BIG_PICTURE_WIDTH, BIG_PICTURE_HEIGHT);
743 EXPECT_NE(bigPicture, nullptr);
744 pictureContent->SetBigPicture(bigPicture);
745 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(pictureContent);
746 EXPECT_NE(content, nullptr);
747 req->SetContent(content);
748 std::shared_ptr<PixelMap> littleIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
749 req->SetLittleIcon(littleIcon);
750 std::shared_ptr<PixelMap> bigIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
751 req->SetBigIcon(bigIcon);
752 ASSERT_EQ(advancedNotificationService_->Publish("label", req), (int)ERR_ANS_ICON_OVER_SIZE);
753 }
754
755 /**
756 * @tc.number : AdvancedNotificationServiceTest_10300
757 * @tc.name : ANS_Cancel_By_Group_10300
758 * @tc.desc : Cancel notification by group name.
759 */
760 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10300, Function | SmallTest | Level1)
761 {
762 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
763 MockIsSystemApp(true);
764 sptr<NotificationRequest> req = new NotificationRequest(1);
765 ASSERT_NE(req, nullptr);
766 req->SetSlotType(NotificationConstant::SlotType::OTHER);
767 req->SetLabel("label");
768 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
769 ASSERT_NE(normalContent, nullptr);
770 normalContent->SetText("text");
771 normalContent->SetTitle("title");
772 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
773 ASSERT_NE(content, nullptr);
774 req->SetContent(content);
775 std::string groupName = "group";
776 req->SetGroupName(groupName);
777 ASSERT_EQ(advancedNotificationService_->Publish("label", req), (int)ERR_OK);
778 ASSERT_EQ(advancedNotificationService_->CancelGroup(groupName, 0), (int)ERR_OK);
779 SleepForFC();
780 }
781
782 /**
783 * @tc.number : AdvancedNotificationServiceTest_10400
784 * @tc.name : ANS_Remove_By_Group_10400
785 * @tc.desc : Remove notification by group name.
786 */
787 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10400, Function | SmallTest | Level1)
788 {
789 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
790 MockIsSystemApp(true);
791 sptr<NotificationRequest> req = new NotificationRequest(1);
792 ASSERT_NE(req, nullptr);
793 req->SetSlotType(NotificationConstant::SlotType::OTHER);
794 req->SetLabel("label");
795 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
796 ASSERT_NE(normalContent, nullptr);
797 normalContent->SetText("text");
798 normalContent->SetTitle("title");
799 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
800 ASSERT_NE(content, nullptr);
801 req->SetContent(content);
802 std::string groupName = "group";
803 req->SetGroupName(groupName);
804 ASSERT_EQ(advancedNotificationService_->Publish("label", req), (int)ERR_OK);
805
806 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
807 ASSERT_EQ(advancedNotificationService_->RemoveGroupByBundle(bundleOption, groupName), (int)ERR_OK);
808 SleepForFC();
809 }
810
811
812 /**
813 * @tc.name: AdvancedNotificationServiceTest_12000
814 * @tc.desc: Send enable notification hisysevent and enable notification error hisysevent.
815 * @tc.type: FUNC
816 * @tc.require: I582Y4
817 */
818 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12000, Function | SmallTest | Level1)
819 {
820 // bundleName is empty
821 ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(
822 std::string(), new NotificationBundleOption(std::string(), -1), true),
823 (int)ERR_ANS_INVALID_PARAM);
824
825 TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
826 sptr<NotificationRequest> req = new NotificationRequest();
827 EXPECT_NE(req, nullptr);
828 req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
829 req->SetLabel("req's label");
830 std::string label = "enable's label";
831 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
832 EXPECT_NE(normalContent, nullptr);
833 normalContent->SetText("normalContent's text");
834 normalContent->SetTitle("normalContent's title");
835 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
836 EXPECT_NE(content, nullptr);
837 req->SetContent(content);
838 ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(
839 std::string(), new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), false),
840 (int)ERR_OK);
841
842 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
843 MockIsSystemApp(true);
844 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_NOT_ALLOWED);
845 SleepForFC();
846 }
847
848 /**
849 * @tc.name: AdvancedNotificationServiceTest_12100
850 * @tc.desc: Send enable notification slot hisysevent.
851 * @tc.type: FUNC
852 * @tc.require: I582Y4
853 */
854 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12100, Function | SmallTest | Level1)
855 {
856 TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
857 sptr<NotificationRequest> req = new NotificationRequest();
858 EXPECT_NE(req, nullptr);
859 req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
860 req->SetLabel("req's label");
861 std::string label = "enable's label";
862 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
863 EXPECT_NE(normalContent, nullptr);
864 normalContent->SetText("normalContent's text");
865 normalContent->SetTitle("normalContent's title");
866 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
867 EXPECT_NE(content, nullptr);
868 req->SetContent(content);
869 auto result = advancedNotificationService_->SetEnabledForBundleSlot(
870 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID),
871 NotificationConstant::SlotType::SOCIAL_COMMUNICATION,
872 false, false);
873 ASSERT_EQ(result, (int)ERR_OK);
874
875 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
876 MockIsSystemApp(true);
877 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED);
878 SleepForFC();
879 }
880
881 /**
882 * @tc.name: AdvancedNotificationServiceTest_12200
883 * @tc.desc: Send remove notification hisysevent.
884 * @tc.type: FUNC
885 * @tc.require: I582Y4
886 */
887 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12200, Function | SmallTest | Level1)
888 {
889 TestAddSlot(NotificationConstant::SlotType::OTHER);
890 int32_t notificationId = 1;
891 std::string label = "testRemove";
892 sptr<NotificationRequest> req = new NotificationRequest(notificationId);
893 req->SetSlotType(NotificationConstant::OTHER);
894 req->SetLabel(label);
895 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
896 MockIsSystemApp(true);
897 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
898
899 auto result = advancedNotificationService_->RemoveNotification(
900 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID),
901 notificationId, label, NotificationConstant::CANCEL_REASON_DELETE);
902 ASSERT_EQ(result, (int)ERR_OK);
903 }
904
905 /**
906 * @tc.name: AdvancedNotificationServiceTest_12300
907 * @tc.desc: SA publish notification, Failed to publish when creatorUid default.
908 * @tc.type: FUNC
909 * @tc.require: I5P1GU
910 */
911 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12300, Function | SmallTest | Level1)
912 {
913 TestAddSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
914 sptr<NotificationRequest> req = new NotificationRequest();
915 EXPECT_NE(req, nullptr);
916 req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
917 req->SetLabel("req's label");
918 std::string label = "publish's label";
919 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
920 EXPECT_NE(normalContent, nullptr);
921 normalContent->SetText("normalContent's text");
922 normalContent->SetTitle("normalContent's title");
923 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
924 EXPECT_NE(content, nullptr);
925 req->SetContent(content);
926
927 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
928 ASSERT_EQ(advancedNotificationService_->Publish(label, req), ERR_ANS_INVALID_UID);
929 SleepForFC();
930
931 req->SetCreatorUid(1);
932 ASSERT_EQ(advancedNotificationService_->Publish(label, req), 0);
933 }
934
935 /*
936 * @tc.name: AdvancedNotificationServiceTest_12400
937 * @tc.desc: DLP App publish notification failed.
938 * @tc.type: FUNC
939 * @tc.require: I582TY
940 */
941 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12400, Function | SmallTest | Level1)
942 {
943 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
944 MockIsSystemApp(true);
945 sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest(1);
946 EXPECT_NE(req, nullptr);
947 req->SetSlotType(NotificationConstant::SlotType::OTHER);
948 req->SetLabel("req's label");
949 std::string label = "publish's label";
950 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
951 EXPECT_NE(normalContent, nullptr);
952 normalContent->SetText("normalContent's text");
953 normalContent->SetTitle("normalContent's title");
954 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
955 EXPECT_NE(content, nullptr);
956 req->SetContent(content);
957 EXPECT_NE(advancedNotificationService_->Publish(label, req), ERR_ANS_DLP_HAP);
958 SleepForFC();
959
960 ASSERT_EQ(advancedNotificationService_->Publish(label, req), ERR_OK);
961 }
962
963 /*
964 * @tc.name: AdvancedNotificationServiceTest_12500
965 * @tc.desc: When the user removed event is received and the userid is less than or equal to 100,
966 * the notification cannot be deleted
967 * @tc.type: FUNC
968 */
969 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12500, Function | SmallTest | Level1)
970 {
971 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
972 MockIsSystemApp(true);
973 sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest(1);
974 EXPECT_NE(req, nullptr);
975 req->SetSlotType(NotificationConstant::SlotType::OTHER);
976 req->SetLabel("req's label");
977 std::string label = "publish's label";
978 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
979 EXPECT_NE(normalContent, nullptr);
980 normalContent->SetText("normalContent's text");
981 normalContent->SetTitle("normalContent's title");
982 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
983 EXPECT_NE(content, nullptr);
984 req->SetContent(content);
985 req->SetCreatorUserId(DEFAULT_USER_ID);
986 ASSERT_EQ(advancedNotificationService_->Publish(label, req), ERR_OK);
987 SleepForFC();
988
989 EventFwk::Want want;
990 EventFwk::CommonEventData data;
991 data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED));
992 data.SetCode(DEFAULT_USER_ID);
993 advancedNotificationService_->systemEventObserver_->OnReceiveEvent(data);
994
995 ASSERT_EQ(advancedNotificationService_->IsNotificationExists(req->GetBaseKey("")), true);
996 }
997
998
999 /**
1000 * @tc.number : AdvancedNotificationServiceTest_15500
1001 * @tc.name : OnReceiveEvent_0100
1002 * @tc.desc : Test OnReceiveEvent function userid<DEFAULT_USER_ID
1003 * @tc.require : I5TIQR
1004 */
1005 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15500, Function | SmallTest | Level1)
1006 {
1007 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1008 MockIsSystemApp(true);
1009 sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest(1);
1010 EXPECT_NE(req, nullptr);
1011 req->SetSlotType(NotificationConstant::SlotType::OTHER);
1012 req->SetLabel("req's label");
1013 std::string label = "publish's label";
1014 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
1015 EXPECT_NE(normalContent, nullptr);
1016 normalContent->SetText("normalContent's text");
1017 normalContent->SetTitle("normalContent's title");
1018 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
1019 EXPECT_NE(content, nullptr);
1020 req->SetContent(content);
1021 req->SetCreatorUserId(DEFAULT_USER_ID);
1022 ASSERT_EQ(advancedNotificationService_->Publish(label, req), ERR_OK);
1023 SleepForFC();
1024
1025 EventFwk::Want want;
1026 EventFwk::CommonEventData data;
1027 data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED));
1028 data.SetCode(50);
1029 advancedNotificationService_->systemEventObserver_->OnReceiveEvent(data);
1030
1031 ASSERT_EQ(advancedNotificationService_->IsNotificationExists(req->GetBaseKey("")), true);
1032 }
1033
1034 /**
1035 * @tc.number : AdvancedNotificationServiceTest_15600
1036 * @tc.name : OnReceiveEvent_0200
1037 * @tc.desc : Test OnReceiveEvent function when userid>DEFAULT_USER_ID
1038 * @tc.require : I5TIQR
1039 */
1040 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15600, Function | SmallTest | Level1)
1041 {
1042 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1043 MockIsSystemApp(true);
1044 sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest(1);
1045 EXPECT_NE(req, nullptr);
1046 req->SetSlotType(NotificationConstant::SlotType::OTHER);
1047 req->SetLabel("req's label");
1048 std::string label = "publish's label";
1049 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
1050 EXPECT_NE(normalContent, nullptr);
1051 normalContent->SetText("normalContent's text");
1052 normalContent->SetTitle("normalContent's title");
1053 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
1054 EXPECT_NE(content, nullptr);
1055 req->SetContent(content);
1056 req->SetCreatorUserId(DEFAULT_USER_ID);
1057 ASSERT_EQ(advancedNotificationService_->Publish(label, req), ERR_OK);
1058 SleepForFC();
1059
1060 EventFwk::Want want;
1061 EventFwk::CommonEventData data;
1062 data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED));
1063 data.SetCode(200);
1064 advancedNotificationService_->systemEventObserver_->OnReceiveEvent(data);
1065
1066 ASSERT_EQ(advancedNotificationService_->IsNotificationExists(req->GetBaseKey("")), true);
1067 }
1068
1069
1070 /**
1071 * @tc.number : AdvancedNotificationServiceTest_16100
1072 * @tc.name : PrepareNotificationInfo_1000
1073 * @tc.desc : Test PrepareNotificationInfo function.
1074 * @tc.require : #I60KYN
1075 */
1076 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16100, Function | SmallTest | Level1)
1077 {
1078 GTEST_LOG_(INFO) << "CancelPreparedNotification_1000 test start";
1079
1080 sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest(1);
1081 EXPECT_NE(req, nullptr);
1082 req->SetSlotType(NotificationConstant::SlotType::OTHER);
1083 req->SetLabel("req's label");
1084 std::string label = "publish's label";
1085 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
1086 EXPECT_NE(normalContent, nullptr);
1087 normalContent->SetText("normalContent's text");
1088 normalContent->SetTitle("normalContent's title");
1089 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
1090 EXPECT_NE(content, nullptr);
1091 req->SetContent(content);
1092 req->SetCreatorUserId(DEFAULT_USER_ID);
1093 req->SetIsAgentNotification(true);
1094 advancedNotificationService_->Publish(label, req);
1095 SleepForFC();
1096
1097 GTEST_LOG_(INFO) << "CancelPreparedNotification_1000 test end";
1098 }
1099
1100
1101 /**
1102 * @tc.number : AdvancedNotificationServiceTest_17200
1103 * @tc.name : ANS_DeleteAll_0100
1104 * @tc.desc : Test DeleteAll function
1105 * @tc.require : #I60KYN
1106 */
1107 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17200, Function | SmallTest | Level1)
1108 {
1109 GTEST_LOG_(INFO) << "ANS_GetActiveNotifications_0100 test start";
1110
1111 TestAddSlot(NotificationConstant::SlotType::OTHER);
1112 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1113 MockIsSystemApp(true);
1114 sptr<NotificationRequest> req = new NotificationRequest(1);
1115 EXPECT_NE(req, nullptr);
1116 req->SetSlotType(NotificationConstant::SlotType::OTHER);
1117 req->SetLabel("req's label");
1118 std::string label = "publish's label";
1119 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
1120 EXPECT_NE(normalContent, nullptr);
1121 normalContent->SetText("normalContent's text");
1122 normalContent->SetTitle("normalContent's title");
1123 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
1124 EXPECT_NE(content, nullptr);
1125 req->SetContent(content);
1126 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
1127 SleepForFC();
1128 req->SetCreatorUserId(SUBSCRIBE_USER_INIT);
1129 std::shared_ptr<Notification> notification = std::make_shared<Notification>(req);
1130
1131 ASSERT_EQ(advancedNotificationService_->DeleteAll(), ERR_OK);
1132
1133 GTEST_LOG_(INFO) << "ANS_GetActiveNotifications_0100 test end";
1134 }
1135
1136 /**
1137 * @tc.number : AdvancedNotificationServiceTest_04100
1138 * @tc.name : ANS_GetSpecialActiveNotifications_0100
1139 * @tc.desc : Test GetSpecialActiveNotifications function
1140 */
1141 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04100, Function | SmallTest | Level1)
1142 {
1143 TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1144 sptr<NotificationRequest> req = new NotificationRequest();
1145 EXPECT_NE(req, nullptr);
1146 req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1147 req->SetLabel("req's label");
1148 req->SetCreatorUid(1);
1149 req->SetAlertOneTime(true);
1150 std::string label = "publish's label";
1151 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
1152 EXPECT_NE(normalContent, nullptr);
1153 normalContent->SetText("normalContent's text");
1154 normalContent->SetTitle("normalContent's title");
1155 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
1156 EXPECT_NE(content, nullptr);
1157 req->SetContent(content);
1158 ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK);
1159
1160 std::vector<sptr<Notification>> allNotifications;
1161 ASSERT_EQ(advancedNotificationService_->GetAllActiveNotifications(allNotifications), (int)ERR_OK);
1162 ASSERT_EQ(allNotifications.size(), (size_t)1);
1163 std::vector<std::string> keys;
1164 for (auto notification : allNotifications) {
1165 keys.push_back(notification->GetKey());
1166 }
1167 std::vector<sptr<Notification>> specialActiveNotifications;
1168 ASSERT_EQ(
1169 advancedNotificationService_->GetSpecialActiveNotifications(keys, specialActiveNotifications), (int)ERR_OK);
1170 ASSERT_EQ(specialActiveNotifications.size(), (size_t)1);
1171 SleepForFC();
1172 }
1173
1174 /**
1175 * @tc.number : ANS_Publish_01500
1176 * @tc.name : ANSPublish01500
1177 * @tc.desc : publish a continuous task notification
1178 */
1179 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11000, Function | SmallTest | Level1)
1180 {
1181 sptr<NotificationRequest> req = new NotificationRequest();
1182 EXPECT_NE(req, nullptr);
1183 req->SetSlotType(NotificationConstant::SlotType::OTHER);
1184 req->SetLabel("req's label");
1185 ASSERT_EQ(advancedNotificationService_->PublishContinuousTaskNotification(req), (int)ERR_OK);
1186 SleepForFC();
1187 }
1188
1189 /**
1190 * @tc.number : ANS_Publish_01600
1191 * @tc.name : ANSPublish01600
1192 * @tc.desc : publish a continuous task notification
1193 */
1194 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11100, Function | SmallTest | Level1)
1195 {
1196 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1197 MockIsSystemApp(true);
1198 sptr<NotificationRequest> req = new NotificationRequest();
1199 EXPECT_NE(req, nullptr);
1200 req->SetSlotType(NotificationConstant::SlotType::OTHER);
1201 req->SetLabel("req's label");
1202 ASSERT_EQ(advancedNotificationService_->PublishContinuousTaskNotification(req), (int)ERR_ANS_NOT_SYSTEM_SERVICE);
1203 SleepForFC();
1204 }
1205
1206 /**
1207 * @tc.number : AdvancedNotificationServiceTest_11200
1208 * @tc.name : ANS_Cancel_0300
1209 * @tc.desc : public two notification to cancel one of them
1210 */
1211 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11200, Function | SmallTest | Level1)
1212 {
1213 std::string label = "testLabel";
1214 {
1215 sptr<NotificationRequest> req = new NotificationRequest(1);
1216 req->SetSlotType(NotificationConstant::OTHER);
1217 req->SetLabel(label);
1218 ASSERT_EQ(advancedNotificationService_->PublishContinuousTaskNotification(req), (int)ERR_OK);
1219 }
1220 ASSERT_EQ(advancedNotificationService_->CancelContinuousTaskNotification(label, 1), (int)ERR_OK);
1221 }
1222
1223 /**
1224 * @tc.number : AdvancedNotificationServiceTest_11300
1225 * @tc.name : ANS_Cancel_0400
1226 * @tc.desc : public two notification to cancel one of them
1227 */
1228 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11300, Function | SmallTest | Level1)
1229 {
1230 std::string label = "testLabel";
1231 {
1232 sptr<NotificationRequest> req = new NotificationRequest(1);
1233 req->SetSlotType(NotificationConstant::OTHER);
1234 req->SetLabel(label);
1235 ASSERT_EQ(advancedNotificationService_->PublishContinuousTaskNotification(req), (int)ERR_OK);
1236 }
1237 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
1238 MockIsSystemApp(true);
1239 ASSERT_EQ(
1240 advancedNotificationService_->CancelContinuousTaskNotification(label, 1), (int)ERR_ANS_NOT_SYSTEM_SERVICE);
1241 }
1242
1243 /**
1244 * @tc.number : AdvancedNotificationServiceTest_12600
1245 * @tc.name : ANS_CancelAsBundle_0100
1246 * @tc.desc : Test CancelAsBundle function when the result is ERR_ANS_NOTIFICATION_NOT_EXISTS
1247 * @tc.require : issueI5S4VP
1248 */
1249 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12600, Function | SmallTest | Level1)
1250 {
1251 TestAddSlot(NotificationConstant::SlotType::OTHER);
1252 int32_t notificationId = 1;
1253 std::string representativeBundle = "RepresentativeBundle";
1254 int32_t userId = 1;
1255 int result = ERR_ANS_NOTIFICATION_NOT_EXISTS;
1256 ASSERT_EQ(advancedNotificationService_->CancelAsBundle(notificationId, representativeBundle, userId), result);
1257 }
1258
1259 /**
1260 * @tc.number : AdvancedNotificationServiceTest_12700
1261 * @tc.name : ANS_CanPublishAsBundle_0100
1262 * @tc.desc : Test CanPublishAsBundle function when the result is ERR_INVALID_OPERATION
1263 * @tc.require : issueI5S4VP
1264 */
1265 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12700, Function | SmallTest | Level1)
1266 {
1267 std::string representativeBundle = "RepresentativeBundle";
1268 bool canPublish = true;
1269 int result = ERR_INVALID_OPERATION;
1270 ASSERT_EQ(advancedNotificationService_->CanPublishAsBundle(representativeBundle, canPublish), result);
1271 }
1272
1273 /**
1274 * @tc.number : AdvancedNotificationServiceTest_12800
1275 * @tc.name : ANS_PublishAsBundle_0100
1276 * @tc.desc : Test PublishAsBundle function when the result is ERR_INVALID_OPERATION
1277 * @tc.require : issueI5S4VP
1278 */
1279 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12800, Function | SmallTest | Level1)
1280 {
1281 sptr<NotificationRequest> notification = nullptr;
1282 std::string representativeBundle = "RepresentativeBundle";
1283 int result = ERR_INVALID_OPERATION;
1284 ASSERT_EQ(advancedNotificationService_->PublishAsBundle(notification, representativeBundle), result);
1285 }
1286
1287
1288 /**
1289 * @tc.number : AdvancedNotificationServiceTest_13200
1290 * @tc.name : ANS_PublishReminder_0100
1291 * @tc.desc : Test PublishReminder function
1292 * @tc.require : issueI5S4VP
1293 */
1294 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_13200, Function | SmallTest | Level1)
1295 {
1296 sptr<ReminderRequest> reminder = nullptr;
1297 ASSERT_EQ(advancedNotificationService_->PublishReminder(reminder), ERR_ANS_INVALID_PARAM);
1298 }
1299
1300 /**
1301 * @tc.number : AdvancedNotificationServiceTest_13300
1302 * @tc.name : ANS_CancelReminder_0100
1303 * @tc.desc : Test CancelReminder function when the result is ERR_NO_INIT
1304 * @tc.require : issueI5S4VP
1305 */
1306 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_13300, Function | SmallTest | Level1)
1307 {
1308 TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1309 sptr<NotificationRequest> req = new NotificationRequest();
1310 EXPECT_NE(req, nullptr);
1311 int32_t reminderId = 1;
1312 ASSERT_EQ(advancedNotificationService_->CancelReminder(reminderId), (int)ERR_NO_INIT);
1313 }
1314
1315 /**
1316 * @tc.number : AdvancedNotificationServiceTest_13400
1317 * @tc.name : ANS_CancelAllReminders_0100
1318 * @tc.desc : Test CancelAllReminders function when the result is ERR_NO_INIT
1319 * @tc.require : issueI5S4VP
1320 */
1321 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_13400, Function | SmallTest | Level1)
1322 {
1323 TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1324 sptr<NotificationRequest> req = new NotificationRequest();
1325 EXPECT_NE(req, nullptr);
1326 ASSERT_EQ(advancedNotificationService_->CancelAllReminders(), (int)ERR_NO_INIT);
1327 }
1328
1329 /**
1330 * @tc.number : AdvancedNotificationServiceTest_21500
1331 * @tc.name : PublishPreparedNotification_1000
1332 * @tc.desc : Test PublishPreparedNotification function.
1333 * @tc.require : issueI62D8C
1334 */
1335 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21500, Function | SmallTest | Level1)
1336 {
1337 GTEST_LOG_(INFO) << "PublishPreparedNotification_1000 test start";
1338
1339 sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest();
1340 sptr<Notification> notification = new (std::nothrow) Notification(req);
1341 EXPECT_NE(notification, nullptr);
1342 sptr<NotificationBundleOption> bundleOption = nullptr;
1343
1344 ASSERT_EQ(advancedNotificationService_->PublishPreparedNotification(req, bundleOption), ERR_ANS_INVALID_PARAM);
1345
1346 GTEST_LOG_(INFO) << "PublishPreparedNotification_1000 test end";
1347 }
1348
1349 /**
1350 * @tc.number : AdvancedNotificationServiceTest_17900
1351 * @tc.name : PublishReminder_1000
1352 * @tc.desc : Test PublishReminder function.
1353 * @tc.require : #I61RF2
1354 */
1355 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17900, Function | SmallTest | Level1)
1356 {
1357 GTEST_LOG_(INFO) << "GetAppTargetBundle_1000 test start";
1358
1359 int32_t reminderId = 1;
1360 sptr<ReminderRequest> reminder = new ReminderRequest(reminderId);
1361 reminder->InitNotificationRequest();
1362 ASSERT_EQ(advancedNotificationService_->PublishReminder(reminder), ERR_REMINDER_NOTIFICATION_NOT_ENABLE);
1363
1364 GTEST_LOG_(INFO) << "GetAppTargetBundle_1000 test end";
1365 }
1366
1367 /**
1368 * @tc.number : AdvancedNotificationServiceTest_18000
1369 * @tc.name : PublishReminder_2000
1370 * @tc.desc : Test PublishReminder function.
1371 * @tc.require : #I61RF2
1372 */
1373 HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_18000, Function | SmallTest | Level1)
1374 {
1375 GTEST_LOG_(INFO) << "GetAppTargetBundle_2000 test start";
1376
1377 MockIsNonBundleName(true);
1378 int32_t reminderId = 1;
1379 sptr<ReminderRequest> reminder = new ReminderRequest(reminderId);
1380 reminder->InitNotificationRequest();
1381 ASSERT_EQ(advancedNotificationService_->PublishReminder(reminder), ERR_ANS_INVALID_BUNDLE);
1382 MockIsNonBundleName(false);
1383 GTEST_LOG_(INFO) << "GetAppTargetBundle_2000 test end";
1384 }
1385
1386 } // namespace Notification
1387 } // namespace OHOS
1388