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 18 #define private public 19 #define protected public 20 #include "notification.h" 21 #undef private 22 #undef protected 23 24 #include "notification_request.h" 25 #include "parcel.h" 26 27 using namespace testing::ext; 28 namespace OHOS { 29 namespace Notification { 30 class NotificationTest : public testing::Test { 31 public: SetUpTestCase()32 static void SetUpTestCase() {} TearDownTestCase()33 static void TearDownTestCase() {} SetUp()34 void SetUp() {} TearDown()35 void TearDown() {} 36 }; 37 38 /** 39 * @tc.name: GetBundleName_00001 40 * @tc.desc: Test when request_ is nullptr get parameters. 41 * @tc.type: FUNC 42 * @tc.require: issueI5WBBH 43 */ 44 HWTEST_F(NotificationTest, GetBundleName_00001, Function | SmallTest | Level1) 45 { 46 sptr<NotificationRequest> request = nullptr; 47 auto rrc = std::make_shared<Notification>(request); 48 std::string ret = ""; 49 EXPECT_EQ(rrc->GetBundleName(), ret); 50 EXPECT_EQ(rrc->GetCreateBundle(), ret); 51 EXPECT_EQ(rrc->GetLabel(), ret); 52 EXPECT_EQ(rrc->GetId(), -1); 53 EXPECT_EQ(rrc->GetUid(), 0); 54 EXPECT_EQ(rrc->GetPid(), 0); 55 EXPECT_EQ(rrc->IsUnremovable(), false); 56 EXPECT_EQ(rrc->IsGroup(), false); 57 EXPECT_EQ(rrc->IsFloatingIcon(), false); 58 EXPECT_EQ(rrc->GetUserId(), 0); 59 } 60 61 /** 62 * @tc.name: GetLedLightColor_00001 63 * @tc.desc: Test GetLedLightColor parameters. 64 * @tc.type: FUNC 65 * @tc.require: issueI5WBBH 66 */ 67 HWTEST_F(NotificationTest, GetLedLightColor_00001, Function | SmallTest | Level1) 68 { 69 int32_t color = 10; 70 std::string deviceId = "DeviceId"; 71 sptr<NotificationRequest> request = nullptr; 72 auto rrc = std::make_shared<Notification>(deviceId, request); 73 rrc->SetLedLightColor(color); 74 EXPECT_EQ(rrc->GetLedLightColor(), color); 75 } 76 77 /** 78 * @tc.name: GetLockscreenVisibleness_00001 79 * @tc.desc: Test GetLockscreenVisibleness parameters. 80 * @tc.type: FUNC 81 * @tc.require: issueI5WBBH 82 */ 83 HWTEST_F(NotificationTest, GetLockscreenVisibleness_00001, Function | SmallTest | Level1) 84 { 85 NotificationConstant::VisiblenessType visbleness = NotificationConstant::VisiblenessType::PUBLIC; 86 sptr<NotificationRequest> request = nullptr; 87 auto rrc = std::make_shared<Notification>(request); 88 rrc->SetLockScreenVisbleness(visbleness); 89 EXPECT_EQ(rrc->GetLockscreenVisibleness(), visbleness); 90 } 91 92 /** 93 * @tc.name: GetGroup_00001 94 * @tc.desc: Test GetGroup parameters. 95 * @tc.type: FUNC 96 * @tc.require: issue 97 */ 98 HWTEST_F(NotificationTest, GetGroup_00001, Function | SmallTest | Level1) 99 { 100 sptr<NotificationRequest> request = nullptr; 101 auto rrc = std::make_shared<Notification>(request); 102 std::string ret = ""; 103 EXPECT_EQ(rrc->GetGroup(), ret); 104 } 105 106 /** 107 * @tc.name: GetGroup_00002 108 * @tc.desc: Test when request_ is not nullptr get parameters. 109 * @tc.type: FUNC 110 * @tc.require: issue 111 */ 112 HWTEST_F(NotificationTest, GetGroup_00002, Function | SmallTest | Level1) 113 { 114 sptr<NotificationRequest> request = new NotificationRequest(1); 115 auto rrc = std::make_shared<Notification>(request); 116 std::string ret = ""; 117 EXPECT_EQ(rrc->GetGroup(), ret); 118 EXPECT_EQ(rrc->GetPid(), 0); 119 EXPECT_EQ(rrc->IsUnremovable(), false); 120 EXPECT_EQ(rrc->IsGroup(), false); 121 EXPECT_EQ(rrc->IsFloatingIcon(), false); 122 } 123 124 /** 125 * @tc.name: GetPostTime_00001 126 * @tc.desc: Test GetPostTime parameters. 127 * @tc.type: FUNC 128 * @tc.require: issue 129 */ 130 HWTEST_F(NotificationTest, GetPostTime_00001, Function | SmallTest | Level1) 131 { 132 int64_t time = 10; 133 sptr<NotificationRequest> request = nullptr; 134 auto rrc = std::make_shared<Notification>(request); 135 rrc->SetPostTime(time); 136 EXPECT_EQ(rrc->GetPostTime(), time); 137 } 138 139 /** 140 * @tc.name: GetSound_00001 141 * @tc.desc: Test GetSound parameters. 142 * @tc.type: FUNC 143 * @tc.require: issue 144 */ 145 HWTEST_F(NotificationTest, GetSound_00001, Function | SmallTest | Level1) 146 { 147 Uri sound = Uri("sound"); 148 bool enable = true; 149 sptr<NotificationRequest> request = nullptr; 150 auto rrc = std::make_shared<Notification>(request); 151 rrc->SetSound(sound); 152 rrc->SetEnableSound(enable); 153 EXPECT_EQ(rrc->GetSound(), sound); 154 } 155 156 /** 157 * @tc.name: GetVibrationStyle_00001 158 * @tc.desc: Test GetVibrationStyle parameters. 159 * @tc.type: FUNC 160 * @tc.require: issue 161 */ 162 HWTEST_F(NotificationTest, GetVibrationStyle_00001, Function | SmallTest | Level1) 163 { 164 std::vector<int64_t> style; 165 sptr<NotificationRequest> request = nullptr; 166 auto rrc = std::make_shared<Notification>(request); 167 rrc->SetVibrationStyle(style); 168 EXPECT_EQ(rrc->GetVibrationStyle(), style); 169 } 170 171 /** 172 * @tc.name: GetRemindType_00001 173 * @tc.desc: Test GetRemindType parameters. 174 * @tc.type: FUNC 175 * @tc.require: issue 176 */ 177 HWTEST_F(NotificationTest, GetRemindType_00001, Function | SmallTest | Level1) 178 { 179 NotificationConstant::RemindType reminType = NotificationConstant::RemindType::NONE; 180 sptr<NotificationRequest> request = nullptr; 181 auto rrc = std::make_shared<Notification>(request); 182 rrc->SetRemindType(reminType); 183 EXPECT_EQ(rrc->GetRemindType(), reminType); 184 } 185 186 /** 187 * @tc.name: GenerateNotificationKey_00001 188 * @tc.desc: Test GenerateNotificationKey parameters. 189 * @tc.type: FUNC 190 * @tc.require: issue 191 */ 192 HWTEST_F(NotificationTest, GenerateNotificationKey_00001, Function | SmallTest | Level1) 193 { 194 std::string deviceId = "DeviceId"; 195 int32_t userId = 10; 196 int32_t uid = 20; 197 std::string label = "Lable"; 198 int32_t id = 30; 199 sptr<NotificationRequest> request = nullptr; 200 auto rrc = std::make_shared<Notification>(deviceId, request); 201 std::string result = "DeviceId_10_20_Lable_30"; 202 EXPECT_EQ(rrc->GenerateNotificationKey(deviceId, userId, uid, label, id), result); 203 } 204 205 /** 206 * @tc.name: IsRemoveAllowed_00001 207 * @tc.desc: Test IsRemoveAllowed parameters. 208 * @tc.type: FUNC 209 * @tc.require: issue 210 */ 211 HWTEST_F(NotificationTest, IsRemoveAllowed_00001, Function | SmallTest | Level1) 212 { 213 bool removeAllowed = true; 214 sptr<NotificationRequest> request = nullptr; 215 auto rrc = std::make_shared<Notification>(request); 216 rrc->SetRemoveAllowed(removeAllowed); 217 EXPECT_EQ(rrc->IsRemoveAllowed(), removeAllowed); 218 } 219 220 /** 221 * @tc.name: GetSourceType_00001 222 * @tc.desc: Test GetSourceType parameters. 223 * @tc.type: FUNC 224 * @tc.require: issue 225 */ 226 HWTEST_F(NotificationTest, GetSourceType_00001, Function | SmallTest | Level1) 227 { 228 NotificationConstant::SourceType sourceType = NotificationConstant::SourceType::TYPE_NORMAL; 229 sptr<NotificationRequest> request = nullptr; 230 auto rrc = std::make_shared<Notification>(request); 231 rrc->SetSourceType(sourceType); 232 EXPECT_EQ(rrc->GetSourceType(), sourceType); 233 } 234 235 /** 236 * @tc.name: GetDeviceId_00001 237 * @tc.desc: Test GetDeviceId parameters. 238 * @tc.type: FUNC 239 * @tc.require: issue 240 */ 241 HWTEST_F(NotificationTest, GetDeviceId_00001, Function | SmallTest | Level1) 242 { 243 std::string deviceId = "DeviceId"; 244 sptr<NotificationRequest> request = new NotificationRequest(); 245 auto rrc = std::make_shared<Notification>(deviceId, request); 246 EXPECT_EQ(rrc->GetDeviceId(), deviceId); 247 } 248 249 /** 250 * @tc.name: Dump_00001 251 * @tc.desc: Test Dump parameters. 252 * @tc.type: FUNC 253 * @tc.require: issue 254 */ 255 HWTEST_F(NotificationTest, Dump_00001, Function | SmallTest | Level1) 256 { 257 std::string deviceId = "DeviceId"; 258 sptr<NotificationRequest> request = new NotificationRequest(); 259 auto rrc = std::make_shared<Notification>(deviceId, request); 260 std::string ret = "Notification{ key = DeviceId_-1_0__0, ledLightColor = 0, " 261 "lockscreenVisbleness = 0, remindType = -1, isRemoveAllowed = true, sourceType = 0, " 262 "deviceId = DeviceId, request = NotificationRequest{ notificationId = 0, " 263 "slotType = 3, createTime = 0, deliveryTime = 0, autoDeletedTime = 0, settingsText = , " 264 "creatorBundleName = , creatorPid = 0, creatorUid = 0, ownerBundleName = , " 265 "ownerUid = 0, groupName = , statusBarText = , label = , shortcutId = , " 266 "sortingKey = , groupAlertType = 0, color = 0, badgeNumber = 0, visiblenessType = 0, " 267 "progressValue = 0, progressMax = 0, badgeStyle = 0, classification = , " 268 "notificationContentType = 0, showDeliveryTime = false, tapDismissed = true, " 269 "colorEnabled = false, alertOneTime = false, showStopwatch = false, isCountdown = false, " 270 "inProgress = false, groupOverview = false, isRemoveAllowed = true, progressIndeterminate = false, " 271 "unremovable = false, floatingIcon = false, onlyLocal = false, permitted = true, " 272 "isAgent = false, removalWantAgent = null, maxScreenWantAgent = null, additionalParams = null, " 273 "littleIcon = null, bigIcon = null, notificationContent = null, " 274 "notificationTemplate = null, actionButtons = empty, messageUsers = empty, " 275 "userInputHistory = empty, distributedOptions = NotificationDistributedOptions" 276 "{ isDistributed = true, devicesSupportDisplay = [], devicesSupportOperate = [] }, " 277 "notificationFlags = null, creatorUserId = -1, ownerUserId = -1, receiverUserId = -1 }, " 278 "postTime = 0, sound = nullptr, vibrationStyle = [] }"; 279 EXPECT_EQ(rrc->Dump(), ret); 280 } 281 282 /** 283 * @tc.name: MarshallingBool_00001 284 * @tc.desc: Test MarshallingBool parameters. 285 * @tc.type: FUNC 286 * @tc.require: issue 287 */ 288 HWTEST_F(NotificationTest, MarshallingBool_00001, Function | SmallTest | Level1) 289 { 290 Parcel parcel; 291 std::string deviceId = "DeviceId"; 292 sptr<NotificationRequest> request = new NotificationRequest(); 293 auto rrc = std::make_shared<Notification>(deviceId, request); 294 EXPECT_EQ(rrc->MarshallingBool(parcel), true); 295 } 296 297 /** 298 * @tc.name: Marshalling_00001 299 * @tc.desc: Test Marshalling parameters. 300 * @tc.type: FUNC 301 * @tc.require: issueI5WBBHI 302 */ 303 HWTEST_F(NotificationTest, Marshalling_00001, Function | SmallTest | Level1) 304 { 305 Parcel parcel; 306 std::string deviceId = "DeviceId"; 307 sptr<NotificationRequest> request = new NotificationRequest(); 308 auto rrc = std::make_shared<Notification>(deviceId, request); 309 EXPECT_EQ(rrc->Marshalling(parcel), true); 310 } 311 312 /** 313 * @tc.name: Unmarshalling_00001 314 * @tc.desc: Test Unmarshalling parameters. 315 * @tc.type: FUNC 316 * @tc.require: issueI5WBBH 317 */ 318 HWTEST_F(NotificationTest, Unmarshalling_001, Function | SmallTest | Level1) 319 { 320 bool unmarshalling = true; 321 Parcel parcel; 322 std::string deviceId = "DeviceId"; 323 sptr<NotificationRequest> request = new NotificationRequest(); 324 std::shared_ptr<Notification> result = 325 std::make_shared<Notification>(deviceId, request); 326 327 if (nullptr != result) { 328 if (nullptr == result->Unmarshalling(parcel)) { 329 unmarshalling = false; 330 } 331 } 332 EXPECT_EQ(unmarshalling, true); 333 } 334 335 /** 336 * @tc.name: ReadFromParcel_00001 337 * @tc.desc: Test ReadFromParcel parameters. 338 * @tc.type: FUNC 339 * @tc.require: issueI5WBBH 340 */ 341 HWTEST_F(NotificationTest, ReadFromParcel_00001, Function | SmallTest | Level1) 342 { 343 Parcel parcel; 344 std::string deviceId = "DeviceId"; 345 sptr<NotificationRequest> request = new NotificationRequest(); 346 auto rrc = std::make_shared<Notification>(deviceId, request); 347 EXPECT_EQ(rrc->ReadFromParcel(parcel), true); 348 } 349 350 /** 351 * @tc.name: GetSound_00002 352 * @tc.desc: Test GetSound parameters. 353 * @tc.type: FUNC 354 * @tc.require: issue 355 */ 356 HWTEST_F(NotificationTest, GetSound_00002, Function | SmallTest | Level1) 357 { 358 Uri sound = Uri("sound"); 359 bool enable = false; 360 sptr<NotificationRequest> request = nullptr; 361 auto rrc = std::make_shared<Notification>(request); 362 rrc->SetSound(sound); 363 rrc->SetEnableSound(enable); 364 EXPECT_EQ(rrc->GetSound(), Uri("")); 365 } 366 367 /** 368 * @tc.name: GetSound_00003 369 * @tc.desc: Test GetSound parameters. 370 * @tc.type: FUNC 371 * @tc.require: issue 372 */ 373 HWTEST_F(NotificationTest, GetSound_00003, Function | SmallTest | Level1) 374 { 375 Uri sound = Uri(""); 376 bool enable = true; 377 sptr<NotificationRequest> request = nullptr; 378 auto rrc = std::make_shared<Notification>(request); 379 rrc->SetSound(sound); 380 rrc->SetEnableSound(enable); 381 EXPECT_EQ(rrc->GetSound(), Uri("")); 382 } 383 384 /** 385 * @tc.name: EnableLight_00001 386 * @tc.desc: Test EnableLight parameters. 387 * @tc.type: FUNC 388 * @tc.require: issue 389 */ 390 HWTEST_F(NotificationTest, EnableLight_00001, Function | SmallTest | Level1) 391 { 392 bool enable = true; 393 sptr<NotificationRequest> request = nullptr; 394 auto rrc = std::make_shared<Notification>(request); 395 rrc->SetEnableLight(enable); 396 EXPECT_EQ(rrc->EnableLight(), enable); 397 } 398 399 /** 400 * @tc.name: EnableSound_00001 401 * @tc.desc: Test EnableSound parameters. 402 * @tc.type: FUNC 403 * @tc.require: issue 404 */ 405 HWTEST_F(NotificationTest, EnableSound_00001, Function | SmallTest | Level1) 406 { 407 bool enable = true; 408 sptr<NotificationRequest> request = nullptr; 409 auto rrc = std::make_shared<Notification>(request); 410 rrc->SetEnableSound(enable); 411 EXPECT_EQ(rrc->EnableSound(), enable); 412 Parcel parcel; 413 rrc->ReadFromParcelString(parcel); 414 } 415 416 /** 417 * @tc.name: EnableVibrate_00001 418 * @tc.desc: Test EnableVibrate parameters. 419 * @tc.type: FUNC 420 * @tc.require: issue 421 */ 422 HWTEST_F(NotificationTest, EnableVibrate_00001, Function | SmallTest | Level1) 423 { 424 bool enable = true; 425 sptr<NotificationRequest> request = nullptr; 426 auto rrc = std::make_shared<Notification>(request); 427 rrc->SetEnableVibration(enable); 428 EXPECT_EQ(rrc->EnableVibrate(), enable); 429 } 430 431 /** 432 * @tc.name: GetBundleName_00002 433 * @tc.desc: Test when request_ is nullptr get parameters. 434 * @tc.type: FUNC 435 * @tc.require: issueI5WBBH 436 */ 437 HWTEST_F(NotificationTest, GetBundleName_00002, Function | SmallTest | Level1) 438 { 439 sptr<NotificationRequest> request = new NotificationRequest(1); 440 auto rrc = std::make_shared<Notification>(request); 441 std::string ret = ""; 442 EXPECT_EQ(rrc->GetBundleName(), ret); 443 EXPECT_EQ(rrc->GetCreateBundle(), ret); 444 } 445 446 /** 447 * @tc.name: GetSound_00004 448 * @tc.desc: Test GetSound parameters. 449 * @tc.type: FUNC 450 * @tc.require: issue 451 */ 452 HWTEST_F(NotificationTest, GetSound_00004, Function | SmallTest | Level1) 453 { 454 Uri sound = Uri("sound"); 455 bool enable = false; 456 sptr<NotificationRequest> request = new NotificationRequest(1); 457 auto rrc = std::make_shared<Notification>(request); 458 rrc->SetSound(sound); 459 rrc->SetEnableSound(enable); 460 EXPECT_EQ(rrc->GetSound(), Uri("")); 461 } 462 463 /** 464 * @tc.name: GetSound_00005 465 * @tc.desc: Test GetSound parameters. 466 * @tc.type: FUNC 467 * @tc.require: issue 468 */ 469 HWTEST_F(NotificationTest, GetSound_00005, Function | SmallTest | Level1) 470 { 471 Uri sound = Uri("sound"); 472 bool enable = true; 473 sptr<NotificationRequest> request = new NotificationRequest(1); 474 auto rrc = std::make_shared<Notification>(request); 475 rrc->SetSound(sound); 476 rrc->SetEnableSound(enable); 477 EXPECT_EQ(rrc->GetSound(), Uri("sound")); 478 } 479 480 /** 481 * @tc.name: Marshalling_00002 482 * @tc.desc: Test Marshalling parameters. 483 * @tc.type: FUNC 484 * @tc.require: issueI5WBBHI 485 */ 486 HWTEST_F(NotificationTest, Marshalling_00002, Function | SmallTest | Level1) 487 { 488 Parcel parcel; 489 std::string deviceId = "DeviceId"; 490 sptr<NotificationRequest> request = new NotificationRequest(); 491 auto rrc = std::make_shared<Notification>(deviceId, request); 492 493 bool enable = true; 494 auto sound = std::make_shared<Uri>("sound"); 495 rrc->SetSound(*sound); 496 rrc->SetEnableSound(enable); 497 498 EXPECT_EQ(rrc->Marshalling(parcel), true); 499 } 500 501 /** 502 * @tc.name: Marshalling_00003 503 * @tc.desc: Test Marshalling parameters. 504 * @tc.type: FUNC 505 * @tc.require: issueI5WBBHI 506 */ 507 HWTEST_F(NotificationTest, Marshalling_00003, Function | SmallTest | Level1) 508 { 509 Parcel parcel; 510 std::string deviceId = "DeviceId"; 511 sptr<NotificationRequest> request = new NotificationRequest(); 512 auto rrc = std::make_shared<Notification>(deviceId, request); 513 514 bool enable = false; 515 auto sound = std::make_shared<Uri>("sound"); 516 rrc->SetSound(*sound); 517 rrc->SetEnableSound(enable); 518 519 EXPECT_EQ(rrc->Marshalling(parcel), true); 520 } 521 } 522 }