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