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 #include <gtest/gtest.h>
16 #include <functional>
17 
18 #include "ans_inner_errors.h"
19 #include "ans_manager_proxy.h"
20 #define private public
21 #include "advanced_notification_service.h"
22 #undef private
23 #include "if_system_ability_manager.h"
24 #include "iservice_registry.h"
25 #include "notification_helper.h"
26 #include "remote_native_token.h"
27 #include "system_ability_definition.h"
28 
29 using namespace testing::ext;
30 namespace OHOS {
31 namespace Notification {
32 static sptr<ISystemAbilityManager> systemAbilityManager =
33     SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
34 const int32_t CALLING_UID = 9998;
35 const int32_t USERID = 100;
36 
37 class AnsInnerKitsModuleSettingTest : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41     void SetUp();
42     void TearDown();
43 
44     static sptr<AdvancedNotificationService> service_;
45 };
46 
47 sptr<AdvancedNotificationService> AnsInnerKitsModuleSettingTest::service_;
SetUpTestCase()48 void AnsInnerKitsModuleSettingTest::SetUpTestCase()
49 {
50     RemoteNativeToken::SetNativeToken("ans_innerkits_module_setting_test");
51     service_ = OHOS::Notification::AdvancedNotificationService::GetInstance();
52     OHOS::ISystemAbilityManager::SAExtraProp saExtraProp;
53     systemAbilityManager->AddSystemAbility(OHOS::ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID, service_, saExtraProp);
54 }
55 
TearDownTestCase()56 void AnsInnerKitsModuleSettingTest::TearDownTestCase()
57 {
58     if (service_ != nullptr) {
59         service_->SelfClean();
60     }
61 }
62 
SetUp()63 void AnsInnerKitsModuleSettingTest::SetUp()
64 {}
65 
TearDown()66 void AnsInnerKitsModuleSettingTest::TearDown()
67 {}
68 
69 /**
70  * @tc.number    : ANS_Interface_MT_NotificationSetting_00100
71  * @tc.name      : NotificationSetting_00100
72  * @tc.desc      : Set a specified application to show badge, get the specified application can show badge.
73  * @tc.expected  : Set a specified application to show badge success, get the specified application can show badge.
74  */
75 HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_00100, Function | MediumTest | Level1)
76 {
77     NotificationBundleOption bundleOption;
78     bundleOption.SetBundleName("bundlename");
79     bundleOption.SetUid(CALLING_UID);
80     GTEST_LOG_(INFO) << "BundleOption is:"<<bundleOption.Dump();
81     EXPECT_EQ(ERR_ANS_PERMISSION_DENIED, NotificationHelper::SetShowBadgeEnabledForBundle(bundleOption, true));
82     bool enabled = false;
83     EXPECT_EQ(ERR_ANS_PERMISSION_DENIED, NotificationHelper::GetShowBadgeEnabledForBundle(bundleOption, enabled));
84     EXPECT_EQ("bundlename", bundleOption.GetBundleName());
85     EXPECT_EQ(CALLING_UID, bundleOption.GetUid());
86 }
87 
88 /**
89  * @tc.number    : ANS_Interface_MT_NotificationSetting_00300
90  * @tc.name      : NotificationSetting_00300
91  * @tc.desc      : Set a specified application can publish notification, get the specified application can publish
92  *                 notification.
93  * @tc.expected  : Set a specified application can publish notification success, get the specified application can
94  *                 publish notification.
95  */
96 HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_00300, Function | MediumTest | Level1)
97 {
98     NotificationBundleOption bundleOption("bundlename", CALLING_UID);
99     std::string deviceId;
100     EXPECT_EQ(ERR_ANS_PERMISSION_DENIED,
101         NotificationHelper::SetNotificationsEnabledForSpecifiedBundle(bundleOption, deviceId, true));
102     bool enabled = false;
103     EXPECT_EQ(ERR_ANS_PERMISSION_DENIED, NotificationHelper::IsAllowedNotify(bundleOption, enabled));
104     EXPECT_EQ("bundlename", bundleOption.GetBundleName());
105     EXPECT_EQ(CALLING_UID, bundleOption.GetUid());
106 }
107 
108 /**
109  * @tc.number    : ANS_Interface_MT_NotificationSetting_00500
110  * @tc.name      : NotificationSetting_00500
111  * @tc.desc      : If the template configuration file does not exist, query whether the template exists.
112  * @tc.expected  : Query return failed.
113  */
114 HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_00500, Function | MediumTest | Level1)
115 {
116     std::string templateName("downloadTemplate");
117     bool support = false;
118     EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support));
119     EXPECT_EQ(true, support);
120 }
121 
122 /**
123  * @tc.number    : ANS_Interface_MT_NotificationSetting_00700
124  * @tc.name      : NotificationSetting_00700
125  * @tc.desc      : The template does not exist in the system, query whether the template exists.
126  * @tc.expected  : Query return failed.
127  */
128 HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_00700, Function | MediumTest | Level1)
129 {
130     std::string templateName("downloadTemplate_1");
131     bool support = false;
132     EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support));
133     EXPECT_EQ(false, support);
134 }
135 
136 /**
137  * @tc.number    : ANS_Interface_MT_NotificationSetting_00800
138  * @tc.name      : NotificationSetting_00800
139  * @tc.desc      : Set whether to sync notifications to devices that do not have the app installed.
140  * @tc.expected  : Set true, get true.
141  */
142 HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_00800, Function | MediumTest | Level1)
143 {
144     NotificationBundleOption bundleOption("bundlename", CALLING_UID);
145     EXPECT_EQ(ERR_ANS_PERMISSION_DENIED, NotificationHelper::SetSyncNotificationEnabledWithoutApp(USERID, true));
146     bool enabled = false;
147     EXPECT_EQ(ERR_ANS_PERMISSION_DENIED, NotificationHelper::GetSyncNotificationEnabledWithoutApp(USERID, enabled));
148 }
149 }  // namespace Notification
150 }  // namespace OHOS