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 <gtest/gtest.h>
17 #include <numeric>
18 #include "event_report.h"
19 #include "notification_constant.h"
20 #include "notification_content.h"
21 #define private public
22 #define protected public
23 #include "notification.h"
24 #undef private
25 #undef protected
26
27 namespace OHOS {
28 namespace Notification {
29 using namespace testing::ext;
30
31 namespace {
32 const std::string TEST_CREATER_BUNDLE_NAME = "creater";
33 const std::string TEST_BUNDLE_OPTION_BUNDLE_NAME = "bundleName";
34 const std::string TEST_NOTIFICATION_LABEL = "notificationLabel";
35 constexpr int32_t TEST_NOTIFICATION_ID = 1;
36 constexpr int32_t TEST_BUNDLE_OPTION_UID = 100;
37 constexpr int32_t TEST_USER_ID = 1000;
38 constexpr int32_t TEST_ERROR_CODE = 22;
39 } // namespace
40
41 class NotificationHisyseventTest : public testing::Test {
42 public:
NotificationHisyseventTest()43 NotificationHisyseventTest()
44 {}
~NotificationHisyseventTest()45 ~NotificationHisyseventTest()
46 {}
47
48 static void SetUpTestCase(void);
49 static void TearDownTestCase(void);
50 void SetUp();
51 void TearDown();
52 };
53
SetUpTestCase(void)54 void NotificationHisyseventTest::SetUpTestCase(void)
55 {}
56
TearDownTestCase(void)57 void NotificationHisyseventTest::TearDownTestCase(void)
58 {}
59
SetUp(void)60 void NotificationHisyseventTest::SetUp(void)
61 {}
62
TearDown(void)63 void NotificationHisyseventTest::TearDown(void)
64 {}
65
66 /**
67 * @tc.name: SendSubscriberErrorSysEvent_0100
68 * @tc.desc: Send "SUBSCRIBE_ERROR" hisysevent.
69 * @tc.type: FUNC
70 * @tc.require: I582Y4
71 */
HWTEST_F(NotificationHisyseventTest,SendSubscriberErrorSysEvent_0100,Level1)72 HWTEST_F(NotificationHisyseventTest, SendSubscriberErrorSysEvent_0100, Level1)
73 {
74 GTEST_LOG_(INFO) << "SendSubscriberErrorSysEvent_0100 start";
75
76 EventInfo eventInfo;
77 eventInfo.pid = getpid();
78 eventInfo.uid = getuid();
79 eventInfo.errCode = TEST_ERROR_CODE;
80 EventReport::SendHiSysEvent(SUBSCRIBE_ERROR, eventInfo);
81
82 eventInfo.userId = TEST_USER_ID;
83 std::vector<std::string> appNames = {"app1_1", "app1_2", "app1_3"};
84 eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""),
85 [appNames](std::string bundleName, const std::string &str) {
86 return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str);
87 });
88 EventReport::SendHiSysEvent(SUBSCRIBE_ERROR, eventInfo);
89 std::string deviceId = "123";
90 sptr<NotificationRequest> request = nullptr;
91 Notification notificationTest(deviceId, request);
92 auto result = notificationTest.GetDeviceId();
93 ASSERT_EQ(result, deviceId);
94
95 GTEST_LOG_(INFO) << "SendSubscriberErrorSysEvent_0100 end";
96 }
97
98 /**
99 * @tc.name: SendEnableNotificationErrorSysEvent_0100
100 * @tc.desc: Send "ENABLE_NOTIFICATION_ERROR" hisysevent.
101 * @tc.type: FUNC
102 * @tc.require: I582Y4
103 */
HWTEST_F(NotificationHisyseventTest,SendEnableNotificationErrorSysEvent_0100,Level1)104 HWTEST_F(NotificationHisyseventTest, SendEnableNotificationErrorSysEvent_0100, Level1)
105 {
106 GTEST_LOG_(INFO) << "SendEnableNotificationErrorSysEvent_0100 start";
107
108 EventInfo eventInfo;
109 eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME;
110 eventInfo.uid = getuid();
111 eventInfo.enable = true;
112 eventInfo.errCode = TEST_ERROR_CODE;
113 EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_ERROR, eventInfo);
114 std::string deviceId = "123";
115 sptr<NotificationRequest> request = nullptr;
116 int32_t notificationId =5;
117 Notification notificationTest(deviceId, request);
118 NotificationRequest notificationRequest(notificationId);
119 pid_t myPid = notificationRequest.GetCreatorPid();
120 notificationRequest.SetCreatorPid(myPid);
121 auto result = notificationTest.GetPid();
122 ASSERT_EQ(result, myPid);
123
124 GTEST_LOG_(INFO) << "SendEnableNotificationErrorSysEvent_0100 end";
125 }
126
127 /**
128 * @tc.name: SendEnableNotificationSlotErrorSysEvent_0100
129 * @tc.desc: Send "ENABLE_NOTIFICATION_SLOT_ERROR" hisysevent.
130 * @tc.type: FUNC
131 * @tc.require: I582Y4
132 */
HWTEST_F(NotificationHisyseventTest,SendEnableNotificationSlotErrorSysEvent_0100,Level1)133 HWTEST_F(NotificationHisyseventTest, SendEnableNotificationSlotErrorSysEvent_0100, Level1)
134 {
135 GTEST_LOG_(INFO) << "SendEnableNotificationSlotErrorSysEvent_0100 start";
136
137 EventInfo eventInfo;
138 eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME;
139 eventInfo.uid = getuid();
140 eventInfo.enable = false;
141 eventInfo.slotType = NotificationConstant::SERVICE_REMINDER;
142 eventInfo.errCode = TEST_ERROR_CODE;
143 EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT_ERROR, eventInfo);
144 std::string deviceId = "123";
145 sptr<NotificationRequest> request = nullptr;
146 Notification notificationTest(deviceId, request);
147 auto result = notificationTest.IsUnremovable();
148 ASSERT_EQ(result, false);
149
150 GTEST_LOG_(INFO) << "SendEnableNotificationSlotErrorSysEvent_0100 end";
151 }
152
153 /**
154 * @tc.name: SendPublishErrorSysEvent_0100
155 * @tc.desc: Send "SUBSCRIBE_ERROR" hisysevent.
156 * @tc.type: FUNC
157 * @tc.require: I582Y4
158 */
HWTEST_F(NotificationHisyseventTest,SendPublishErrorSysEvent_0100,Level1)159 HWTEST_F(NotificationHisyseventTest, SendPublishErrorSysEvent_0100, Level1)
160 {
161 GTEST_LOG_(INFO) << "SendPublishErrorSysEvent_0100 start";
162
163 EventInfo eventInfo;
164 eventInfo.notificationId = TEST_NOTIFICATION_ID;
165 eventInfo.contentType = static_cast<int32_t>(NotificationContent::Type::LONG_TEXT);
166 eventInfo.bundleName = TEST_CREATER_BUNDLE_NAME;
167 eventInfo.userId = TEST_USER_ID;
168 eventInfo.errCode = TEST_ERROR_CODE;
169 EventReport::SendHiSysEvent(PUBLISH_ERROR, eventInfo);
170 std::string deviceId = "123";
171 sptr<NotificationRequest> request = nullptr;
172 Notification notificationTest(deviceId, request);
173 auto result = notificationTest.IsGroup();
174 ASSERT_EQ(result, false);
175
176 GTEST_LOG_(INFO) << "SendPublishErrorSysEvent_0100 end";
177 }
178
179 /**
180 * @tc.name: SendFlowControlOccurSysEvent_0100
181 * @tc.desc: Send "FLOW_CONTROL_OCCUR" hisysevent.
182 * @tc.type: FUNC
183 * @tc.require: I582Y4
184 */
HWTEST_F(NotificationHisyseventTest,SendFlowControlOccurSysEvent_0100,Level1)185 HWTEST_F(NotificationHisyseventTest, SendFlowControlOccurSysEvent_0100, Level1)
186 {
187 GTEST_LOG_(INFO) << "SendFlowControlOccurSysEvent_0100 start";
188
189 EventInfo eventInfo;
190 eventInfo.notificationId = TEST_NOTIFICATION_ID;
191 eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME;
192 eventInfo.uid = TEST_BUNDLE_OPTION_UID;
193 EventReport::SendHiSysEvent(FLOW_CONTROL_OCCUR, eventInfo);
194 std::string deviceId = "123";
195 sptr<NotificationRequest> request = nullptr;
196 Notification notificationTest(deviceId, request);
197 auto result = notificationTest.IsFloatingIcon();
198 ASSERT_EQ(result, false);
199
200 GTEST_LOG_(INFO) << "SendFlowControlOccurSysEvent_0100 end";
201 }
202
203 /**
204 * @tc.name: SendSubscribeSysEvent_0100
205 * @tc.desc: Send "SUBSCRIBE" hisysevent.
206 * @tc.type: FUNC
207 * @tc.require: I582Y4
208 */
HWTEST_F(NotificationHisyseventTest,SendSubscribeSysEvent_0100,Level1)209 HWTEST_F(NotificationHisyseventTest, SendSubscribeSysEvent_0100, Level1)
210 {
211 GTEST_LOG_(INFO) << "SendSubscribeSysEvent_0100 start";
212
213 EventInfo eventInfo;
214 eventInfo.pid = getpid();
215 eventInfo.uid = getuid();
216 EventReport::SendHiSysEvent(SUBSCRIBE, eventInfo);
217
218 eventInfo.userId = TEST_USER_ID;
219 std::vector<std::string> appNames = {"app1_1", "app1_2", "app1_3"};
220 eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""),
221 [appNames](std::string bundleName, const std::string &str) {
222 return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str);
223 });
224 EventReport::SendHiSysEvent(SUBSCRIBE, eventInfo);
225
226 std::vector<std::string> anotherBundle = {"app"};
227 eventInfo.bundleName = std::accumulate(anotherBundle.begin(), anotherBundle.end(), std::string(""),
228 [anotherBundle](std::string bundleName, const std::string &str) {
229 return (str == anotherBundle.front()) ? (bundleName + str) : (bundleName + "," + str);
230 });
231 EventReport::SendHiSysEvent(SUBSCRIBE, eventInfo);
232 std::string deviceId = "123";
233 sptr<NotificationRequest> request = nullptr;
234 Parcel p;
235 Notification notificationTest(deviceId, request);
236 auto result = notificationTest.MarshallingBool(p);
237 ASSERT_EQ(result, true);
238
239 GTEST_LOG_(INFO) << "SendSubscribeSysEvent_0100 end";
240 }
241
242 /**
243 * @tc.name: SendUnSubscribeSysEvent_0100
244 * @tc.desc: Send "UNSUBSCRIBE" hisysevent.
245 * @tc.type: FUNC
246 * @tc.require: I582Y4
247 */
HWTEST_F(NotificationHisyseventTest,SendUnSubscribeSysEvent_0100,Level1)248 HWTEST_F(NotificationHisyseventTest, SendUnSubscribeSysEvent_0100, Level1)
249 {
250 GTEST_LOG_(INFO) << "SendUnSubscribeSysEvent_0100 start";
251
252 EventInfo eventInfo;
253 eventInfo.pid = getpid();
254 eventInfo.uid = getuid();
255 eventInfo.userId = TEST_USER_ID;
256 std::vector<std::string> appNames = {"app1_1", "app1_2", "app1_3"};
257 eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""),
258 [appNames](std::string bundleName, const std::string &str) {
259 return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str);
260 });
261 EventReport::SendHiSysEvent(UNSUBSCRIBE, eventInfo);
262 std::string deviceId = "123";
263 sptr<NotificationRequest> request = nullptr;
264 Parcel p;
265 Notification notificationTest(deviceId, request);
266 auto result = notificationTest.MarshallingInt32(p);
267 ASSERT_EQ(result, true);
268
269 GTEST_LOG_(INFO) << "SendUnSubscribeSysEvent_0100 end";
270 }
271
272 /**
273 * @tc.name: SendEnableNotificationSysEvent_0100
274 * @tc.desc: Send "ENABLE_NOTIFICATION" hisysevent.
275 * @tc.type: FUNC
276 * @tc.require: I582Y4
277 */
HWTEST_F(NotificationHisyseventTest,SendEnableNotificationSysEvent_0100,Level1)278 HWTEST_F(NotificationHisyseventTest, SendEnableNotificationSysEvent_0100, Level1)
279 {
280 GTEST_LOG_(INFO) << "SendEnableNotificationSysEvent_0100 start";
281
282 EventInfo eventInfo;
283 eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME;
284 eventInfo.uid = getuid();
285 eventInfo.enable = true;
286 EventReport::SendHiSysEvent(ENABLE_NOTIFICATION, eventInfo);
287 std::string deviceId = "123";
288 sptr<NotificationRequest> request = nullptr;
289 Parcel p;
290 Notification notificationTest(deviceId, request);
291 auto result = notificationTest.MarshallingInt64(p);
292 ASSERT_EQ(result, true);
293
294 GTEST_LOG_(INFO) << "SendEnableNotificationSysEvent_0100 end";
295 }
296
297 /**
298 * @tc.name: SendEnableNotificationSlotSysEvent_0100
299 * @tc.desc: Send "ENABLE_NOTIFICATION_SLOT" hisysevent.
300 * @tc.type: FUNC
301 * @tc.require: I582Y4
302 */
HWTEST_F(NotificationHisyseventTest,SendEnableNotificationSlotSysEvent_0100,Level1)303 HWTEST_F(NotificationHisyseventTest, SendEnableNotificationSlotSysEvent_0100, Level1)
304 {
305 GTEST_LOG_(INFO) << "SendEnableNotificationSlotSysEvent_0100 start";
306
307 EventInfo eventInfo;
308 eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME;
309 eventInfo.uid = getuid();
310 eventInfo.enable = true;
311 eventInfo.slotType = NotificationConstant::CONTENT_INFORMATION;
312 EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT, eventInfo);
313 std::string deviceId = "123";
314 sptr<NotificationRequest> request = nullptr;
315 Parcel p;
316 Notification notificationTest(deviceId, request);
317 auto result = notificationTest.MarshallingParcelable(p);
318 ASSERT_EQ(result, true);
319
320 GTEST_LOG_(INFO) << "SendEnableNotificationSlotSysEvent_0100 end";
321 }
322
323 /**
324 * @tc.name: SendPublishSysEvent_0100
325 * @tc.desc: Send "PUBLISH" hisysevent.
326 * @tc.type: FUNC
327 * @tc.require: I582Y4
328 */
HWTEST_F(NotificationHisyseventTest,SendPublishSysEvent_0100,Level1)329 HWTEST_F(NotificationHisyseventTest, SendPublishSysEvent_0100, Level1)
330 {
331 GTEST_LOG_(INFO) << "SendPublishSysEvent_0100 start";
332
333 EventInfo eventInfo;
334 eventInfo.notificationId = TEST_NOTIFICATION_ID;
335 eventInfo.contentType = static_cast<int32_t>(NotificationContent::Type::LONG_TEXT);
336 eventInfo.bundleName = TEST_CREATER_BUNDLE_NAME;
337 eventInfo.userId = TEST_USER_ID;
338 EventReport::SendHiSysEvent(PUBLISH, eventInfo);
339 std::string deviceId = "123";
340 sptr<NotificationRequest> request = nullptr;
341 Parcel p;
342 Notification notificationTest(deviceId, request);
343 auto result = notificationTest.Marshalling(p);
344 ASSERT_EQ(result, true);
345
346 GTEST_LOG_(INFO) << "SendPublishSysEvent_0100 end";
347 }
348
349 /**
350 * @tc.name: SendCancelSysEvent_0100
351 * @tc.desc: Send "CANCEL" hisysevent.
352 * @tc.type: FUNC
353 * @tc.require: I582Y4
354 */
HWTEST_F(NotificationHisyseventTest,SendCancelSysEvent_0100,Level1)355 HWTEST_F(NotificationHisyseventTest, SendCancelSysEvent_0100, Level1)
356 {
357 GTEST_LOG_(INFO) << "SendCancelSysEvent_0100 start";
358
359 EventInfo eventInfo;
360 eventInfo.notificationId = TEST_NOTIFICATION_ID;
361 eventInfo.notificationLabel = TEST_NOTIFICATION_LABEL;
362 eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME;
363 eventInfo.uid = TEST_BUNDLE_OPTION_UID;
364 EventReport::SendHiSysEvent(CANCEL, eventInfo);
365 std::string deviceId = "123";
366 sptr<NotificationRequest> request = nullptr;
367 Parcel p;
368 Notification notificationTest(deviceId, request);
369 auto result = notificationTest.ReadFromParcel(p);
370 ASSERT_EQ(result, false);
371
372 GTEST_LOG_(INFO) << "SendCancelSysEvent_0100 end";
373 }
374
375 /**
376 * @tc.name: SendRemoveSysEvent_0100
377 * @tc.desc: Send "REMOVE" hisysevent.
378 * @tc.type: FUNC
379 * @tc.require: I582Y4
380 */
HWTEST_F(NotificationHisyseventTest,SendRemoveSysEvent_0100,Level1)381 HWTEST_F(NotificationHisyseventTest, SendRemoveSysEvent_0100, Level1)
382 {
383 GTEST_LOG_(INFO) << "SendRemoveSysEvent_0100 start";
384
385 EventInfo eventInfo;
386 eventInfo.notificationId = TEST_NOTIFICATION_ID;
387 eventInfo.notificationLabel = TEST_NOTIFICATION_LABEL;
388 eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME;
389 eventInfo.uid = TEST_BUNDLE_OPTION_UID;
390 EventReport::SendHiSysEvent(REMOVE, eventInfo);
391 std::string deviceId = "123";
392 sptr<NotificationRequest> request = nullptr;
393 std::vector<int64_t> style = {1};
394 Notification notificationTest(deviceId, request);
395 notificationTest.SetVibrationStyle(style);
396 auto result = notificationTest.GetVibrationStyle();
397 ASSERT_EQ(result, style);
398 EventReport::SendHiSysEvent("", eventInfo);
399
400 GTEST_LOG_(INFO) << "SendRemoveSysEvent_0100 end";
401 }
402 } // namespace Notification
403 } // namespace OHOS