/*
 * Copyright (c) 2023 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 <securec.h>

#include "gtest/gtest.h"

#define private public
#define protected public
#include "interfaces/inner_api/drawable_descriptor/drawable_descriptor.h"
#include "native_drawable_descriptor.h"
#include "node_extened.h"
#include "resource_manager.h"
#include "test/mock/frameworks/core/pipeline/mock_pipeline_context.h"

using namespace testing;
using namespace testing::ext;

namespace OHOS::Ace {
namespace {
constexpr int32_t ID = 1;
const uint32_t DENSITY = 0;
const std::string PATH_NAME = "";
static const uint8_t MINIMAL_PNG[] = {
    0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
    0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
    0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 0xde, 0x00, 0x00, 0x00,
    0x0c, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0xff, 0xff, 0x3f,
    0x00, 0x05, 0xfe, 0x02, 0xfe, 0x0d, 0xef, 0x46, 0xb8, 0x00, 0x00, 0x00,
    0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
};
constexpr size_t pngLen = sizeof(MINIMAL_PNG);
} // namespace
class DrawableDescriptorTest : public testing::Test {
public:
    static void SetUpTestCase() {};
    static void TearDownTestCase() {};
};

/**
 * @tc.name: DrawableDescTest001
 * @tc.desc: test DrawableDescriptor GetPixelMap when pixMap is empty;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest001, TestSize.Level1)
{
    Napi::DrawableDescriptor drawableDescriptor;
    auto res = drawableDescriptor.GetPixelMap();
    EXPECT_EQ(res, nullptr);
    ArkUI_DrawableDescriptor* drawDes = OH_ArkUI_CreateFromNapiDrawable(&drawableDescriptor);
    EXPECT_EQ(drawDes->size, 0);
    delete drawDes;
}

/**
 * @tc.name: DrawableDescTest002
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest002, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create layeredDrawableDescriptor and call GetPixelMap when layeredPixelMap is empty
     * @tc.expected: return nullptr
     */
    std::unique_ptr<uint8_t[]> jsonBuf;
    size_t len = 0;
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
    auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr));
    auto res = layeredDrawableDescriptor.GetPixelMap();
    EXPECT_EQ(res, nullptr);

    /**
     * @tc.steps: step2. call GetForeground when foreground is empty
     * @tc.expected: return nullptr
     */
    auto res2 = layeredDrawableDescriptor.GetForeground();
    EXPECT_EQ(res2, nullptr);

    /**
     * @tc.steps: step3. call GetBackground when background is empty
     * @tc.expected: return nullptr
     */
    auto res3 = layeredDrawableDescriptor.GetBackground();
    EXPECT_EQ(res3, nullptr);
}

/**
 * @tc.name: DrawableDescTest003
 * @tc.desc: test LayeredDrawableDescriptor::GetMask()
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest003, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create layeredDrawableDescriptor and call GetMask when layeredPixelMap is empty
     * @tc.expected: return nullptr
     */
    std::unique_ptr<uint8_t[]> jsonBuf;
    size_t len = 0;
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
    ASSERT_NE(resourceMgr, nullptr);
    auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr));
    /**
     * @tc.steps: step2. call GetStaticMaskClipPath
     * @tc.expected: return rightly
     */
    auto str = layeredDrawableDescriptor.GetStaticMaskClipPath();
    EXPECT_NE(str, PATH_NAME);
}

/**
 * @tc.name: DrawableDescTest004
 * @tc.desc: test DrawableDescriptorFactory::Create()
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest004, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create DrawableDescriptorFactory and call create when RState is not success
     * @tc.expected: return nullptr
     */
    std::unique_ptr<uint8_t[]> jsonBuf;
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
    ASSERT_NE(resourceMgr, nullptr);
    Napi::DrawableDescriptorFactory drawableDescriptorFactory;
    Global::Resource::RState state(Global::Resource::INVALID_FORMAT);
    Napi::DrawableDescriptor::DrawableType drawableType;
    auto res = drawableDescriptorFactory.Create(ID, resourceMgr, state, drawableType, DENSITY);
    EXPECT_EQ(res, nullptr);
    auto res2 = drawableDescriptorFactory.Create(nullptr, resourceMgr, state, drawableType, DENSITY);
    EXPECT_EQ(res2, nullptr);
    std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo = { nullptr, 0 };
    std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo = { nullptr, 0 };
    std::string path = "path";
    auto res5 = drawableDescriptorFactory.Create(foregroundInfo, backgroundInfo, path, drawableType, resourceMgr);
    ASSERT_NE(res5, nullptr);
}

