1 /*
2  * Copyright (c) 2021 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 <functional>
17 #include <gtest/gtest.h>
18 
19 #include "ans_inner_errors.h"
20 #include "ans_ut_constant.h"
21 #include "notification_preferences.h"
22 #include "notification_slot.h"
23 #include "permission_filter.h"
24 #include "string_ex.h"
25 
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace Notification {
29 class PermissionFilterTest : public testing::Test {
30 public:
SetUpTestCase()31     static void SetUpTestCase() {};
TearDownTestCase()32     static void TearDownTestCase() {};
33     void SetUp();
34     void TearDown();
35 };
36 
SetUp()37 void PermissionFilterTest::SetUp()
38 {
39     NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
40 }
41 
TearDown()42 void PermissionFilterTest::TearDown()
43 {
44     NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
45 }
46 
47 /**
48  * @tc.number    : PermissionFilterTest_00100
49  * @tc.name      :
50  * @tc.desc      : Test OnPublish function.Expect return ERR_OK
51  */
52 HWTEST_F(PermissionFilterTest, PermissionFilterTest_00100, Function | SmallTest | Level1)
53 {
54     // Test OnStart function.
55     PermissionFilter permissionFilter;
56     permissionFilter.OnStart();
57 
58     // Test OnPublish function.
59     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::OTHER);
60     std::vector<sptr<NotificationSlot>> slots;
61     slots.push_back(slot);
62     NotificationPreferences::GetInstance()->AddNotificationSlots(
63         new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), slots);
64     std::shared_ptr<NotificationRecord> record = std::make_shared<NotificationRecord>();
65     record->bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
66     record->request = new NotificationRequest();
67     record->request->SetOwnerBundleName(TEST_DEFUALT_BUNDLE);
68     record->notification = new Notification(record->request);
69     record->slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
70     // ASSERT_EQ(permissionFilter.OnPublish(record), ERR_OK);
71 }
72 
73 /**
74  * @tc.number    : PermissionFilterTest_00200
75  * @tc.name      :
76  * @tc.desc      : Test OnPublish function when NotificationsEnabled is false.Expect return ERR_ANS_NOT_ALLOWED
77  */
78 HWTEST_F(PermissionFilterTest, PermissionFilterTest_00200, Function | SmallTest | Level1)
79 {
80     // Test OnStop function.
81     PermissionFilter permissionFilter;
82     permissionFilter.OnStop();
83 
84     // Test OnPublish function.
85     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::OTHER);
86     std::vector<sptr<NotificationSlot>> slots;
87     slots.push_back(slot);
88     NotificationPreferences::GetInstance()->AddNotificationSlots(
89         new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), slots);
90     NotificationPreferences::GetInstance()->SetNotificationsEnabledForBundle(
91         new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), false);
92     std::shared_ptr<NotificationRecord> record = std::make_shared<NotificationRecord>();
93     record->bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
94     record->request = new NotificationRequest();
95     record->request->SetOwnerBundleName(TEST_DEFUALT_BUNDLE);
96     record->notification = new Notification(record->request);
97     record->slot = slot;
98 
99     ASSERT_EQ((int)permissionFilter.OnPublish(record), (int)ERR_ANS_NOT_ALLOWED);
100 }
101 }  // namespace Notification
102 }  // namespace OHOS