diff --git a/frameworks/ans/test/unittest/BUILD.gn b/frameworks/ans/test/unittest/BUILD.gn index c9e5e05259ae3342f3d249ffb5bc199c4cd122fc..272d8e015eceb6b1a6b0ac020ac3da6269665b6f 100644 --- a/frameworks/ans/test/unittest/BUILD.gn +++ b/frameworks/ans/test/unittest/BUILD.gn @@ -32,6 +32,7 @@ ohos_unittest("ans_reminder_unit_test") { sources = [ "${frameworks_module_ans_path}/test/unittest/ans_log_test.cpp", "${frameworks_module_ans_path}/test/unittest/enabled_notification_callback_data_test.cpp", + "${frameworks_module_ans_path}/test/unittest/message_user_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_bundle_option_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_content_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_conversational_content_test.cpp", diff --git a/frameworks/ans/test/unittest/message_user_test.cpp b/frameworks/ans/test/unittest/message_user_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00f3658d87e1a80eb47eb0ccb3b7bfbaf0461930 --- /dev/null +++ b/frameworks/ans/test/unittest/message_user_test.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#define private public +#define protected public +#include "message_user.h" +#undef private +#undef protected + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class MessageUserTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +/** + * @tc.name: FromJson_00001 + * @tc.desc: Test FromJson parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(MessageUserTest, FromJson_00001, Function | SmallTest | Level1) +{ + sptr messageUser = nullptr; + nlohmann::json jsonObject; + EXPECT_EQ(messageUser ->FromJson(jsonObject), nullptr); +} + +/** + * @tc.name: Unmarshalling_00001 + * @tc.desc: Test Unmarshalling parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(MessageUserTest, Unmarshalling_001, Function | SmallTest | Level1) +{ + bool unmarshalling = true; + Parcel parcel; + std::shared_ptr result = std::make_shared(); + + if (nullptr != result) { + if (nullptr == result->Unmarshalling(parcel)) { + unmarshalling = false; + } + } + EXPECT_EQ(unmarshalling, false); +} + +/** + * @tc.name: ReadFromParcel_00001 + * @tc.desc: Test ReadFromParcel parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(MessageUserTest, ReadFromParcel_00001, Function | SmallTest | Level1) +{ + MessageUser messageUser; + Parcel parcel; + + EXPECT_EQ(messageUser.ReadFromParcel(parcel), false); +} + +/** + * @tc.name: ReadFromParcel_00002 + * @tc.desc: Test ReadFromParcel parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(MessageUserTest, ReadFromParcel_00002, Function | SmallTest | Level1) +{ + MessageUser messageUser; + Parcel parcel; + int32_t empty = 10; + + parcel.WriteInt32(empty); + EXPECT_EQ(messageUser.ReadFromParcel(parcel), false); +} +} +} \ No newline at end of file diff --git a/frameworks/ans/test/unittest/notification_helper_test.cpp b/frameworks/ans/test/unittest/notification_helper_test.cpp index 5dda006cbb89eed0ba24773afe29f73e47ee6297..ff5a0ec12a5d18521bd9d9c73f74f4e351fec770 100644 --- a/frameworks/ans/test/unittest/notification_helper_test.cpp +++ b/frameworks/ans/test/unittest/notification_helper_test.cpp @@ -626,6 +626,21 @@ HWTEST_F(NotificationHelperTest, SetNotificationsEnabledForAllBundles_00001, Fun EXPECT_EQ(ret, (int)ERR_OK); } +/** + * @tc.name: SetNotificationsEnabledForDefaultBundle_00001 + * @tc.desc: Test SetNotificationsEnabledForDefaultBundle parameters. + * @tc.type: FUNC + * @tc.require: issueI5WRQ2 + */ +HWTEST_F(NotificationHelperTest, SetNotificationsEnabledForDefaultBundle_00001, Function | SmallTest | Level1) +{ + std::string deviceId = "DeviceId"; + bool enabled = true; + NotificationHelper notificationHelper; + ErrCode ret = notificationHelper.SetNotificationsEnabledForDefaultBundle(deviceId, enabled); + EXPECT_EQ(ret, (int)ERR_INVALID_OPERATION); +} + /** * @tc.name: SetShowBadgeEnabledForBundle_00001 * @tc.desc: Test SetShowBadgeEnabledForBundle parameters. diff --git a/frameworks/core/test/unittest/ans_subscriber_proxy_test/ans_subscriber_proxy_unit_test.cpp b/frameworks/core/test/unittest/ans_subscriber_proxy_test/ans_subscriber_proxy_unit_test.cpp index 826199bf2fa43ce5358272734bf61f9fcd09dd42..fe16d0ca33ed9c335f26cfbdce19211b0c0fa67f 100644 --- a/frameworks/core/test/unittest/ans_subscriber_proxy_test/ans_subscriber_proxy_unit_test.cpp +++ b/frameworks/core/test/unittest/ans_subscriber_proxy_test/ans_subscriber_proxy_unit_test.cpp @@ -566,4 +566,76 @@ HWTEST_F(AnsSubscriberProxyUnitTest, OnEnabledNotificationChanged_0200, Function sptr callbackData = new (std::nothrow) EnabledNotificationCallbackData(); ASSERT_NE(nullptr, callbackData); proxy->OnEnabledNotificationChanged(callbackData); +} + +/* + * @tc.name: OnConnected_0100 + * @tc.desc: test AnsSubscriberProxy's OnConnected function + * @tc.type: FUNC + * @tc.require: issueI62D8C + */ +HWTEST_F(AnsSubscriberProxyUnitTest, OnConnected_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsSubscriberProxyUnitTest, OnConnected_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + proxy->OnConnected(); +} + +/* + * @tc.name: OnConnected_0200 + * @tc.desc: test AnsSubscriberProxy's OnConnected function + * @tc.type: FUNC + * @tc.require: #issueI62D8C + */ +HWTEST_F(AnsSubscriberProxyUnitTest, OnConnected_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsSubscriberProxyUnitTest, OnConnected_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + proxy->OnConnected(); +} + +/* + * @tc.name: OnDisconnected_0100 + * @tc.desc: test AnsSubscriberProxy's OnDisconnected function + * @tc.type: FUNC + * @tc.require: issueI62D8C + */ +HWTEST_F(AnsSubscriberProxyUnitTest, OnDisconnected_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsSubscriberProxyUnitTest, OnDisconnected_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + proxy->OnDisconnected(); +} + +/* + * @tc.name: OnDisconnected_0200 + * @tc.desc: test AnsSubscriberProxy's OnDisconnected function + * @tc.type: FUNC + * @tc.require: issueI62D8C + */ +HWTEST_F(AnsSubscriberProxyUnitTest, OnDisconnected_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsSubscriberProxyUnitTest, OnDisconnected_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + proxy->OnDisconnected(); } \ No newline at end of file diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 4fd77c09f2ffc472cdbbea85db11ba9a1980f44d..570a4f23f07888f02fdc11427a03d94ca026159e 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -36,6 +36,7 @@ #include "want_agent_info.h" #include "want_agent_helper.h" #include "want_params.h" +#include "bundle_manager_helper.h" using namespace testing::ext; using namespace OHOS::Media; @@ -3242,5 +3243,81 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21200, GTEST_LOG_(INFO) << "SendFlowControlOccurHiSysEvent_0200 test end"; } +/** + * @tc.number : AdvancedNotificationServiceTest_21300 + * @tc.name : PrepareNotificationRequest_0200 + * @tc.desc : Test PrepareNotificationRequest function when uid < 0. + * @tc.require : issueI62D8C + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21300, Function | SmallTest | Level1) +{ + GTEST_LOG_(INFO) << "PrepareNotificationRequest_0200 test start"; + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); + + sptr req = new NotificationRequest(); + int32_t myNotificationId = 10; + bool isAgentTrue = true; + NotificationRequest notificationRequest(myNotificationId); + notificationRequest.SetIsAgentNotification(isAgentTrue); + + std::shared_ptr bundleManager = nullptr; + + EXPECT_EQ(advancedNotificationService_->PrepareNotificationRequest(req), ERR_OK); + GTEST_LOG_(INFO) << "PrepareNotificationRequest_0200 test end"; +} + +/** + * @tc.number : AdvancedNotificationServiceTest_21400 + * @tc.name : PrepareNotificationInfo_2000 + * @tc.desc : Test PrepareNotificationInfo function. + * @tc.require : issueI62D8C + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21400, Function | SmallTest | Level1) +{ + GTEST_LOG_(INFO) << "PrepareNotificationInfo_2000 test start"; + + sptr req = new (std::nothrow) NotificationRequest(1); + EXPECT_NE(req, nullptr); + sptr bundleOption = nullptr; + + EXPECT_EQ(advancedNotificationService_->PrepareNotificationInfo(req, bundleOption), ERR_OK); + + GTEST_LOG_(INFO) << "PrepareNotificationInfo_2000 test end"; +} + +/** + * @tc.number : AdvancedNotificationServiceTest_21500 + * @tc.name : PublishPreparedNotification_1000 + * @tc.desc : Test PublishPreparedNotification function. + * @tc.require : issueI62D8C + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21500, Function | SmallTest | Level1) +{ + GTEST_LOG_(INFO) << "PublishPreparedNotification_1000 test start"; + + sptr req = new (std::nothrow) NotificationRequest(); + sptr notification = new (std::nothrow) Notification(req); + EXPECT_NE(notification, nullptr); + sptr bundleOption = nullptr; + + EXPECT_EQ(advancedNotificationService_->PublishPreparedNotification(req, bundleOption), ERR_ANS_INVALID_PARAM); + + GTEST_LOG_(INFO) << "PublishPreparedNotification_1000 test end"; +} + +/** + * @tc.number : AdvancedNotificationServiceTest_220000 + * @tc.name : TimeToString_1000 + * @tc.desc : Test TimeToString function. + * @tc.require : #I61RF2 + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_220000, Function | SmallTest | Level1) +{ + int64_t time = 60; + std::string ret = "1970-01-01, "; + std::string result = advancedNotificationService_->TimeToString(time); + EXPECT_EQ(result.substr(0, result.size() - 8), ret); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/system_event_observer_test.cpp b/services/ans/test/unittest/system_event_observer_test.cpp index 7b0baeda394e4e58de65e9c8ac489a3740051e60..a6246c54fd5c0fca3c5e5107fe202a4a9d91c400 100644 --- a/services/ans/test/unittest/system_event_observer_test.cpp +++ b/services/ans/test/unittest/system_event_observer_test.cpp @@ -108,5 +108,21 @@ HWTEST_F(SystemEventObserverTest, OnReceiveEvent_004, Function | SmallTest | Lev stub_->OnReceiveEvent(data); } +/** + * @tc.number : GetBundleOption_001 + * @tc.name : + * @tc.desc : Test GetBundleOption function. + * @tc.require : issueI5S4VP + */ +HWTEST_F(SystemEventObserverTest, GetBundleOption_001, Function | SmallTest | Level1) +{ + EventFwk::Want want; + sptr systemEventObserver = stub_->GetBundleOption(want); + EXPECT_NE(systemEventObserver, nullptr); + sptr bundleOption = new NotificationBundleOption(std::string(), -1); + bundleOption ->SetBundleName("BundleName"); + bundleOption ->SetUid(2); + stub_->GetBundleOption(want); +} } // namespace Notification } // namespace OHOS \ No newline at end of file