* Copyright (c) 2025 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 "notification_utils_test.h"
#include "notification_request.h"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
#include "../test_log.h"
using namespace testing;
using namespace testing::ext;
using namespace OHOS::MechBodyController;
namespace OHOS {
void NotificationUtilsTest::SetUpTestCase()
{
DTEST_LOG << "NotificationUtilsTest::SetUpTestCase" << std::endl;
}
void NotificationUtilsTest::TearDownTestCase()
{
DTEST_LOG << "NotificationUtilsTest::TearDownTestCase" << std::endl;
}
void NotificationUtilsTest::SetUp()
{
DTEST_LOG << "NotificationUtilsTest::SetUp" << std::endl;
}
void NotificationUtilsTest::TearDown()
{
DTEST_LOG << "NotificationUtilsTest::TearDown" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseNotificationConfigJson_001, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseNotificationConfigJson_001 begin" << std::endl;
json configJson = json::array();
Notification::NotificationRequest request;
NotificationUtils::ParseNotificationConfigJson(request, configJson);
EXPECT_EQ(request.GetNotificationId(), 0);
EXPECT_EQ(request.GetCreatorUid(), 0);
DTEST_LOG << "NotificationUtilsTest ParseNotificationConfigJson_001 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseNotificationConfigJson_002, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseNotificationConfigJson_002 begin" << std::endl;
json configJson = json::object();
Notification::NotificationRequest request;
NotificationUtils::ParseNotificationConfigJson(request, configJson);
EXPECT_EQ(request.GetNotificationId(), 0);
EXPECT_EQ(request.GetCreatorUid(), 0);
DTEST_LOG << "NotificationUtilsTest ParseNotificationConfigJson_002 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseNotificationConfigJson_003, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseNotificationConfigJson_003 begin" << std::endl;
json configJson = {
{"creatorUid", 1001},
{"notificationId", 1001}
};
Notification::NotificationRequest request;
NotificationUtils::ParseNotificationConfigJson(request, configJson);
EXPECT_EQ(request.GetCreatorUid(), 1001);
EXPECT_EQ(request.GetNotificationId(), 1001);
DTEST_LOG << "NotificationUtilsTest ParseNotificationConfigJson_003 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseBasicFields_001, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseBasicFields_001 begin" << std::endl;
json configJson = {
{"creatorUid", 1001},
{"creatorPid", 1234},
{"creatorUserId", 0},
{"notificationId", 1001},
{"notificationControlFlags", 512},
{"tapDismissed", true},
{"unremovable", false}
};
Notification::NotificationRequest request;
NotificationUtils::ParseBasicFields(request, configJson);
EXPECT_EQ(request.GetCreatorUid(), 1001);
EXPECT_EQ(request.GetCreatorPid(), 1234);
EXPECT_EQ(request.GetCreatorUserId(), 0);
EXPECT_EQ(request.GetNotificationId(), 1001);
EXPECT_EQ(request.GetNotificationControlFlags(), 512);
EXPECT_EQ(request.IsTapDismissed(), true);
EXPECT_EQ(request.IsUnremovable(), false);
DTEST_LOG << "NotificationUtilsTest ParseBasicFields_001 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseBasicFields_002, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseBasicFields_002 begin" << std::endl;
json configJson = json::object();
Notification::NotificationRequest request;
NotificationUtils::ParseBasicFields(request, configJson);
EXPECT_EQ(request.GetCreatorUid(), 0);
EXPECT_EQ(request.GetCreatorPid(), 0);
EXPECT_EQ(request.GetCreatorUserId(), -1);
EXPECT_EQ(request.GetNotificationId(), 0);
DTEST_LOG << "NotificationUtilsTest ParseBasicFields_002 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseBasicFields_003, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseBasicFields_003 begin" << std::endl;
json configJson = {
{"creatorUid", 1001},
{"notificationId", 1001}
};
Notification::NotificationRequest request;
NotificationUtils::ParseBasicFields(request, configJson);
EXPECT_EQ(request.GetCreatorUid(), 1001);
EXPECT_EQ(request.GetNotificationId(), 1001);
EXPECT_EQ(request.GetCreatorPid(), 0);
EXPECT_EQ(request.GetCreatorUserId(), -1);
DTEST_LOG << "NotificationUtilsTest ParseBasicFields_003 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseAdvancedFields_001, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseAdvancedFields_001 begin" << std::endl;
json configJson = {
{"slotType", Notification::NotificationConstant::SlotType::SOCIAL_COMMUNICATION}
};
Notification::NotificationRequest request;
NotificationUtils::ParseAdvancedFields(request, configJson);
auto slotType = request.GetSlotType();
EXPECT_EQ(slotType, Notification::NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
DTEST_LOG << "NotificationUtilsTest ParseAdvancedFields_001 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseAdvancedFields_002, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseAdvancedFields_002 begin" << std::endl;
json configJson = json::object();
Notification::NotificationRequest request;
NotificationUtils::ParseAdvancedFields(request, configJson);
auto slotType = request.GetSlotType();
EXPECT_EQ(slotType, Notification::NotificationConstant::SlotType::OTHER);
DTEST_LOG << "NotificationUtilsTest ParseAdvancedFields_002 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseExtraInfoFields_002, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseExtraInfoFields_002 begin" << std::endl;
json configJson = json::object();
Notification::NotificationRequest request;
NotificationUtils::ParseExtraInfoFields(request, configJson);
auto additionalData = request.GetAdditionalData();
EXPECT_EQ(additionalData, nullptr);
DTEST_LOG << "NotificationUtilsTest ParseExtraInfoFields_002 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseExtraInfoFields_003, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseExtraInfoFields_003 begin" << std::endl;
json configJson = {
{"extraInfo", json::array()}
};
Notification::NotificationRequest request;
NotificationUtils::ParseExtraInfoFields(request, configJson);
auto additionalData = request.GetAdditionalData();
EXPECT_EQ(additionalData, nullptr);
DTEST_LOG << "NotificationUtilsTest ParseExtraInfoFields_003 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseNormalContentJson_001, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseNormalContentJson_001 begin" << std::endl;
json contentJson = json::array();
Notification::NotificationRequest request;
NotificationUtils::ParseNormalContentJson(request, contentJson);
auto content = request.GetContent();
EXPECT_EQ(content, nullptr);
DTEST_LOG << "NotificationUtilsTest ParseNormalContentJson_001 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseLiveViewContentJson_001, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseLiveViewContentJson_001 begin" << std::endl;
json contentJson = json::array();
Notification::NotificationRequest request;
NotificationUtils::ParseLiveViewContentJson(request, contentJson);
auto content = request.GetContent();
EXPECT_EQ(content, nullptr);
DTEST_LOG << "NotificationUtilsTest ParseLiveViewContentJson_001 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseWantAgentJson_001, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseWantAgentJson_001 begin" << std::endl;
json wantAgentJson = json::array();
Notification::NotificationRequest request;
NotificationUtils::ParseWantAgentJson(request, wantAgentJson);
auto wantAgent = request.GetWantAgent();
EXPECT_EQ(wantAgent, nullptr);
DTEST_LOG << "NotificationUtilsTest ParseWantAgentJson_001 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseCapsuleJson_001, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseCapsuleJson_001 begin" << std::endl;
json capsuleJson = json::array();
Notification::NotificationCapsule capsule = NotificationUtils::ParseCapsuleJson(capsuleJson);
EXPECT_EQ(capsule.GetTitle(), "");
EXPECT_EQ(capsule.GetContent(), "");
EXPECT_EQ(capsule.GetBackgroundColor(), "");
DTEST_LOG << "NotificationUtilsTest ParseCapsuleJson_001 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseCapsuleJson_002, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseCapsuleJson_002 begin" << std::endl;
json capsuleJson = {
{"title", "CapsuleTitle"},
{"content", "CapsuleContent"},
{"backgroundColor", "#3B7DF0"}
};
Notification::NotificationCapsule capsule = NotificationUtils::ParseCapsuleJson(capsuleJson);
EXPECT_EQ(capsule.GetTitle(), "CapsuleTitle");
EXPECT_EQ(capsule.GetContent(), "");
EXPECT_EQ(capsule.GetBackgroundColor(), "#3B7DF0");
DTEST_LOG << "NotificationUtilsTest ParseCapsuleJson_002 end" << std::endl;
}
HWTEST_F(NotificationUtilsTest, ParseCapsuleJson_003, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseCapsuleJson_003 begin" << std::endl;
json capsuleJson = {
{"title", "CapsuleTitle"}
};
Notification::NotificationCapsule capsule = NotificationUtils::ParseCapsuleJson(capsuleJson);
EXPECT_EQ(capsule.GetTitle(), "CapsuleTitle");
EXPECT_EQ(capsule.GetContent(), "");
EXPECT_EQ(capsule.GetBackgroundColor(), "");
DTEST_LOG << "NotificationUtilsTest ParseCapsuleJson_003 end" << std::endl;
}
* @tc.name : ParseWantAgentJson_002
* @tc.number: ParseWantAgentJson_002
* @tc.desc : Test ParseWantAgentJson with all fields present in wantAgentJson.
*/
HWTEST_F(NotificationUtilsTest, ParseWantAgentJson_002, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseWantAgentJson_002 begin" << std::endl;
json wantAgentJson = {
{"requestCode", 1},
{"operationType", 0},
{"flags", {1}},
{"wants", {
{
{"elementName", {
{"bundleName", "com.test.bundle"},
{"bundleNameAbility", "TestAbility"}
}}
}
}}
};
Notification::NotificationRequest request;
NotificationUtils::ParseWantAgentJson(request, wantAgentJson);
auto wantAgent = request.GetWantAgent();
EXPECT_EQ(wantAgent, nullptr);
DTEST_LOG << "NotificationUtilsTest ParseWantAgentJson_002 end" << std::endl;
}
* @tc.name : ParseWantAgentJson_003
* @tc.number: ParseWantAgentJson_003
* @tc.desc : Test ParseWantAgentJson with empty object (all fields missing).
*/
HWTEST_F(NotificationUtilsTest, ParseWantAgentJson_003, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseWantAgentJson_003 begin" << std::endl;
json wantAgentJson = json::object();
Notification::NotificationRequest request;
NotificationUtils::ParseWantAgentJson(request, wantAgentJson);
auto wantAgent = request.GetWantAgent();
EXPECT_EQ(wantAgent, nullptr);
DTEST_LOG << "NotificationUtilsTest ParseWantAgentJson_003 end" << std::endl;
}
* @tc.name : ParseWantAgentJson_004
* @tc.number: ParseWantAgentJson_004
* @tc.desc : Test ParseWantAgentJson with wants but no elementName.
*/
HWTEST_F(NotificationUtilsTest, ParseWantAgentJson_004, TestSize.Level3)
{
DTEST_LOG << "NotificationUtilsTest ParseWantAgentJson_004 begin" << std::endl;
json wantAgentJson = {
{"requestCode", 2},
{"operationType", 2},
{"flags", json::array()},
{"wants", {
json::object()
}}
};
Notification::NotificationRequest request;
NotificationUtils::ParseWantAgentJson(request, wantAgentJson);
auto wantAgent = request.GetWantAgent();
EXPECT_EQ(wantAgent, nullptr);
DTEST_LOG << "NotificationUtilsTest ParseWantAgentJson_004 end" << std::endl;
}
}