1 /*
2 * Copyright (c) 2022-2023 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 #define private public
20 #include "ans_inner_errors.h"
21 #include "distributed_notification_manager.h"
22 #include "distributed_preferences.h"
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Notification {
27 class DistributedNotificationManagerTest : public testing::Test {
28 public:
29 void SetUp() override;
30 void TearDown() override;
31
32 public:
OnPublish(const std::string & deviceId,const std::string & bundleName,sptr<NotificationRequest> & request)33 virtual void OnPublish(
34 const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request) {};
OnUpdate(const std::string & deviceId,const std::string & bundleName,sptr<NotificationRequest> & request)35 virtual void OnUpdate(
36 const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request) {};
OnDelete(const std::string & deviceId,const std::string & bundleName,const std::string & label,int32_t id)37 virtual void OnDelete(
38 const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) {};
39
40 protected:
41 std::shared_ptr<DistributedNotificationManager> distributedManager_;
42 };
43
SetUp()44 void DistributedNotificationManagerTest::SetUp()
45 {
46 distributedManager_ = DistributedNotificationManager::GetInstance();
47 distributedManager_->OnDeviceConnected("test");
48 }
49
TearDown()50 void DistributedNotificationManagerTest::TearDown()
51 {
52 distributedManager_ = nullptr;
53 DistributedNotificationManager::DestroyInstance();
54 }
55
56 /**
57 * @tc.name : Distributed_Publish_00100
58 * @tc.number : Distributed_Publish_00100
59 * @tc.desc : Publish a local notification to remote used distributed.
60 */
61 HWTEST_F(DistributedNotificationManagerTest, Distributed_Publish_00100, Function | SmallTest | Level1)
62 {
63 sptr<NotificationRequest> request = new NotificationRequest(1000);
64 request->SetLabel("<label>");
65
66 std::string bundleName = "<bundleName>";
67 std::string label = request->GetLabel();
68 int32_t id = request->GetNotificationId();
69
70 EXPECT_EQ(distributedManager_->Publish(bundleName, label, id, request), ERR_ANS_DISTRIBUTED_OPERATION_FAILED);
71 }
72
73 /**
74 * @tc.name : Distributed_Publish_00200
75 * @tc.number : Distributed_Publish_00200
76 * @tc.desc : Publish a local notification to remote used distributed.
77 */
78 HWTEST_F(DistributedNotificationManagerTest, Distributed_Publish_00200, Function | SmallTest | Level1)
79 {
80 sptr<NotificationRequest> request = new NotificationRequest(1000);
81 request->SetLabel("<label>");
82
83 std::string bundleName = "<bundleName>";
84 std::string label = request->GetLabel();
85 int32_t id = request->GetNotificationId();
86
87 EXPECT_EQ(distributedManager_->Publish(bundleName, label, id, request), ERR_ANS_DISTRIBUTED_OPERATION_FAILED);
88 }
89
90 /**
91 * @tc.name : Distributed_Update_00100
92 * @tc.number : Distributed_Update_00100
93 * @tc.desc : Update a local notification to remote used distributed.
94 */
95 HWTEST_F(DistributedNotificationManagerTest, Distributed_Update_00100, Function | SmallTest | Level1)
96 {
97 sptr<NotificationRequest> request = new NotificationRequest(1000);
98 request->SetLabel("<label>");
99
100 std::string bundleName = "<bundleName>";
101 std::string label = request->GetLabel();
102 int32_t id = request->GetNotificationId();
103
104 EXPECT_EQ(distributedManager_->Update(bundleName, label, id, request), ERR_ANS_DISTRIBUTED_OPERATION_FAILED);
105 }
106
107 /**
108 * @tc.name : Distributed_Update_00200
109 * @tc.number : Distributed_Update_00200
110 * @tc.desc : Update a local notification to remote used distributed.
111 */
112 HWTEST_F(DistributedNotificationManagerTest, Distributed_Update_00200, Function | SmallTest | Level1)
113 {
114 sptr<NotificationRequest> request = new NotificationRequest(1000);
115 request->SetLabel("<label>");
116
117 std::string bundleName = "<bundleName>";
118 std::string label = request->GetLabel();
119 int32_t id = request->GetNotificationId();
120
121 EXPECT_EQ(distributedManager_->Update(bundleName, label, id, request), ERR_ANS_DISTRIBUTED_OPERATION_FAILED);
122 }
123
124 /**
125 * @tc.name : Distributed_Delete_00100
126 * @tc.number : Distributed_Delete_00100
127 * @tc.desc : Delete a local notification to remote used distributed.
128 */
129 HWTEST_F(DistributedNotificationManagerTest, Distributed_Delete_00100, Function | SmallTest | Level1)
130 {
131 sptr<NotificationRequest> request = new NotificationRequest(1000);
132 request->SetLabel("<label>");
133
134 std::string bundleName = "<bundleName>";
135 std::string label = request->GetLabel();
136 int32_t id = request->GetNotificationId();
137
138 EXPECT_EQ(distributedManager_->Delete(bundleName, label, id), ERR_ANS_DISTRIBUTED_OPERATION_FAILED);
139 }
140
141 /**
142 * @tc.name : Distributed_Delete_00200
143 * @tc.number : Distributed_Delete_00200
144 * @tc.desc : Delete a remote notification to remote used distributed.
145 */
146 HWTEST_F(DistributedNotificationManagerTest, Distributed_Delete_00200, Function | SmallTest | Level1)
147 {
148 sptr<NotificationRequest> request = new NotificationRequest(1000);
149 request->SetLabel("<label>");
150
151 std::string deviceId = "<remoteDeviceId>";
152 std::string bundleName = "<bundleName>";
153 std::string label = request->GetLabel();
154 int32_t id = request->GetNotificationId();
155
156 EXPECT_EQ(distributedManager_->DeleteRemoteNotification(deviceId, bundleName, label, id),
157 ERR_ANS_DISTRIBUTED_OPERATION_FAILED);
158 }
159
160 /**
161 * @tc.name : Distributed_Delete_00300
162 * @tc.number : Distributed_Delete_00300
163 * @tc.desc : Delete a remote notification to remote used distributed.
164 */
165 HWTEST_F(DistributedNotificationManagerTest, Distributed_Delete_00300, Function | SmallTest | Level1)
166 {
167 sptr<NotificationRequest> request = new NotificationRequest(1000);
168 request->SetLabel("<label>");
169
170 std::string bundleName = "<bundleName>";
171 std::string label = request->GetLabel();
172 int32_t id = request->GetNotificationId();
173
174 EXPECT_EQ(distributedManager_->Delete(bundleName, label, id), ERR_ANS_DISTRIBUTED_OPERATION_FAILED);
175 }
176
177 /**
178 * @tc.name : Distributed_Register_Manager_callback_00100
179 * @tc.number : Distributed_Register_Manager_callback_00100
180 * @tc.desc : Register callback to watch distributed database change.
181 */
182 HWTEST_F(DistributedNotificationManagerTest, Distributed_Register_Manager_callback_00100, Function | SmallTest | Level1)
183 {
184 DistributedNotificationManager::IDistributedCallback callback = {
185 .OnPublish = std::bind(&DistributedNotificationManagerTest::OnPublish,
186 this,
187 std::placeholders::_1,
188 std::placeholders::_2,
189 std::placeholders::_3),
190 .OnUpdate = std::bind(&DistributedNotificationManagerTest::OnUpdate,
191 this,
192 std::placeholders::_1,
193 std::placeholders::_2,
194 std::placeholders::_3),
195 .OnDelete = std::bind(&DistributedNotificationManagerTest::OnDelete,
196 this,
197 std::placeholders::_1,
198 std::placeholders::_2,
199 std::placeholders::_3,
200 std::placeholders::_4),
201 };
202
203 EXPECT_EQ(distributedManager_->RegisterCallback(callback), ERR_OK);
204 }
205
206 /**
207 * @tc.name : Distributed_Unregister_Manager_callback_00100
208 * @tc.number : Distributed_Unregister_Manager_callback_00100
209 * @tc.desc : Unregister callback to watch distributed database change.
210 */
211 HWTEST_F(
212 DistributedNotificationManagerTest, Distributed_Unregister_Manager_callback_00100, Function | SmallTest | Level1)
213 {
214 EXPECT_EQ(distributedManager_->UngegisterCallback(), ERR_OK);
215 }
216
217 /**
218 * @tc.name : Distributed_Get_Current_Notification_00100
219 * @tc.number : Distributed_Get_Current_Notification_00100
220 * @tc.desc : Get current notification in distributed database.
221 */
222 HWTEST_F(DistributedNotificationManagerTest, Distributed_Get_Current_Notification_00100, Function | SmallTest | Level1)
223 {
224 std::vector<sptr<NotificationRequest>> requestList;
225 EXPECT_EQ(distributedManager_->GetCurrentDistributedNotification(requestList),
226 ERR_ANS_DISTRIBUTED_OPERATION_FAILED);
227 }
228
229 /**
230 * @tc.name : Distributed_Get_Local_DeviceInfo_00100
231 * @tc.number : Distributed_Get_Local_DeviceInfo_00100
232 * @tc.desc : Get local distributed device information.
233 */
234 HWTEST_F(DistributedNotificationManagerTest, Distributed_Get_Local_DeviceInfo_00100, Function | SmallTest | Level1)
235 {
236 DistributedDatabase::DeviceInfo deviceInfo;
237 EXPECT_EQ(distributedManager_->GetLocalDeviceInfo(deviceInfo), ERR_OK);
238 }
239
240 /**
241 * @tc.name : Distributed_ResolveDistributedKey_00100
242 * @tc.number : Distributed_ResolveDistributedKey_00100
243 * @tc.desc : text ResolveDistributedKey function.
244 */
245 HWTEST_F(DistributedNotificationManagerTest, Distributed_ResolveDistributedKey_00100, Function | SmallTest | Level1)
246 {
247 std::string key("<key>");
248 DistributedNotificationManager::ResolveKey resolveKey;
249 EXPECT_EQ(distributedManager_->ResolveDistributedKey(key, resolveKey), false);
250 }
251
252 /**
253 * @tc.name : Distributed_ResolveDistributedKey_00200
254 * @tc.number : Distributed_ResolveDistributedKey_00200
255 * @tc.desc : text ResolveDistributedKey function.
256 */
257 HWTEST_F(DistributedNotificationManagerTest, Distributed_ResolveDistributedKey_00200, Function | SmallTest | Level1)
258 {
259 std::string key("deviceId|bundleName");
260 DistributedNotificationManager::ResolveKey resolveKey;
261 EXPECT_EQ(distributedManager_->ResolveDistributedKey(key, resolveKey), false);
262 }
263
264 /**
265 * @tc.name : Distributed_ResolveDistributedKey_00300
266 * @tc.number : Distributed_ResolveDistributedKey_00300
267 * @tc.desc : text ResolveDistributedKey function.
268 */
269 HWTEST_F(DistributedNotificationManagerTest, Distributed_ResolveDistributedKey_00300, Function | SmallTest | Level1)
270 {
271 std::string key("deviceId|bundleName|label");
272 DistributedNotificationManager::ResolveKey resolveKey;
273 EXPECT_EQ(distributedManager_->ResolveDistributedKey(key, resolveKey), false);
274 }
275
276 /**
277 * @tc.name : Distributed_ResolveDistributedKey_00400
278 * @tc.number : Distributed_ResolveDistributedKey_00400
279 * @tc.desc : text ResolveDistributedKey function.
280 */
281 HWTEST_F(DistributedNotificationManagerTest, Distributed_ResolveDistributedKey_00400, Function | SmallTest | Level1)
282 {
283 std::string key("deviceId|bundleName|label|0");
284 DistributedNotificationManager::ResolveKey resolveKey;
285 EXPECT_EQ(distributedManager_->ResolveDistributedKey(key, resolveKey), true);
286 }
287
288 /**
289 * @tc.name : Distributed_CheckDeviceId_00100
290 * @tc.number : Distributed_CheckDeviceId_00100
291 * @tc.desc : text CheckDeviceId function.
292 */
293 HWTEST_F(DistributedNotificationManagerTest, Distributed_CheckDeviceId_00100, Function | SmallTest | Level1)
294 {
295 std::string deviceId = "<remoteDeviceId>";
296 std::string key("<key>");
297 std::string value("<value>");
298 // text OnDatabaseInsert function.
299 distributedManager_->OnDatabaseInsert(deviceId, key, value);
300 // text OnDatabaseUpdate function.
301 distributedManager_->OnDatabaseUpdate(deviceId, key, value);
302 // text OnDatabaseDelete function.
303 distributedManager_->OnDatabaseDelete(deviceId, key, value);
304 // text CheckDeviceId function.
305 EXPECT_EQ(distributedManager_->CheckDeviceId(deviceId, key), false);
306 }
307
308 /**
309 * @tc.name : Distributed_CheckDeviceId_00200
310 * @tc.number : Distributed_CheckDeviceId_00200
311 * @tc.desc : text CheckDeviceId function.
312 */
313 HWTEST_F(DistributedNotificationManagerTest, Distributed_CheckDeviceId_00200, Function | SmallTest | Level1)
314 {
315 std::string deviceId = "deviceId";
316 std::string key("deviceId|bundleName|label|0");
317 std::string value("");
318 // text OnDatabaseInsert function.
319 distributedManager_->OnDatabaseInsert(deviceId, key, value);
320 // text OnDatabaseUpdate function.
321 distributedManager_->OnDatabaseUpdate(deviceId, key, value);
322 // text OnDatabaseDelete function.
323 distributedManager_->OnDatabaseDelete(deviceId, key, value);
324 // text CheckDeviceId function.
325 EXPECT_EQ(distributedManager_->CheckDeviceId(deviceId, key), true);
326 }
327
328 /**
329 * @tc.name : Distributed_OnDeviceDisconnected_00100
330 * @tc.number : Distributed_OnDeviceDisconnected_00100
331 * @tc.desc : text OnDeviceDisconnected function.
332 */
333 HWTEST_F(DistributedNotificationManagerTest, Distributed_OnDeviceDisconnected_00100, Function | SmallTest | Level1)
334 {
335 const std::string deviceId = "<remoteDeviceId>";
336 // text OnDeviceDisconnected function.
337 distributedManager_->OnDeviceDisconnected(deviceId);
338 // text PublishCallback function.
339 const std::string bundleName = "<bundleName>";
340 sptr<NotificationRequest> request = new NotificationRequest(1);
341 EXPECT_EQ(distributedManager_->PublishCallback(deviceId, bundleName, request), true);
342 }
343
344 /**
345 * @tc.name : Distributed_UpdateCallback_00100
346 * @tc.number : Distributed_UpdateCallback_00100
347 * @tc.desc : text UpdateCallback function.
348 */
349 HWTEST_F(DistributedNotificationManagerTest, Distributed_UpdateCallback_00100, Function | SmallTest | Level1)
350 {
351 std::string deviceId = "<remoteDeviceId>";
352 std::string bundleName = "<bundleName>";
353 sptr<NotificationRequest> request = new NotificationRequest(1000);
354 EXPECT_EQ(distributedManager_->UpdateCallback(deviceId, bundleName, request), true);
355 }
356
357 /**
358 * @tc.name : Distributed_DeleteCallback_00100
359 * @tc.number : Distributed_DeleteCallback_00100
360 * @tc.desc : text DeleteCallback function.
361 */
362 HWTEST_F(DistributedNotificationManagerTest, Distributed_DeleteCallback_00100, Function | SmallTest | Level1)
363 {
364 sptr<NotificationRequest> request = new NotificationRequest(1000);
365 request->SetLabel("<label>");
366
367 std::string deviceId = "<remoteDeviceId>";
368 std::string bundleName = "<bundleName>";
369 std::string label = request->GetLabel();
370 int32_t id = request->GetNotificationId();
371 EXPECT_EQ(distributedManager_->DeleteCallback(deviceId, bundleName, label, id), true);
372 }
373
374 /**
375 * @tc.name : Distributed_OnDistributedKvStoreDeathRecipient_00100
376 * @tc.number : Distributed_OnDistributedKvStoreDeathRecipient_00100
377 * @tc.desc : text OnDistributedKvStoreDeathRecipient function.
378 */
379 HWTEST_F(DistributedNotificationManagerTest, Distributed_OnDistributedKvStoreDeathRecipient_00100,
380 Function | SmallTest | Level1)
381 {
382 DistributedPreferences::GetInstance()->SetDistributedEnable(true);
383 EXPECT_EQ(distributedManager_->OnDistributedKvStoreDeathRecipient(), ERR_OK);
384 }
385 } // namespace Notification
386 } // namespace OHOS
387