/**
 * @tc.name: DrawableDescTest005
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest005, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create layeredDrawableDescriptor and call SetMaskPath
     * @tc.expected:return path.
     */
    std::unique_ptr<uint8_t[]> jsonBuf;
    size_t len = 0;
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr;
    std::string path = "path";
    uint32_t iconType = 1;
    uint32_t density = 2;
    auto layeredDrawableDescriptor =
        Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr), path, iconType, density);

    /**
     * @tc.steps: step2. check
     */
    EXPECT_EQ(layeredDrawableDescriptor.maskPath_, path);
    EXPECT_EQ(layeredDrawableDescriptor.iconType_, iconType);
    EXPECT_EQ(layeredDrawableDescriptor.density_, density);
}

/**
 * @tc.name: DrawableDescTest006
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest006, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create layeredDrawableDescriptor and call SetMaskPath
     * @tc.expected:return path.
     */
    std::unique_ptr<uint8_t[]> jsonBuf;
    size_t len = 0;
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr;
    std::string path = "path";
    uint32_t iconType = 1;
    uint32_t density = 2;
    auto layeredDrawableDescriptor =
        Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr), path, iconType, density);
    /**
     * @tc.steps: step2. check
     */
    std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo = { nullptr, 0 };
    std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo = { nullptr, 0 };
    layeredDrawableDescriptor.InitLayeredParam(foregroundInfo, backgroundInfo);

    /**
     * @tc.steps: step2. check
     */
    EXPECT_EQ(layeredDrawableDescriptor.foreground_, std::nullopt);
    EXPECT_EQ(layeredDrawableDescriptor.background_, std::nullopt);
}

/**
 * @tc.name: DrawableDescTest007
 * @tc.desc: test DrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest007, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create DrawableDescriptor and call GetDrawableType()
     * @tc.expected:return BASE.
     */
    Napi::DrawableDescriptor drawableDescriptor;
    auto res = drawableDescriptor.GetDrawableType();
    EXPECT_EQ(res, Napi::DrawableDescriptor::DrawableType::BASE);
}

/**
 * @tc.name: DrawableDescTest008
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest008, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create layeredDrawableDescriptor and call GetDrawableType()
     * @tc.expected:return LAYERED.
     */
    std::unique_ptr<uint8_t[]> jsonBuf;
    size_t len = 0;
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr;
    std::string path = "path";
    uint32_t iconType = 1;
    uint32_t density = 2;
    auto layeredDrawableDescriptor =
        Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr), path, iconType, density);

    /**
     * @tc.steps: step2. check
     */
    auto res = layeredDrawableDescriptor.GetDrawableType();
    EXPECT_EQ(res, Napi::DrawableDescriptor::DrawableType::LAYERED);
}

/**
 * @tc.name: DrawableDescTest0010
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0010, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create layeredDrawableDescriptor and init mask only
     */
    std::shared_ptr<Global::Resource::ResourceManager> resMgr(Global::Resource::CreateResourceManager());
    auto layeredDrawable = Napi::LayeredDrawableDescriptor();
    /**
     * @tc.steps: step2. init resource name and data
     */
    layeredDrawable.InitialMask(resMgr);
    /**
     * @tc.steps: step2. check creating mask ok
     */
    EXPECT_TRUE(layeredDrawable.GetDefaultMask());
}

/**
 * @tc.name: DrawableDescTest0011
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0011, TestSize.Level1)
{
    /**
     * @tc.steps: step1. pixelMap param not exist in pixelMapDrawable
     */
    std::shared_ptr<Global::Resource::ResourceManager> resMgr(Global::Resource::CreateResourceManager());
    auto layeredDrawable = Napi::LayeredDrawableDescriptor();
    /**
     * @tc.steps: step2. init resource name and data
     */
    layeredDrawable.InitialMask(resMgr);
    /**
     * @tc.steps: step3. update foreground into layeredDrawable
     */
    std::shared_ptr<Media::PixelMap> foreground = std::make_shared<Media::PixelMap>();
    layeredDrawable.SetForeground(foreground);
    layeredDrawable.layeredPixelMap_ = foreground;
    auto composedResult = layeredDrawable.GetPixelMap();
    /**
     * @tc.steps: step3. check pixelMap should not be null since this layeredDrawable is customized
     */
    EXPECT_NE(composedResult, nullptr);
}

