From 16c84db8b15351e99f4ccf6ef1984ed9fc6b7a7c Mon Sep 17 00:00:00 2001 From: dongqingran Date: Sat, 4 Nov 2023 18:27:48 +0800 Subject: [PATCH] add tdd for system live view Signed-off-by: dongqingran --- frameworks/ans/test/unittest/BUILD.gn | 1 + ...ification_local_live_view_content_test.cpp | 44 ++++++ .../ans_innerkits_module_publish_test.cpp | 140 ++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 frameworks/ans/test/unittest/notification_local_live_view_content_test.cpp diff --git a/frameworks/ans/test/unittest/BUILD.gn b/frameworks/ans/test/unittest/BUILD.gn index 80f902f98..9e93dde4a 100644 --- a/frameworks/ans/test/unittest/BUILD.gn +++ b/frameworks/ans/test/unittest/BUILD.gn @@ -39,6 +39,7 @@ ohos_unittest("ans_reminder_unit_test") { "${frameworks_module_ans_path}/test/unittest/notification_do_not_disturb_date_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_flags_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_helper_test.cpp", + "${frameworks_module_ans_path}/test/unittest/notification_local_live_view_content_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_long_text_content_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_media_content_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_multiline_content_test.cpp", diff --git a/frameworks/ans/test/unittest/notification_local_live_view_content_test.cpp b/frameworks/ans/test/unittest/notification_local_live_view_content_test.cpp new file mode 100644 index 000000000..bd0219e39 --- /dev/null +++ b/frameworks/ans/test/unittest/notification_local_live_view_content_test.cpp @@ -0,0 +1,44 @@ +/* + * 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 +#include "notification_local_live_view_content.h" + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class NotificationLocalLiveViewContentTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +/** + * @tc.name: SetTypeCode_00001 + * @tc.desc: Test SetTypeCode parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLocalLiveViewContentTest, SetTypeCode_00001, Function | SmallTest | Level1) +{ + int32_t typeCode = 1; + auto rrc = std::make_shared(); + rrc->SetType(typeCode); + EXPECT_EQ(rrc->GetType(), typeCode); +} +} +} diff --git a/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp b/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp index 51640904f..c8cb6c82b 100644 --- a/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -17,6 +17,8 @@ #include "ans_inner_errors.h" #include "ans_manager_proxy.h" +#include "notification_local_live_view_content.h" +#include "notification_local_live_view_subscriber.h" #define private public #include "advanced_notification_service.h" #undef private @@ -60,6 +62,8 @@ const int32_t CASE_FOURTEEN = 14; const int32_t CASE_FIFTEEN = 15; const int32_t CASE_SIXTEEN = 16; const int32_t CASE_SEVENTEEN = 17; +const int32_t CASE_EIGHTEEN = 18; +const int32_t CASE_NINETEEN = 19; const int32_t PIXEL_MAP_TEST_WIDTH = 32; const int32_t PIXEL_MAP_TEST_HEIGHT = 32; @@ -73,9 +77,33 @@ std::mutex g_unsubscribe_mtx; std::mutex g_send_finished_mtx; AAFwk::Want g_want; +std::mutex g_system_live_view_subscribe_mtx; +std::mutex g_system_live_view_subscribe_response_mtx; +std::string g_onResponseReceivedButtonName {"testButton"}; +int32_t g_onResponseReceivedId = -1; + const time_t TIME_OUT_SECONDS_LIMIT = 5; const std::string CLASSIFICATION_ALARM {"alarm"}; +class TestLocalLiveViewSubscriber : public NotificationLocalLiveViewSubscriber { + void OnConnected() + {} + + void OnDisconnected() + {} + + void OnResponse(int32_t notificationId, sptr buttonOption) + { + GTEST_LOG_(INFO) << "OnResponse notificationId : " << notificationId; + g_onResponseReceivedId = notificationId; + g_onResponseReceivedButtonName = buttonOption->GetButtonName(); + g_system_live_view_subscribe_response_mtx.unlock(); + } + + void OnDied() + {} +}; + class TestAnsSubscriber : public NotificationSubscriber { public: void OnConnected() override @@ -430,6 +458,7 @@ public: void WaitOnSubscribeResult(); void WaitOnConsumed(); void WaitOnUnsubscribeResult(); + void WaitOnResponse(int32_t notificationId, std::string buttonName); void CheckJsonConverter(const NotificationRequest *request); static sptr service_; @@ -514,6 +543,25 @@ void AnsInnerKitsModulePublishTest::WaitOnUnsubscribeResult() } } +void AnsInnerKitsModulePublishTest::WaitOnResponse(int32_t notificationId, std::string buttonName) +{ + struct tm publishTime = {0}; + EXPECT_EQ(OHOS::GetSystemCurrentTime(&publishTime), true); + struct tm publishDoingTime = {0}; + EXPECT_EQ(OHOS::GetSystemCurrentTime(&publishDoingTime), true); + int64_t publishSeconds = 0; + while (!g_system_live_view_subscribe_response_mtx.try_lock()) { + EXPECT_EQ(OHOS::GetSystemCurrentTime(&publishDoingTime), true); + publishSeconds = OHOS::GetSecondsBetween(publishTime, publishDoingTime); + if (publishSeconds >= TIME_OUT_SECONDS_LIMIT) { + GTEST_LOG_(INFO) << "g_system_live_view_subscribe_response_mtx.try_lock overtime"; + break; + } + } + EXPECT_EQ(g_onResponseReceivedButtonName, buttonName); + EXPECT_EQ(g_onResponseReceivedId, notificationId); +} + void AnsInnerKitsModulePublishTest::CheckJsonConverter(const NotificationRequest *request) { nlohmann::json jsonObject; @@ -1594,6 +1642,98 @@ HWTEST_F(AnsInnerKitsModulePublishTest, ANS_Interface_MT_Publish_09000, Function WaitOnUnsubscribeResult(); } +/** + * @tc.number : ANS_Interface_MT_Publish_10001 + * @tc.name : Publish_10001 + * @tc.desc : Add notification slot(type is LIVE_VIEW), make a subscriber, a system live view subscriber + * and publish a system live view notification with capsule. + * @tc.expected : Add notification slot success, make a subscriber and publish a notification success. + */ +HWTEST_F(AnsInnerKitsModulePublishTest, ANS_Interface_MT_Publish_10001, Function | MediumTest | Level1) +{ + NotificationSlot slot(NotificationConstant::LIVE_VIEW); + EXPECT_EQ(0, NotificationHelper::AddNotificationSlot(slot)); + auto subscriber = TestAnsSubscriber(); + g_subscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber)); + WaitOnSubscribeResult(); + + auto systemLiveViewSubscriber = TestLocalLiveViewSubscriber(); + EXPECT_EQ(0, NotificationHelper::SubscribeLocalLiveViewNotification(systemLiveViewSubscriber)); + + MessageUser messageUser; + std::shared_ptr liveContent = + std::make_shared(); + EXPECT_NE(liveContent, nullptr); + std::shared_ptr content = std::make_shared(liveContent); + EXPECT_NE(content, nullptr); + + NotificationRequest req; + req.SetContent(content); + req.SetSlotType(NotificationConstant::LIVE_VIEW); + req.SetNotificationId(CASE_EIGHTEEN); + + g_consumed_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); + WaitOnConsumed(); + + g_unsubscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::UnSubscribeNotification(subscriber)); + WaitOnUnsubscribeResult(); +} + +/** + * @tc.number : ANS_Interface_MT_Publish_10002 + * @tc.name : Publish_10002 + * @tc.desc : Add notification slot(type is LIVE_VIEW), make a subscriber, a system live view subscriber + * and publish a system live view notification. Then trigger a button. + * @tc.expected : Add notification slot success, make a subscriber, publish a notification + * and trigger a buuton success. + */ +HWTEST_F(AnsInnerKitsModulePublishTest, ANS_Interface_MT_Publish_10002, Function | MediumTest | Level1) +{ + NotificationSlot slot(NotificationConstant::LIVE_VIEW); + EXPECT_EQ(0, NotificationHelper::AddNotificationSlot(slot)); + auto subscriber = TestAnsSubscriber(); + g_subscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber)); + WaitOnSubscribeResult(); + + auto systemLiveViewSubscriber = TestLocalLiveViewSubscriber(); + EXPECT_EQ(0, NotificationHelper::SubscribeLocalLiveViewNotification(systemLiveViewSubscriber)); + + MessageUser messageUser; + std::shared_ptr liveContent = + std::make_shared(); + EXPECT_NE(liveContent, nullptr); + std::shared_ptr content = std::make_shared(liveContent); + EXPECT_NE(content, nullptr); + + NotificationRequest req; + req.SetContent(content); + req.SetSlotType(NotificationConstant::LIVE_VIEW); + req.SetNotificationId(CASE_NINETEEN); + + g_consumed_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); + WaitOnConsumed(); + + std::string buttonName = "testButton"; + NotificationBundleOption bundleOption; + bundleOption.SetBundleName("bundleName"); + bundleOption.SetUid(1); + int32_t notificationId = CASE_NINETEEN; + NotificationButtonOption buttonOption; + buttonOption.SetButtonName(buttonName); + g_system_live_view_subscribe_response_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::TriggerLocalLiveView(bundleOption, notificationId, buttonOption)); + WaitOnResponse(CASE_NINETEEN, buttonName); + + g_unsubscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::UnSubscribeNotification(subscriber)); + WaitOnUnsubscribeResult(); +} + /** * @tc.name: ANS_Interface_MT_SetBadgeNumber_00100 * @tc.desc: check SetBadgeNumber interface return value. -- Gitee