From 8d78c4f18be01b13b81d3bc9aeee12df9ad6fc3c Mon Sep 17 00:00:00 2001 From: wujiqin Date: Wed, 23 Nov 2022 17:48:00 +0800 Subject: [PATCH] =?UTF-8?q?fixed=206c4aaf4=20from=20https://gitee.com/wuji?= =?UTF-8?q?qin5/notification=5Fdistributed=5Fnotification=5Fservice/pulls/?= =?UTF-8?q?810=20IssueNo:https://gitee.com/openharmony/notification=5Fdist?= =?UTF-8?q?ributed=5Fnotification=5Fservice/issues/I62D8C=3Ffrom=3Dproject?= =?UTF-8?q?-issue=20Description:ans=20TDD=E7=94=A8=E4=BE=8B=E8=A1=A5?= =?UTF-8?q?=E9=BD=9003=20Sig:SIG=5FApplicationFramework=20Feature=20or=20B?= =?UTF-8?q?ugfix:Bugfix=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wujiqin Change-Id: Ie532823215ea55132167c7180f9c1e6b43650e24 --- frameworks/ans/test/unittest/BUILD.gn | 1 + .../ans/test/unittest/message_user_test.cpp | 98 +++++++++++++++++++ .../unittest/notification_helper_test.cpp | 15 +++ .../ans_subscriber_proxy_unit_test.cpp | 72 ++++++++++++++ .../advanced_notification_service_test.cpp | 77 +++++++++++++++ .../unittest/system_event_observer_test.cpp | 16 +++ 6 files changed, 279 insertions(+) create mode 100644 frameworks/ans/test/unittest/message_user_test.cpp diff --git a/frameworks/ans/test/unittest/BUILD.gn b/frameworks/ans/test/unittest/BUILD.gn index c9e5e0525..272d8e015 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 000000000..00f3658d8 --- /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 5dda006cb..ff5a0ec12 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 826199bf2..fe16d0ca3 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 4fd77c09f..570a4f23f 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 7b0baeda3..a6246c54f 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 -- Gitee