/**
 * @tc.name: DrawableDescTest0012
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0012, TestSize.Level1)
{
    /**
     * @tc.steps: step1. init drawble
     */
    auto layeredDrawable = Napi::LayeredDrawableDescriptor();
    /**
     * @tc.steps: step2. get pixelMap directly from layeredDrawable
     */
    auto composedResult = layeredDrawable.GetPixelMap();
    /**
     * @tc.steps: step3. check pixelMap should be null since this layeredDrawable is not customized
     * therefore foreground, background does not exist when create
     */
    EXPECT_EQ(composedResult, nullptr);
}

/**
 * @tc.name: DrawableDescTest0013
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0013, TestSize.Level1)
{
    /**
     * @tc.steps: step1. init layeredDrawble
     */
    auto drawable = Napi::DrawableDescriptor();

    /**
     * @tc.steps: step2. set pixelMap to drawable
     */
    drawable.SetPixelMap(std::make_shared<Media::PixelMap>());
    /**
     * @tc.steps: step3. check drawable has pixelMap
     */
    EXPECT_TRUE(drawable.HasPixelMap());
    drawable.ResetPixelMap();
    EXPECT_FALSE(drawable.HasPixelMap());
}

/**
 * @tc.name: DrawableDescTest0014
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0014, TestSize.Level1)
{
    /**
     * @tc.steps: step1. init layeredDrawble
     */
    auto drawable = Napi::LayeredDrawableDescriptor();

    /**
     * @tc.steps: step2. set param to layeredDrawable
     */
    drawable.SetForeground(std::make_shared<Media::PixelMap>());
    drawable.SetBackground(std::make_shared<Media::PixelMap>());
    drawable.SetMask(std::make_shared<Media::PixelMap>());
    /**
     * @tc.steps: step3. check layeredDrawable is customized
     */
    EXPECT_TRUE(drawable.Customized());
}

/**
 * @tc.name: DrawableDescTest0015
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0015, TestSize.Level1)
{
    /**
     * @tc.steps: step1. init layeredDrawble
     */
    auto drawable = Napi::LayeredDrawableDescriptor();

    /**
     * @tc.steps: step2. set param to layeredDrawable
     */
    drawable.SetForeground(std::make_shared<Media::PixelMap>());
    drawable.SetBackground(std::make_shared<Media::PixelMap>());
    drawable.SetMask(std::make_shared<Media::PixelMap>());
    drawable.SetBlendMode(1);

    /**
     * @tc.steps: step3. check layeredDrawable blendMode has been setted.
     */
    EXPECT_EQ(drawable.blendMode_, 1);
    EXPECT_EQ(drawable.foregroundOverBackground_, true);

    /**
     * @tc.steps: step4. set param to layeredDrawable
     */
    drawable.blendMode_ = -1;
    drawable.foregroundOverBackground_ = false;
    drawable.SetBlendMode(-10);

    /**
     * @tc.steps: step5. check layeredDrawable blendMode has not been setted.
     */
    EXPECT_EQ(drawable.blendMode_, -1);
    EXPECT_EQ(drawable.foregroundOverBackground_, false);

    /**
     * @tc.steps: step6. set param to layeredDrawable
     */
    drawable.foregroundOverBackground_ = true;
    drawable.InitBlendMode();

    /**
     * @tc.steps: step7. check layeredDrawable blendMode has not been setted.
     */
    EXPECT_EQ(drawable.blendMode_, 3);
    EXPECT_EQ(drawable.foregroundOverBackground_, true);
}

