• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "channel_manager.h"
17 #include "control_cmd_parser.h"
18 #include "gtest/gtest.h"
19 #include "gmock/gmock.h"
20 #include "system_notifier.h"
21 #include "notification_helper.h"
22 #include "image_source.h"
23 #include "dfs_error.h"
24 #include "device_manager.h"
25 #include "utils_log.h"
26 #include "file_ex.h"
27 
28 namespace {
29 int g_publishNotification = OHOS::FileManagement::ERR_OK;
30 int g_cancelNotification = OHOS::FileManagement::ERR_OK;
31 std::string g_loadStringFromFile = "";
32 }  // namespace
33 
34 namespace OHOS {
LoadStringFromFile(const std::string & filePath,std::string & content)35 bool LoadStringFromFile(const std::string& filePath, std::string& content)
36 {
37     content = g_loadStringFromFile;
38     return true;
39 }
40 }
41 
42 namespace OHOS {
43 namespace Notification {
PublishNotification(const NotificationRequest & request,const std::string & instanceKey)44 ErrCode NotificationHelper::PublishNotification(const NotificationRequest &request, const std::string &instanceKey)
45 {
46     return g_publishNotification;
47 }
48 
CancelNotification(int32_t notificationId,const std::string & instanceKey)49 ErrCode NotificationHelper::CancelNotification(int32_t notificationId, const std::string &instanceKey)
50 {
51     return g_cancelNotification;
52 }
53 }  // namespace Notification
54 }  // namespace OHOS
55 
56 namespace OHOS {
57 namespace Storage {
58 namespace DistributedFile {
59 
SendRequest(const std::string & networkId,ControlCmd & request,ControlCmd & response,bool needResponse)60 int32_t ChannelManager::SendRequest(const std::string &networkId,
61                                     ControlCmd &request,
62                                     ControlCmd &response,
63                                     bool needResponse)
64 {
65     return OHOS::FileManagement::ERR_OK;
66 }
67 
68 namespace Test {
69 using namespace testing;
70 using namespace testing::ext;
71 using OHOS::FileManagement::ERR_BAD_VALUE;
72 using OHOS::FileManagement::ERR_OK;
73 
74 class SystemNotifierTest : public testing::Test {
75 public:
76     static void SetUpTestCase(void);
77     static void TearDownTestCase(void);
78     void SetUp();
79     void TearDown();
80 
81 public:
82     static inline std::string testNetworkId = "test-network-id";
83     static inline int32_t testNotificationId = 1001;
84 };
85 
SetUpTestCase(void)86 void SystemNotifierTest::SetUpTestCase(void)
87 {
88     GTEST_LOG_(INFO) << "SystemNotifierTest SetUpTestCase";
89 }
90 
TearDownTestCase(void)91 void SystemNotifierTest::TearDownTestCase(void)
92 {
93     GTEST_LOG_(INFO) << "SystemNotifierTest TearDownTestCase";
94 }
95 
SetUp(void)96 void SystemNotifierTest::SetUp(void)
97 {
98     GTEST_LOG_(INFO) << "SetUp";
99     g_publishNotification = OHOS::FileManagement::ERR_OK;
100     g_cancelNotification = OHOS::FileManagement::ERR_OK;
101     g_loadStringFromFile = "{\"key-value\":[{\"key\":\"notification_title\",\"value\":\"文件桥\"},"
102     "{\"key\":\"notification_text\",\"value\":\"可在 {} 上管理本机文件\"},"
103     "{\"key\":\"capsule_title\",\"value\":\"已连接\"},"
104     "{\"key\":\"button_name\",\"value\":\"断开\"}]}";
105 }
106 
TearDown(void)107 void SystemNotifierTest::TearDown(void)
108 {
109     GTEST_LOG_(INFO) << "TearDown";
110 }
111 
112 /**
113  * @tc.name: SystemNotifierTest_CreateLocalLiveView_001
114  * @tc.desc: Verify CreateLocalLiveView creates notification successfully.
115  * @tc.type: FUNC
116  * @tc.require: I7TDJK
117  */
118 HWTEST_F(SystemNotifierTest, CreateLocalLiveView_001, TestSize.Level1)
119 {
120     GTEST_LOG_(INFO) << "CreateLocalLiveView_001 start";
121 
122     auto &notifier = SystemNotifier::GetInstance();
123     int32_t ret = notifier.CreateLocalLiveView(testNetworkId);
124     EXPECT_EQ(ret, ERR_OK);
125 
126     g_publishNotification = ERR_BAD_VALUE;
127     notifier.notificationMap_.clear();
128     int32_t ret2 = notifier.CreateLocalLiveView(testNetworkId);
129     EXPECT_EQ(ret2, ERR_BAD_VALUE);
130 
131     GTEST_LOG_(INFO) << "CreateLocalLiveView_001 end";
132 }
133 
134 /**
135  * @tc.name: SystemNotifierTest_CreateNotification_001
136  * @tc.desc: Verify CreateNotification creates notification successfully.
137  * @tc.type: FUNC
138  * @tc.require: I7TDJK
139  */
140 HWTEST_F(SystemNotifierTest, CreateNotification_001, TestSize.Level1)
141 {
142     GTEST_LOG_(INFO) << "CreateNotification_001 start";
143 
144     auto &notifier = SystemNotifier::GetInstance();
145     int32_t ret = notifier.CreateNotification(testNetworkId);
146     EXPECT_EQ(ret, ERR_OK);
147 
148     g_publishNotification = ERR_BAD_VALUE;
149     notifier.notificationMap_.clear();
150     int32_t ret2 = notifier.CreateNotification(testNetworkId);
151     EXPECT_EQ(ret2, ERR_BAD_VALUE);
152 
153     GTEST_LOG_(INFO) << "CreateNotification_001 end";
154 }
155 
156 /**
157  * @tc.name: SystemNotifierTest_CancelNotifyByNetworkId_001
158  * @tc.desc: Verify CancelNotifyByNetworkId cancels notification successfully.
159  * @tc.type: FUNC
160  * @tc.require: I7TDJK
161  */
162 HWTEST_F(SystemNotifierTest, CancelNotifyByNetworkId_001, TestSize.Level1)
163 {
164     GTEST_LOG_(INFO) << "CancelNotifyByNetworkId_001 start";
165 
166     auto &notifier = SystemNotifier::GetInstance();
167     notifier.CreateNotification(testNetworkId);
168 
169     int32_t ret = notifier.DestroyNotifyByNetworkId(testNetworkId);
170     EXPECT_EQ(ret, ERR_OK);
171 
172     GTEST_LOG_(INFO) << "CancelNotifyByNetworkId_001 end";
173 }
174 
175 /**
176  * @tc.name: SystemNotifierTest_CancelNotifyByNotificationId_001
177  * @tc.desc: Verify CancelNotifyByNotificationId cancels notification successfully.
178  * @tc.type: FUNC
179  * @tc.require: I7TDJK
180  */
181 HWTEST_F(SystemNotifierTest, CancelNotifyByNotificationId_001, TestSize.Level1)
182 {
183     GTEST_LOG_(INFO) << "CancelNotifyByNotificationId_001 start";
184 
185     auto &notifier = SystemNotifier::GetInstance();
186     int32_t res = notifier.CreateNotification(testNetworkId);
187     EXPECT_EQ(res, ERR_OK);
188 
189     notifier.notificationMap_["network-1"] = 666;
190     notifier.notificationMap_["network-2"] = 999;
191     notifier.notificationMap_["network-3"] = 333;
192 
193     int32_t ret = notifier.DestroyNotifyByNotificationId(666);
194     EXPECT_EQ(ret, ERR_OK);
195 
196     GTEST_LOG_(INFO) << "CancelNotifyByNotificationId_001 end";
197 }
198 
199 /**
200  * @tc.name: SystemNotifierTest_CancelNotifyByNetworkId_002
201  * @tc.desc: Verify CancelNotifyByNetworkId handles non-existent networkId.
202  * @tc.type: FUNC
203  * @tc.require: I7TDJK
204  */
205 HWTEST_F(SystemNotifierTest, CancelNotifyByNetworkId_002, TestSize.Level1)
206 {
207     GTEST_LOG_(INFO) << "CancelNotifyByNetworkId_002 start";
208 
209     auto &notifier = SystemNotifier::GetInstance();
210     int32_t ret = notifier.DestroyNotifyByNetworkId("non-existent-network-id");
211     EXPECT_EQ(ret, ERR_BAD_VALUE);
212 
213     GTEST_LOG_(INFO) << "CancelNotifyByNetworkId_002 end";
214 }
215 
216 /**
217  * @tc.name: SystemNotifierTest_CancelNotifyByNotificationId_002
218  * @tc.desc: Verify CancelNotifyByNotificationId handles non-existent notificationId.
219  * @tc.type: FUNC
220  * @tc.require: I7TDJK
221  */
222 HWTEST_F(SystemNotifierTest, CancelNotifyByNotificationId_002, TestSize.Level1)
223 {
224     GTEST_LOG_(INFO) << "CancelNotifyByNotificationId_002 start";
225 
226     g_cancelNotification = ERR_BAD_VALUE;
227     auto &notifier = SystemNotifier::GetInstance();
228     int32_t ret = notifier.DestroyNotifyByNotificationId(9999);  // Non-existent ID
229     EXPECT_EQ(ret, ERR_OK);
230 
231     GTEST_LOG_(INFO) << "CancelNotifyByNotificationId_002 end";
232 }
233 
234 /**
235  * @tc.name: SystemNotifierTest_NotificationLifecycle_001
236  * @tc.desc: Verify complete notification lifecycle.
237  * @tc.type: FUNC
238  * @tc.require: I7TDJK
239  */
240 HWTEST_F(SystemNotifierTest, NotificationLifecycle_001, TestSize.Level1)
241 {
242     GTEST_LOG_(INFO) << "NotificationLifecycle_001 start";
243 
244     auto &notifier = SystemNotifier::GetInstance();
245     int32_t ret = notifier.CreateNotification(testNetworkId);
246     EXPECT_EQ(ret, ERR_OK);
247 
248     ret = notifier.DestroyNotifyByNetworkId(testNetworkId);
249     EXPECT_EQ(ret, ERR_OK);
250 
251     GTEST_LOG_(INFO) << "NotificationLifecycle_001 end";
252 }
253 
254 /**
255  * @tc.name: SystemNotifierTest_MultipleNotifications_001
256  * @tc.desc: Verify handling of multiple notifications.
257  * @tc.type: FUNC
258  * @tc.require: I7TDJK
259  */
260 HWTEST_F(SystemNotifierTest, MultipleNotifications_001, TestSize.Level1)
261 {
262     GTEST_LOG_(INFO) << "MultipleNotifications_001 start";
263 
264     std::string networkId1 = "network-id-1";
265     std::string networkId2 = "network-id-2";
266 
267     auto &notifier = SystemNotifier::GetInstance();
268     int32_t ret = notifier.CreateNotification(networkId1);
269     EXPECT_EQ(ret, ERR_OK);
270     ret = notifier.CreateNotification(networkId2);
271     EXPECT_EQ(ret, ERR_OK);
272 
273     ret = notifier.DestroyNotifyByNetworkId(networkId2);
274     EXPECT_EQ(ret, ERR_OK);
275     ret = notifier.DestroyNotifyByNetworkId(networkId1);
276     EXPECT_EQ(ret, ERR_OK);
277 
278     GTEST_LOG_(INFO) << "MultipleNotifications_001 end";
279 }
280 
281 /**
282  * @tc.name: SystemNotifierTest_GetPixelMap_001
283  * @tc.desc: Verify GetPixelMap.
284  * @tc.type: FUNC
285  * @tc.require: I7TDJK
286  */
287 HWTEST_F(SystemNotifierTest, GetPixelMap_001, TestSize.Level1)
288 {
289     GTEST_LOG_(INFO) << "GetPixelMap_001 start";
290 
291     auto &notifier = SystemNotifier::GetInstance();
292     std::shared_ptr<Media::PixelMap> nullPixelMap = nullptr;
293     auto ret = notifier.GetPixelMap("testPath", nullPixelMap);
294     EXPECT_EQ(ret, false);
295 
296     std::shared_ptr<Media::PixelMap> tempPixelMap = std::make_shared<Media::PixelMap>();
297     auto ret2 = notifier.GetPixelMap("testPath/testPath", tempPixelMap);
298     EXPECT_EQ(ret2, false);
299 
300     auto ret3 = notifier.GetPixelMap(
301         "/system/etc/dfs_service/resources/icon/capsule_icon.png", nullPixelMap);
302     EXPECT_EQ(ret3, true);
303     GTEST_LOG_(INFO) << "GetPixelMap_001 end";
304 }
305 
306 /**
307  * @tc.name: SystemNotifierTest_UpdateResourceMap_001
308  * @tc.desc: Verify UpdateResourceMap.
309  * @tc.type: FUNC
310  * @tc.require: I7TDJK
311  */
312 HWTEST_F(SystemNotifierTest, UpdateResourceMap_001, TestSize.Level1)
313 {
314     GTEST_LOG_(INFO) << "UpdateResourceMap_001 start";
315 
316     auto &notifier = SystemNotifier::GetInstance();
317     g_loadStringFromFile = "";
318     EXPECT_NO_THROW(notifier.UpdateResourceMap("/system/etc/dfs_service/resources/i18/xxx.json"));
319 
320     g_loadStringFromFile = "{\"key-value_no\":[{\"key\":\"notification_title\",\"value\":\"文件桥\"},"
321     "{\"key\":\"notification_text\",\"value\":\"可在 {} 上管理本机文件\"},"
322     "{\"key\":\"capsule_title\",\"value\":\"已连接\"},"
323     "{\"key\":\"button_name\",\"value\":\"断开\"}]}";
324     EXPECT_NO_THROW(notifier.UpdateResourceMap("/system/etc/dfs_service/resources/i18/xxx.json"));
325 
326     g_loadStringFromFile = "{\"key-value\":[{\"key_no\":\"notification_title\",\"value\":\"文件桥\"},"
327     "{\"key\":\"notification_text\",\"value\":\"可在 {} 上管理本机文件\"},"
328     "{\"key\":\"capsule_title\",\"value\":\"已连接\"},"
329     "{\"key\":\"button_name\",\"value\":\"断开\"}]}";
330     EXPECT_NO_THROW(notifier.UpdateResourceMap("/system/etc/dfs_service/resources/i18/xxx.json"));
331 
332     g_loadStringFromFile = "{\"key-value\":[{\"key\":\"notification_title\",\"value_no\":\"文件桥\"},"
333     "{\"key\":\"notification_text\",\"value\":\"可在 {} 上管理本机文件\"},"
334     "{\"key\":\"capsule_title\",\"value\":\"已连接\"},"
335     "{\"key\":\"button_name\",\"value\":\"断开\"}]}";
336     EXPECT_NO_THROW(notifier.UpdateResourceMap("/system/etc/dfs_service/resources/i18/xxx.json"));
337 
338     g_loadStringFromFile = "{\"key-value\":[{\"key\":\"notification_title\",\"value\":\"文件桥\"}]}";
339     EXPECT_NO_THROW(notifier.UpdateResourceMap("/system/etc/dfs_service/resources/i18/xxx.json"));
340 
341     g_loadStringFromFile = "{\"key-value\":[{\"key\":notification_title,\"value\":文件桥}]}";
342     EXPECT_NO_THROW(notifier.UpdateResourceMap("/system/etc/dfs_service/resources/i18/xxx.json"));
343 
344     GTEST_LOG_(INFO) << "UpdateResourceMap_001 end";
345 }
346 
347 /**
348  * @tc.name: SystemNotifierTest_GetKeyValue_001
349  * @tc.desc: Verify getKeyValue.
350  * @tc.type: FUNC
351  * @tc.require: I7TDJK
352  */
353 HWTEST_F(SystemNotifierTest, GetKeyValue_001, TestSize.Level1)
354 {
355     GTEST_LOG_(INFO) << "GetKeyValue_001 start";
356 
357     auto &notifier = SystemNotifier::GetInstance();
358     EXPECT_EQ(notifier.GetKeyValue("no_such_key"), "");
359 
360     GTEST_LOG_(INFO) << "GetKeyValue_001 end";
361 }
362 
363 }  // namespace Test
364 }  // namespace DistributedFile
365 }  // namespace Storage
366 }  // namespace OHOS