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 <gtest/gtest.h>
17 
18 #include "ans_inner_errors.h"
19 #include "ans_ut_constant.h"
20 #include "notification_bundle_option.h"
21 #include "notification_constant.h"
22 #define private public
23 #define protected public
24 #include "notification_preferences_info.h"
25 #include "advanced_notification_service.h"
26 #undef private
27 #undef protected
28 
29 using namespace testing::ext;
30 namespace OHOS {
31 namespace Notification {
32 class NotificationPreferencesInfoTest : public testing::Test {
33 public:
SetUpTestCase()34     static void SetUpTestCase() {};
TearDownTestCase()35     static void TearDownTestCase() {};
SetUp()36     void SetUp() {};
TearDown()37     void TearDown() {};
38 };
39 
40 /**
41  * @tc.name: GetSlotFlagsKeyFromType_00001
42  * @tc.desc: Test GetSlotFlagsKeyFromType
43  * @tc.type: FUNC
44  * @tc.require: issue
45  */
46 HWTEST_F(NotificationPreferencesInfoTest, GetSlotFlagsKeyFromType_00001, Function | SmallTest | Level1)
47 {
48     NotificationPreferencesInfo::BundleInfo bundleInfo;
49     const char *res= bundleInfo.GetSlotFlagsKeyFromType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
50     std::string resStr(res);
51     ASSERT_EQ(resStr, "Social_communication");
52 
53     res = bundleInfo.GetSlotFlagsKeyFromType(NotificationConstant::SlotType::SERVICE_REMINDER);
54     resStr = res;
55     ASSERT_EQ(resStr, "Service_reminder");
56 
57     res = bundleInfo.GetSlotFlagsKeyFromType(NotificationConstant::SlotType::CONTENT_INFORMATION);
58     resStr = res;
59     ASSERT_EQ(resStr, "Content_information");
60 
61     res = bundleInfo.GetSlotFlagsKeyFromType(NotificationConstant::SlotType::OTHER);
62     resStr = res;
63     ASSERT_EQ(resStr, "Other");
64 
65     res = bundleInfo.GetSlotFlagsKeyFromType(NotificationConstant::SlotType::CUSTOM);
66     resStr = res;
67     ASSERT_EQ(resStr, "Custom");
68 
69     res = bundleInfo.GetSlotFlagsKeyFromType(NotificationConstant::SlotType::LIVE_VIEW);
70     resStr = res;
71     ASSERT_EQ(resStr, "Live_view");
72 
73     res = bundleInfo.GetSlotFlagsKeyFromType(NotificationConstant::SlotType::CUSTOMER_SERVICE);
74     resStr = res;
75     ASSERT_EQ(resStr, "Custom_service");
76 }
77 
78 
79 /**
80  * @tc.name: SetSlotFlagsForSlot_00001
81  * @tc.desc: Test SetSlotFlagsForSlot
82  * @tc.type: FUNC
83  * @tc.require: issue
84  */
85 HWTEST_F(NotificationPreferencesInfoTest, SetSlotFlagsForSlot_00001, Function | SmallTest | Level1)
86 {
87     NotificationPreferencesInfo::BundleInfo bundleInfo;
88     bundleInfo.SetSlotFlags(1);
89     bundleInfo.SetSlotFlagsForSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
90     int res = bundleInfo.GetSlotFlagsForSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
91     ASSERT_EQ(res, 0);
92 }
93 
94 /**
95  * @tc.name: MakeDoNotDisturbProfileKey_0100
96  * @tc.desc: test MakeDoNotDisturbProfileKey can convert key right.
97  * @tc.type: FUNC
98  */
99 HWTEST_F(NotificationPreferencesInfoTest, MakeDoNotDisturbProfileKey_0100, TestSize.Level1)
100 {
101     std::shared_ptr<NotificationPreferencesInfo> preferencesInfo = std::make_shared<NotificationPreferencesInfo>();
102     int32_t userId = 1;
103     int32_t profileId = 1;
104     string profilekey = "1_1";
105     auto res = preferencesInfo->MakeDoNotDisturbProfileKey(userId, profileId);
106     ASSERT_EQ(res, profilekey);
107 }
108 
109 /**
110  * @tc.name: AddDoNotDisturbProfiles_0100
111  * @tc.desc: test AddDoNotDisturbProfiles can add success.
112  * @tc.type: FUNC
113  */
114 HWTEST_F(NotificationPreferencesInfoTest, AddDoNotDisturbProfiles_0100, TestSize.Level1)
115 {
116     std::shared_ptr<NotificationPreferencesInfo> preferencesInfo = std::make_shared<NotificationPreferencesInfo>();
117     int32_t userId = 1;
118     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
119     sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
120     int32_t profileId = 1;
121     profile->SetProfileId(profileId);
122     profiles.emplace_back(profile);
123     preferencesInfo->AddDoNotDisturbProfiles(userId, profiles);
124 
125     auto res = preferencesInfo->GetDoNotDisturbProfiles(profileId, userId, profile);
126     ASSERT_EQ(res, true);
127 }
128 
129 /**
130  * @tc.name: RemoveDoNotDisturbProfiles_0100
131  * @tc.desc: test RemoveDoNotDisturbProfiles can remove success.
132  * @tc.type: FUNC
133  */
134 HWTEST_F(NotificationPreferencesInfoTest, RemoveDoNotDisturbProfiles_0100, TestSize.Level1)
135 {
136     std::shared_ptr<NotificationPreferencesInfo> preferencesInfo = std::make_shared<NotificationPreferencesInfo>();
137     int32_t userId = 1;
138     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
139     sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
140     profiles.emplace_back(profile);
141     preferencesInfo->RemoveDoNotDisturbProfiles(userId, profiles);
142     int32_t profileId = 1;
143     auto res = preferencesInfo->GetDoNotDisturbProfiles(profileId, userId, profile);
144     ASSERT_EQ(res, false);
145 }
146 
147 /**
148  * @tc.name: GetDoNotDisturbProfiles_0100
149  * @tc.desc: test GetDoNotDisturbProfiles can get success.
150  * @tc.type: FUNC
151  */
152 HWTEST_F(NotificationPreferencesInfoTest, GetDoNotDisturbProfiles_0100, TestSize.Level1)
153 {
154     std::shared_ptr<NotificationPreferencesInfo> preferencesInfo = std::make_shared<NotificationPreferencesInfo>();
155     int32_t userId = 1;
156     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
157     sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
158     int32_t profileId = 1;
159     profile->SetProfileId(profileId);
160     profiles.emplace_back(profile);
161     preferencesInfo->AddDoNotDisturbProfiles(userId, profiles);
162     auto res = preferencesInfo->GetDoNotDisturbProfiles(profileId, userId, profile);
163     ASSERT_EQ(res, true);
164 }
165 
166 /**
167  * @tc.name: RemoveBundleInfo_0100
168  * @tc.desc: test RemoveBundleInfo.
169  * @tc.type: FUNC
170  */
171 HWTEST_F(NotificationPreferencesInfoTest, RemoveBundleInfo_0100, TestSize.Level1)
172 {
173     std::shared_ptr<NotificationPreferencesInfo> preferencesInfo = std::make_shared<NotificationPreferencesInfo>();
174     sptr<NotificationBundleOption> bundleInfo = new NotificationBundleOption("test", 1);;
175     auto res = preferencesInfo->RemoveBundleInfo(bundleInfo);
176     ASSERT_EQ(res, false);
177 }
178 }
179 }
180