/**
 * @tc.name: DrawableDescTest0017
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0017, TestSize.Level1)
{
    /**
     * @tc.steps: step1. init layeredDrawable
     */
    auto drawable = Napi::LayeredDrawableDescriptor();

    /**
     * @tc.steps: step2. set param to layeredDrawable
     */
    std::shared_ptr<Media::PixelMap> layeredPixelMap;
    std::shared_ptr<Media::PixelMap> badgedPixelMap;
    std::shared_ptr<Media::PixelMap> compositePixelMap;
    bool ret = drawable.GetCompositePixelMapWithBadge(layeredPixelMap, badgedPixelMap, compositePixelMap);
    /**
     * @tc.steps: step3. check layeredDrawable result
     */
    EXPECT_FALSE(ret);
}

/**
 * @tc.name: DrawableDescTest0018
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0018, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    ;
    const char* item = "item";
    Napi::DrawableItem resItem = drawable.PreGetDrawableItem(nullptr, item);
    EXPECT_EQ(resItem.len_, 0);
    const char* item1 = "";
    resItem = drawable.PreGetDrawableItem(nullptr, item1);
    EXPECT_EQ(resItem.len_, 0);
}

/**
 * @tc.name: DrawableDescTest0019
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0019, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    const char* jsonBuf = "{\"background\":\"background\"}";
    drawable.len_ = strlen(jsonBuf) + 1;
    drawable.jsonBuf_ = std::make_unique<uint8_t[]>(drawable.len_);
    memcpy_s(drawable.jsonBuf_.get(), strlen(jsonBuf), jsonBuf, strlen(jsonBuf));
    auto ret = drawable.PreGetPixelMapFromJsonBuf(nullptr, true);
    EXPECT_FALSE(ret);
}

/**
 * @tc.name: DrawableDescTest0020
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0020, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    const char* jsonBuf = "{\"foreground\": \"foreground\"}";
    drawable.len_ = strlen(jsonBuf) + 1;
    drawable.jsonBuf_ = std::make_unique<uint8_t[]>(drawable.len_);
    memcpy_s(drawable.jsonBuf_.get(), strlen(jsonBuf), jsonBuf, strlen(jsonBuf));
    auto ret = drawable.PreGetPixelMapFromJsonBuf(nullptr, false);
    EXPECT_FALSE(ret);
}

/**
 * @tc.name: DrawableDescTest0021
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0021, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    auto ret = drawable.GetPixelMapFromBuffer();
    EXPECT_FALSE(ret);
}

/**
 * @tc.name: DrawableDescTest0023
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0023, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    drawable.backgroundItem_.state_ = Global::Resource::SUCCESS;
    auto ret = drawable.GetPixelMapFromJsonBuf(true);
    EXPECT_FALSE(ret);
}

/**
 * @tc.name: DrawableDescTest0024
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0024, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    drawable.maskPath_ = "/data/local/tmp/image/123.jpg";
    auto ret = drawable.GetMaskByPath();
    EXPECT_FALSE(ret);
    std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
    drawable.mask_ = pixelMap;
    ret = drawable.GetMaskByPath();
    EXPECT_FALSE(ret);
}

/**
 * @tc.name: DrawableDescTest0025
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0025, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
    auto ret = drawable.GetMaskByName(resourceMgr, "name");
    EXPECT_FALSE(ret);
}

/**
 * @tc.name: DrawableDescTest0026
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0026, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
    drawable.foreground_ = pixelMap;
    auto ret = drawable.GetForeground();
    EXPECT_FALSE(ret == nullptr);
}

/**
 * @tc.name: DrawableDescTest0027
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0027, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
    drawable.background_ = pixelMap;
    auto ret = drawable.GetBackground();
    EXPECT_FALSE(ret == nullptr);
}

/**
 * @tc.name: DrawableDescTest0028
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0028, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
    drawable.mask_ = pixelMap;
    auto ret = drawable.GetMask();
    EXPECT_FALSE(ret == nullptr);
}

/**
 * @tc.name: DrawableDescTest0029
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0029, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
    drawable.layeredPixelMap_ = pixelMap;
    auto ret = drawable.GetPixelMap();
    EXPECT_FALSE(ret == nullptr);
}

/**
 * @tc.name: DrawableDescTest0031
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0031, TestSize.Level1)
{
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
    auto drawable = Napi::LayeredDrawableDescriptor();
    const char* item = "i:1";
    Napi::DrawableItem resItem = drawable.PreGetDrawableItem(resourceMgr, item);
    EXPECT_EQ(resItem.len_, 0);
}

/**
 * @tc.name: DrawableDescTest0034
 * @tc.desc: Test PreGetDrawableItem function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0034, TestSize.Level1)
{
    auto descriptor = Napi::LayeredDrawableDescriptor();
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());

    // Case 1: Input string does not contain ':'
    {
        const char* testStr = "InvalidString";
        Napi::DrawableItem resItem = descriptor.PreGetDrawableItem(resourceMgr, testStr);
        EXPECT_EQ(resItem.len_, 0);
    }

    // Case 2: Input string contains ':' but the part after ':' is not a number
    {
        const char* testStr = "drawable:abc";
        auto resItem = descriptor.PreGetDrawableItem(resourceMgr, testStr);
        EXPECT_EQ(resItem.len_, 0);
    }

    // Case 3: Input string is correctly formatted
    {
        const char* testStr = "drawable:123";
        auto resItem = descriptor.PreGetDrawableItem(resourceMgr, testStr);
        EXPECT_EQ(resItem.len_, 0);
    }
}

/**
 * @tc.name: DrawableDescTest0035
 * @tc.desc: test OptionalPixelMap;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0035, TestSize.Level1)
{
    /**
     * @tc.steps: step1. pixelMap param not exist in pixelMapDrawable
     */
    std::shared_ptr<Global::Resource::ResourceManager> resMgr(Global::Resource::CreateResourceManager());
    auto layeredDrawable = Napi::LayeredDrawableDescriptor();
    /**
     * @tc.steps: step2. init resource name and data
     */
    layeredDrawable.InitialMask(resMgr);
    /**
     * @tc.steps: step3. update foreground is nullptr into layeredDrawable
     */
    layeredDrawable.foreground_ = std::make_optional(nullptr);
    auto foregroundRes1 = layeredDrawable.GetForeground();
    EXPECT_EQ(foregroundRes1, nullptr);
    /**
     * @tc.steps: step3. update foreground into layeredDrawable
     */
    layeredDrawable.foreground_ = std::make_shared<Media::PixelMap>();
    auto foregroundRes2 = layeredDrawable.GetForeground();
    EXPECT_NE(foregroundRes2, nullptr);
}

