1 /*
2  * Copyright (c) 2022 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 <memory>
17 
18 #include "gtest/gtest.h"
19 
20 #define private public
21 #include "distributed_preferences.h"
22 #include "distributed_preferences_info.h"
23 
24 using namespace testing::ext;
25 namespace OHOS {
26 namespace Notification {
27 class DistributedPreferencesTest : public testing::Test {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     std::shared_ptr<DistributedPreferences> distributedPreferences_;
34 };
35 
SetUp()36 void DistributedPreferencesTest::SetUp()
37 {
38     distributedPreferences_ = DistributedPreferences::GetInstance();
39 }
40 
TearDown()41 void DistributedPreferencesTest::TearDown()
42 {
43     distributedPreferences_ = nullptr;
44     DistributedPreferences::DestroyInstance();
45 }
46 
47 /**
48  * @tc.name      : DistributedPreferences_SetDistributedEnable_00100
49  * @tc.number    : SetDistributedEnable_00100
50  * @tc.desc      : Set distributed notification enable.
51  */
52 HWTEST_F(DistributedPreferencesTest, SetDistributedEnable_00100, Function | SmallTest | Level1)
53 {
54     bool enable = true;
55 
56     EXPECT_EQ(distributedPreferences_->SetDistributedEnable(enable), ERR_OK);
57 }
58 
59 /**
60  * @tc.name      : DistributedPreferences_GetDistributedEnable_00100
61  * @tc.number    : GetDistributedEnable_00100
62  * @tc.desc      : Get distributed notification enable.
63  */
64 HWTEST_F(DistributedPreferencesTest, GetDistributedEnable_00100, Function | SmallTest | Level1)
65 {
66     bool enable;
67 
68     EXPECT_EQ(distributedPreferences_->GetDistributedEnable(enable), ERR_OK);
69 }
70 
71 /**
72  * @tc.name      : DistributedPreferences_SetDistributedBundleEnable_00100
73  * @tc.number    : SetDistributedBundleEnable_00100
74  * @tc.desc      : Set distributed notification enable of a bundle.
75  */
76 HWTEST_F(DistributedPreferencesTest, SetDistributedBundleEnable_00100, Function | SmallTest | Level1)
77 {
78     bool enable = true;
79     sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("<bundleName>", 783);
80 
81     EXPECT_EQ(distributedPreferences_->SetDistributedBundleEnable(bundleOption, enable), ERR_OK);
82 }
83 
84 /**
85  * @tc.name      : DistributedPreferences_SetDistributedBundleEnable_00200
86  * @tc.number    : SetDistributedBundleEnable_00200
87  * @tc.desc      : Set distributed notification enable of a bundle.
88  */
89 HWTEST_F(DistributedPreferencesTest, SetDistributedBundleEnable_00200, Function | SmallTest | Level1)
90 {
91     bool enable = true;
92     sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("<bundleName>", 783);
93 
94     EXPECT_EQ(distributedPreferences_->SetDistributedBundleEnable(bundleOption, enable), ERR_OK);
95 }
96 
97 /**
98  * @tc.name      : DistributedPreferences_ClearDataInRestoreFactorySettings_00100
99  * @tc.number    : ClearDataInRestoreFactorySettings_00100
100  * @tc.desc      : Clear all data when system restore factory settings.
101  */
102 HWTEST_F(DistributedPreferencesTest, ClearDataInRestoreFactorySettings_00100, Function | SmallTest | Level1)
103 {
104     EXPECT_EQ(distributedPreferences_->ClearDataInRestoreFactorySettings(), ERR_OK);
105 }
106 
107 /**
108  * @tc.name      : DistributedPreferences_DeleteDistributedBundleInfo_00100
109  * @tc.number    : DeleteDistributedBundleInfo_00100
110  * @tc.desc      : Clear bundle info with distributed notification enable state.
111  */
112 HWTEST_F(DistributedPreferencesTest, DeleteDistributedBundleInfo_00100, Function | SmallTest | Level1)
113 {
114     sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("<bundleName>", 783);
115     EXPECT_EQ(distributedPreferences_->DeleteDistributedBundleInfo(bundleOption), ERR_OK);
116 }
117 
118 /**
119  * @tc.name      : DistributedPreferences_ResolveDistributedEnable_00100
120  * @tc.number    : ResolveDistributedEnable_00100
121  * @tc.desc      : text ResolveDistributedEnable function.
122  */
123 HWTEST_F(DistributedPreferencesTest, ResolveDistributedEnable_00100, Function | SmallTest | Level1)
124 {
125     std::string value("<value>");
126     EXPECT_EQ(distributedPreferences_->ResolveDistributedEnable(value), true);
127 }
128 
129 /**
130  * @tc.name      : DistributedPreferences_ResolveDistributedBundleEnable_00100
131  * @tc.number    : ResolveDistributedBundleEnable_00100
132  * @tc.desc      : text ResolveDistributedBundleEnable function.
133  */
134 HWTEST_F(DistributedPreferencesTest, ResolveDistributedBundleEnable_00100, Function | SmallTest | Level1)
135 {
136     int32_t startPos = 0;
137     std::string key("<key>");
138     std::string value("<value>");
139     EXPECT_EQ(distributedPreferences_->ResolveDistributedBundleEnable(key, startPos, value), false);
140 }
141 
142 /**
143  * @tc.name      : DistributedPreferences_ResolveDistributedBundleEnable_00200
144  * @tc.number    : ResolveDistributedBundleEnable_00200
145  * @tc.desc      : text ResolveDistributedBundleEnable function.
146  */
147 HWTEST_F(DistributedPreferencesTest, ResolveDistributedBundleEnable_00200, Function | SmallTest | Level1)
148 {
149     int32_t startPos = 1;
150     std::string key("bundleName");
151     std::string value("<value>");
152     EXPECT_EQ(distributedPreferences_->ResolveDistributedBundleEnable(key, startPos, value), false);
153 }
154 
155 /**
156  * @tc.name      : DistributedPreferences_ResolveDistributedBundleEnable_00300
157  * @tc.number    : ResolveDistributedBundleEnable_00300
158  * @tc.desc      : text ResolveDistributedBundleEnable function.
159  */
160 HWTEST_F(DistributedPreferencesTest, ResolveDistributedBundleEnable_00300, Function | SmallTest | Level1)
161 {
162     int32_t startPos = 1;
163     std::string key("bundleName|100101");
164     std::string value("<value>");
165     EXPECT_EQ(distributedPreferences_->ResolveDistributedBundleEnable(key, startPos, value), true);
166 }
167 
168 /**
169  * @tc.name      : DistributedPreferences_ResolveSyncWithoutAppEnable_00100
170  * @tc.number    : ResolveSyncWithoutAppEnable_00100
171  * @tc.desc      : text ResolveSyncWithoutAppEnable function.
172  */
173 HWTEST_F(DistributedPreferencesTest, ResolveSyncWithoutAppEnable_00100, Function | SmallTest | Level1)
174 {
175     int32_t startPos = 1;
176     std::string key("<key>");
177     std::string value("<value>");
178     EXPECT_EQ(distributedPreferences_->ResolveSyncWithoutAppEnable(key, startPos, value), true);
179 }
180 
181 /**
182  * @tc.name      : DistributedPreferencesInfo_GetSyncEnabledWithoutApp_00100
183  * @tc.number    : GetSyncEnabledWithoutApp_00100
184  * @tc.desc      : text GetSyncEnabledWithoutApp function.
185  */
186 HWTEST_F(DistributedPreferencesTest, GetSyncEnabledWithoutApp_00100, Function | SmallTest | Level1)
187 {
188     bool enabled = true;
189     DistributedPreferencesInfo distributedPreferencesInfo_;
190     distributedPreferencesInfo_.GetSyncEnabledWithoutApp(0, enabled);
191     EXPECT_EQ(enabled, false);
192 }
193 
194 /**
195  * @tc.name      : DistributedPreferencesInfo_GetDistributedBundleEnable_00100
196  * @tc.number    : GetDistributedBundleEnable_00100
197  * @tc.desc      : text GetDistributedBundleEnable function.
198  */
199 HWTEST_F(DistributedPreferencesTest, GetDistributedBundleEnable_00100, Function | SmallTest | Level1)
200 {
201     DistributedPreferencesInfo distributedPreferencesInfo_;
202     bool res = distributedPreferencesInfo_.GetDistributedBundleEnable("bundle", 0);
203     EXPECT_EQ(res, true);
204 }
205 
206 /**
207  * @tc.name      : DistributedPreferencesInfo_GetDistributedBundleEnable_00200
208  * @tc.number    : GetDistributedBundleEnable_00200
209  * @tc.desc      : text GetDistributedBundleEnable function.
210  */
211 HWTEST_F(DistributedPreferencesTest, GetDistributedBundleEnable_00200, Function | SmallTest | Level1)
212 {
213     DistributedPreferencesInfo distributedPreferencesInfo_;
214     bool res = distributedPreferencesInfo_.GetDistributedBundleEnable("com.ohos.mms", 0);
215     EXPECT_EQ(res, true);
216 }
217 
218 /**
219  * @tc.name      : DistributedPreferencesInfo_GetSyncEnabledWithoutApp_00200
220  * @tc.number    : GetSyncEnabledWithoutApp_00200
221  * @tc.desc      : text GetSyncEnabledWithoutApp function.
222  */
223 HWTEST_F(DistributedPreferencesTest, GetSyncEnabledWithoutApp_00200, Function | SmallTest | Level1)
224 {
225     bool enabled = true;
226     DistributedPreferencesInfo distributedPreferencesInfo_;
227     distributedPreferencesInfo_.GetSyncEnabledWithoutApp(100, enabled);
228     EXPECT_EQ(enabled, false);
229 }
230 }  // namespace Notification
231 }  // namespace OHOS