1/*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "gtest/gtest.h"
17
18#include "effect_memory.h"
19
20using namespace testing::ext;
21
22namespace OHOS {
23namespace Media {
24namespace Effect {
25namespace Test {
26
27constexpr int BUFFER_SIZE = 1024;
28
29constexpr uint32_t WIDTH = 1920;
30constexpr uint32_t HEIGHT = 1080;
31constexpr IEffectFormat FORMATE_TYPE = IEffectFormat::RGBA8888;
32constexpr uint32_t ROW_STRIDE = WIDTH * 4;
33constexpr uint32_t LEN = ROW_STRIDE * HEIGHT;
34
35class TestEffectMemoryManager : public testing::Test {
36public:
37    TestEffectMemoryManager() = default;
38
39    ~TestEffectMemoryManager() override = default;
40    static void SetUpTestCase() {}
41
42    static void TearDownTestCase() {}
43
44    void SetUp() override
45    {
46        buffer = malloc(BUFFER_SIZE);
47        std::shared_ptr<HeapMemoryData> memoryData = std::make_unique<HeapMemoryData>();
48        memoryData->data = buffer;
49        memoryData_ = memoryData;
50    }
51
52    void TearDown() override
53    {
54        memoryData_ = nullptr;
55        free(buffer);
56        buffer = nullptr;
57    }
58
59    void *buffer = nullptr;
60    std::shared_ptr<HeapMemoryData> memoryData_;
61};
62
63HWTEST_F(TestEffectMemoryManager, TestEffectMemoryManager001, TestSize.Level1) {
64
65    std::unique_ptr<HeapMemory> heapMemory = std::make_unique<HeapMemory>();
66    ErrorCode result = heapMemory->Release();
67    ASSERT_NE(result, ErrorCode::SUCCESS);
68
69    MemoryInfo memoryInfo;
70    memoryInfo.bufferInfo.width_ = WIDTH;
71    memoryInfo.bufferInfo.height_ = HEIGHT;
72    memoryInfo.bufferInfo.len_ = LEN;
73    memoryInfo.bufferInfo.rowStride_ = ROW_STRIDE;
74    memoryInfo.bufferInfo.formatType_ = FORMATE_TYPE;
75
76    std::shared_ptr<MemoryData> memoryData = heapMemory->Alloc(memoryInfo);
77    ASSERT_NE(memoryData, nullptr);
78    result = heapMemory->Release();
79    ASSERT_EQ(result, ErrorCode::SUCCESS);
80
81    std::shared_ptr<DmaMemory> dmaMemory = std::make_shared<DmaMemory>();
82    result = dmaMemory->Release();
83    ASSERT_NE(result, ErrorCode::SUCCESS);
84
85    memoryData = dmaMemory->Alloc(memoryInfo);
86    ASSERT_NE(memoryData, nullptr);
87    result = dmaMemory->Release();
88    ASSERT_EQ(result, ErrorCode::SUCCESS);
89
90    std::shared_ptr<SharedMemory> sharedMemory = std::make_shared<SharedMemory>();
91    result = sharedMemory->Release();
92    ASSERT_NE(result, ErrorCode::SUCCESS);
93
94    memoryData = sharedMemory->Alloc(memoryInfo);
95    ASSERT_NE(memoryData, nullptr);
96    result = sharedMemory->Release();
97    ASSERT_EQ(result, ErrorCode::SUCCESS);
98
99    EffectMemory *effectMemory = new EffectMemory();
100    BufferType bufferType = BufferType::SHARED_MEMORY;
101    std::unique_ptr<AbsMemory> absMemory = effectMemory->CreateMemory(bufferType);
102    ASSERT_NE(absMemory, nullptr);
103    bufferType = BufferType::DEFAULT;
104    absMemory = effectMemory->CreateMemory(bufferType);
105    ASSERT_EQ(absMemory, nullptr);
106}
107}
108}
109}
110}