/**
 * @tc.name: DrawableDescTest0036
 * @tc.desc: test LayeredDrawableDescriptor::GetMask()
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0036, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create layeredDrawableDescriptor and call GetMask when layeredPixelMap is empty
     * @tc.expected: return nullptr
     */
    std::unique_ptr<uint8_t[]> jsonBuf;
    size_t len = 0;
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
    ASSERT_NE(resourceMgr, nullptr);
    auto layeredDrawableDescriptor = Napi::LayeredDrawableDescriptor(std::move(jsonBuf), len, std::move(resourceMgr));
    /**
     * @tc.steps: step2. call SetDecodeSize
     * @tc.expected: return rightly
     */
    int32_t width = 500;
    int32_t height = 500;
    layeredDrawableDescriptor.SetDecodeSize(width, height);
    Napi::OptionalDecodeSize res = layeredDrawableDescriptor.GetDecodeSize();
    std::optional<std::pair<int, int>> param = std::make_pair(width, height);
    EXPECT_EQ(res, param);
}

/**
 * @tc.name: DrawableDescTest0037
 * @tc.desc: test LayeredDrawableDescriptor::GetMask()
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0037, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create layeredDrawableDescriptor
     */
    size_t len = 1;
    std::string path = "abc";
    uint32_t iconType = 0;
    Napi::DataInfo foregroundInfo = { std::make_unique<uint8_t[]>(1024), 1024 };
    Napi::DataInfo backgroundInfo = { std::make_unique<uint8_t[]>(512), 512 };
    const std::pair<int32_t, int32_t> decoderSize = { 10, 20 };
    auto layeredDrawableDescriptor =
        Napi::LayeredDrawableDescriptor(len, path, iconType, foregroundInfo, backgroundInfo, decoderSize);
    /**
     * @tc.steps: step2. EXPECT len
     * @tc.expected: return rightly
     */
    EXPECT_EQ(len, layeredDrawableDescriptor.len_);
}

/**
 * @tc.name: DrawableDescTest038
 * @tc.desc: test LayeredDrawableDescriptor's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest038, TestSize.Level1)
{
    /**
     * @tc.steps: step1. create layeredDrawableDescriptor and call SetMaskPath
     * @tc.expected:return path.
     */
    std::unique_ptr<uint8_t[]> jsonBuf1;
    std::unique_ptr<uint8_t[]> jsonBuf2;
    size_t len = 0;
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr;
    std::string path = "path";
    uint32_t iconType = 1;
    uint32_t density = 2;
    auto layeredDrawableDescriptor1 =
        Napi::LayeredDrawableDescriptor(std::move(jsonBuf1), len, std::move(resourceMgr), path, iconType, density);
    EXPECT_FALSE(layeredDrawableDescriptor1.foregroundOverBackground_);

    auto layeredDrawableDescriptor2 = Napi::LayeredDrawableDescriptor(
        std::move(jsonBuf1), len, std::move(resourceMgr), path, iconType, density, true);
    EXPECT_TRUE(layeredDrawableDescriptor2.foregroundOverBackground_);

    auto layeredDrawableDescriptor3 = Napi::LayeredDrawableDescriptor(
        std::move(jsonBuf1), len, std::move(resourceMgr), path, iconType, density, false);
    EXPECT_FALSE(layeredDrawableDescriptor3.foregroundOverBackground_);
}

/**
 * @tc.name: DrawableDescTestItem001
 * @tc.desc: test DrawableItem's member functions;
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTestItem001, TestSize.Level1)
{
    std::shared_ptr<Global::Resource::ResourceManager> resourceMgr(Global::Resource::CreateResourceManager());
    auto drawable = Napi::LayeredDrawableDescriptor();
    const char* item = "i:1";
    Napi::DrawableItem resItem;
    Global::Resource::RState state(Global::Resource::INVALID_FORMAT);
    EXPECT_EQ(resItem.state_, state);
    resItem = drawable.PreGetDrawableItem(resourceMgr, item);
    EXPECT_EQ(resItem.len_, 0);
}

/**
 * @tc.name: DrawableDescTest0039
 * @tc.desc: test InitLayeredParam function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0039, TestSize.Level1)
{
    auto fgData = std::make_unique<uint8_t[]>(pngLen);
    memcpy_s(fgData.get(), pngLen, MINIMAL_PNG, pngLen);
    Napi::DataInfo foregroundInfo = { std::move(fgData), pngLen };
    Napi::DataInfo backgroundInfo = { nullptr, 0 };

    auto layeredDrawable = Napi::LayeredDrawableDescriptor();
    layeredDrawable.InitLayeredParam(foregroundInfo, backgroundInfo);

    EXPECT_TRUE(layeredDrawable.foreground_.has_value());
    EXPECT_NE(layeredDrawable.foreground_.value(), nullptr);
    EXPECT_FALSE(layeredDrawable.background_.has_value());
}

/**
 * @tc.name: DrawableDescTest0040
 * @tc.desc: test InitLayeredParam function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0040, TestSize.Level1)
{
    auto bgData = std::make_unique<uint8_t[]>(pngLen);
    memcpy_s(bgData.get(), pngLen, MINIMAL_PNG, pngLen);
    Napi::DataInfo foregroundInfo = { nullptr, 0 };
    Napi::DataInfo backgroundInfo = { std::move(bgData), pngLen };

    auto layeredDrawable = Napi::LayeredDrawableDescriptor();
    layeredDrawable.InitLayeredParam(foregroundInfo, backgroundInfo);

    EXPECT_FALSE(layeredDrawable.foreground_.has_value());
    EXPECT_TRUE(layeredDrawable.background_.has_value());
    EXPECT_NE(layeredDrawable.background_.value(), nullptr);
}

/**
 * @tc.name: DrawableDescTest0041
 * @tc.desc: test InitLayeredParam function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0041, TestSize.Level1)
{
    auto fgData = std::make_unique<uint8_t[]>(pngLen);
    memcpy_s(fgData.get(), pngLen, MINIMAL_PNG, pngLen);
    auto bgData = std::make_unique<uint8_t[]>(pngLen);
    memcpy_s(bgData.get(), pngLen, MINIMAL_PNG, pngLen);
    Napi::DataInfo foregroundInfo = { std::move(fgData), pngLen };
    Napi::DataInfo backgroundInfo = { std::move(bgData), pngLen };

    auto layeredDrawable = Napi::LayeredDrawableDescriptor();
    layeredDrawable.InitLayeredParam(foregroundInfo, backgroundInfo);

    EXPECT_TRUE(layeredDrawable.foreground_.has_value());
    EXPECT_NE(layeredDrawable.foreground_.value(), nullptr);
    EXPECT_TRUE(layeredDrawable.background_.has_value());
    EXPECT_NE(layeredDrawable.background_.value(), nullptr);
}

/**
 * @tc.name: DrawableDescTest0042
 * @tc.desc: test InitLayeredParam function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0042, TestSize.Level1)
{
    auto fgData = std::make_unique<uint8_t[]>(pngLen);
    memcpy_s(fgData.get(), pngLen, MINIMAL_PNG, pngLen);
    auto bgData = std::make_unique<uint8_t[]>(pngLen);
    memcpy_s(bgData.get(), pngLen, MINIMAL_PNG, pngLen);
    Napi::DataInfo foregroundInfo = { std::move(fgData), pngLen };
    Napi::DataInfo backgroundInfo = { std::move(bgData), pngLen };

    auto layeredDrawable = Napi::LayeredDrawableDescriptor();
    layeredDrawable.InitLayeredParam(foregroundInfo, backgroundInfo);

    EXPECT_TRUE(layeredDrawable.foreground_.has_value());
    EXPECT_NE(layeredDrawable.foreground_.value(), nullptr);
    EXPECT_TRUE(layeredDrawable.background_.has_value());
    EXPECT_NE(layeredDrawable.background_.value(), nullptr);
}

/**
 * @tc.name: DrawableDescTest0043
 * @tc.desc: test InitLayeredParam function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0043, TestSize.Level1)
{
    auto fgData = std::make_unique<uint8_t[]>(pngLen);
    memcpy_s(fgData.get(), pngLen, MINIMAL_PNG, pngLen);
    Napi::DataInfo foregroundInfo = { std::move(fgData), pngLen };
    Napi::DataInfo backgroundInfo = { nullptr, 0 };

    auto layeredDrawable = Napi::LayeredDrawableDescriptor();
    layeredDrawable.InitLayeredParam(foregroundInfo, backgroundInfo);

    EXPECT_TRUE(layeredDrawable.foreground_.has_value());
    EXPECT_NE(layeredDrawable.foreground_.value(), nullptr);
    EXPECT_FALSE(layeredDrawable.background_.has_value());
}

/**
 * @tc.name: DrawableDescTest0044
 * @tc.desc: test GetMask function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0044, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    auto pixelMap = std::make_shared<Media::PixelMap>();
    drawable.mask_ = pixelMap;
    auto result = drawable.GetMask();
    ASSERT_NE(result, nullptr);
    EXPECT_EQ(result->GetDrawableType(), Napi::DrawableDescriptor::DrawableType::BASE);
}

/**
 * @tc.name: DrawableDescTest0045
 * @tc.desc: test GetMask function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0045, TestSize.Level1)
{
    auto resMgr = std::shared_ptr<Global::Resource::ResourceManager>(Global::Resource::CreateResourceManager());
    ASSERT_NE(resMgr, nullptr);
    auto drawable = Napi::LayeredDrawableDescriptor();
    drawable.InitialMask(resMgr);

    EXPECT_FALSE(drawable.mask_.has_value());
    EXPECT_TRUE(drawable.maskPath_.empty());

    auto result = drawable.GetMask();
    EXPECT_NE(result, nullptr);
    EXPECT_TRUE(drawable.mask_.has_value());
}

/**
 * @tc.name: DrawableDescTest0046
 * @tc.desc: test GetMask function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0046, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    EXPECT_FALSE(drawable.mask_.has_value());
    EXPECT_TRUE(drawable.maskPath_.empty());

    auto result = drawable.GetMask();
    EXPECT_EQ(result, nullptr);
}

/**
 * @tc.name: DrawableDescTest0047
 * @tc.desc: test GetMask function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0047, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    drawable.mask_ = std::make_optional(nullptr);

    auto result = drawable.GetMask();
    EXPECT_EQ(result, nullptr);
}

/**
 * @tc.name: DrawableDescTest0048
 * @tc.desc: test GetLayeredIconParm function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0048, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    Napi::SharedBitMap foreground;
    Napi::SharedBitMap background;
    Napi::SharedBitMap mask;
    EXPECT_FALSE(drawable.foreground_.has_value());
    EXPECT_FALSE(drawable.customized_);

    auto ret = drawable.GetLayeredIconParm(foreground, background, mask);
    EXPECT_FALSE(ret);
    EXPECT_EQ(foreground, nullptr);
}

/**
 * @tc.name: DrawableDescTest0049
 * @tc.desc: test GetLayeredIconParm function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0049, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    drawable.foreground_ = std::make_shared<Media::PixelMap>();
    EXPECT_TRUE(drawable.foreground_.has_value());
    EXPECT_FALSE(drawable.background_.has_value());
    EXPECT_FALSE(drawable.customized_);

    Napi::SharedBitMap foreground;
    Napi::SharedBitMap background;
    Napi::SharedBitMap mask;
    auto ret = drawable.GetLayeredIconParm(foreground, background, mask);
    EXPECT_FALSE(ret);
    EXPECT_NE(foreground, nullptr);
    EXPECT_EQ(background, nullptr);
}

/**
 * @tc.name: DrawableDescTest0050
 * @tc.desc: test GetLayeredIconParm function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0050, TestSize.Level1)
{
    auto drawable = Napi::LayeredDrawableDescriptor();
    drawable.foreground_ = std::make_shared<Media::PixelMap>();
    drawable.background_ = std::make_shared<Media::PixelMap>();
    EXPECT_TRUE(drawable.foreground_.has_value());
    EXPECT_TRUE(drawable.background_.has_value());
    EXPECT_FALSE(drawable.mask_.has_value());
    EXPECT_TRUE(drawable.maskPath_.empty());
    EXPECT_FALSE(drawable.customized_);

    Napi::SharedBitMap foreground;
    Napi::SharedBitMap background;
    Napi::SharedBitMap mask;
    auto ret = drawable.GetLayeredIconParm(foreground, background, mask);
    EXPECT_FALSE(ret);
    EXPECT_NE(foreground, nullptr);
    EXPECT_NE(background, nullptr);
    EXPECT_EQ(mask, nullptr);
}

/**
 * @tc.name: DrawableDescTest0051
 * @tc.desc: test CompositeIconNotAdaptive function
 * @tc.type: FUNC
 */
HWTEST_F(DrawableDescriptorTest, DrawableDescTest0051, TestSize.Level1)
{
    auto fgData = std::make_unique<uint8_t[]>(pngLen);
    memcpy_s(fgData.get(), pngLen, MINIMAL_PNG, pngLen);
    Napi::DataInfo foregroundInfo = { std::move(fgData), pngLen };

    auto bgData = std::make_unique<uint8_t[]>(pngLen);
    memcpy_s(bgData.get(), pngLen, MINIMAL_PNG, pngLen);
    Napi::DataInfo backgroundInfo = { std::move(bgData), pngLen };

    auto drawable = Napi::LayeredDrawableDescriptor();
    drawable.InitLayeredParam(foregroundInfo, backgroundInfo);

    auto resMgr = std::shared_ptr<Global::Resource::ResourceManager>(Global::Resource::CreateResourceManager());
    drawable.InitialMask(resMgr);
    drawable.customized_ = true;

    ASSERT_TRUE(drawable.foreground_.has_value());
    ASSERT_NE(drawable.foreground_.value(), nullptr);
    ASSERT_TRUE(drawable.background_.has_value());
    ASSERT_NE(drawable.background_.value(), nullptr);
    EXPECT_FALSE(drawable.foregroundOverBackground_);

    Napi::SharedBitMap foreground;
    Napi::SharedBitMap background;
    Napi::SharedBitMap mask;
    auto ret = drawable.GetLayeredIconParm(foreground, background, mask);
    ASSERT_TRUE(ret);
    ASSERT_NE(foreground, nullptr);
    ASSERT_NE(background, nullptr);
    ASSERT_NE(mask, nullptr);

    drawable.CompositeIconNotAdaptive(foreground, background, mask);

    EXPECT_TRUE(drawable.layeredPixelMap_.has_value());
}
} // namespace OHOS::Ace