1/*
2 * Copyright (C) 2023 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#ifndef AVBUFFER_UNITTEST_H
17#define AVBUFFER_UNITTEST_H
18#include <dirent.h>
19#include <gtest/gtest.h>
20#include <memory>
21#include <string>
22#include <vector>
23#include "av_hardware_allocator.h"
24#include "buffer/avbuffer.h"
25#include "dmabuf_alloc.h"
26#include "meta.h"
27#include "meta_key.h"
28#include "surface_buffer.h"
29#include "surface_type.h"
30
31#define INT_TESTKEY Tag::APP_PID
32#define LONG_TESTKEY Tag::MEDIA_DURATION
33#define DOUBLE_TESTKEY Tag::VIDEO_CAPTURE_RATE
34#define STRING_TESTKEY Tag::MEDIA_FILE_URI
35namespace OHOS {
36namespace Media {
37class AVBufferMock;
38namespace AVBufferUT {
39const std::string_view INT_CAPI_TESTKEY = "IntKey";
40const std::string_view LONG_CAPI_TESTKEY = "LongKey";
41const std::string_view FlOAT_CAPI_TESTKEY = "FloatKey";
42const std::string_view DOUBLE_CAPI_TESTKEY = "DoubleKey";
43const std::string_view STRING_CAPI_TESTKEY = "StringKey";
44
45constexpr int32_t MEMSIZE = 1024 * 1024;
46constexpr int32_t POSITION_ONE = 1024 * 64;
47constexpr int32_t TEST_BUFFER_SIZE = 1048 * 1048 * 8;
48constexpr int32_t TEST_LOOP_DEPTH = 10;
49
50const int32_t INTVALUE = 141;
51const int64_t LONGVALUE = 115441;
52const float FLOATVALUE = 1.25;
53const double DOUBLEVALUE = 1.625;
54const std::string STRINGVALUE = "STRING_TESTVALUE";
55
56const int32_t DEFAULT_OFFSET = 1000;
57const int64_t DEFAULT_PTS = 33000;
58const int64_t DEFAULT_DTS = 100;
59const int64_t DEFAULT_DURATION = 1000;
60const uint32_t DEFAULT_FLAG = 1 << 0;
61
62const int32_t DEFAULT_PIXELSIZE = 4;
63const BufferRequestConfig DEFAULT_CONFIG = {
64    .width = 800,
65    .height = 600,
66    .strideAlignment = 0x8,
67    .format = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888,
68    .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
69    .timeout = 0,
70};
71
72class AVBufferInnerUnitTest : public testing::Test {
73public:
74    static void SetUpTestCase(void);
75    static void TearDownTestCase(void);
76    void SetUp(void);
77    void TearDown(void);
78
79private:
80    void CreateLocalHardwareMem();
81    void CreateLocalHardwareMemSecure();
82    void CreateLocalSharedMem();
83    void CreateLocalSurfaceMem();
84    void CreateLocalVirtualMem();
85
86    void CreateLocalSurfaceMemByParcel();
87    void CreateLocalSurfaceMemBySptr();
88
89    void CreateLocalHardwareMemByConfig();
90    void CreateLocalSharedMemByConfig();
91    void CreateLocalSurfaceMemByConfig();
92    void CreateLocalVirtualMemByConfig();
93
94    void CreateRemoteHardwareMem();
95    void CreateRemoteSharedMem();
96    void CreateRemoteSurfaceMem();
97    void CreateRemoteSurfaceMemByParcel();
98    void CreateRemoteSurfaceMemBySptr();
99    void CreateLocalNullMem();
100    void GetRemoteBuffer();
101
102    void CheckMetaSetAndGet();
103    void CheckMetaTransParcel();
104    void CheckAttrTrans();
105    void CheckMemTrans();
106    void CheckMemTransPos(int32_t pos);
107    void CheckMemTransOutOfRange(int32_t pos);
108    void CheckDataSize();
109    std::shared_ptr<AVAllocator> allocator_ = nullptr;
110    std::shared_ptr<AVBuffer> buffer_ = nullptr;
111    std::shared_ptr<AVBuffer> remoteBuffer_ = nullptr;
112    std::shared_ptr<Meta> meta_ = nullptr;
113    std::shared_ptr<MessageParcel> parcel_ = nullptr;
114    MemoryFlag memFlag_;
115    int32_t capacity_ = MEMSIZE;
116    int32_t align_ = 0;
117    int32_t dmaFd_ = -1;
118    AVBufferConfig config_;
119    std::vector<DmabufHeapBuffer> dmaBufferLst_;
120
121    std::vector<uint8_t> inputBuffer_;
122    std::vector<uint8_t> outputBuffer_;
123};
124
125class HardwareHeapFactory {
126public:
127    static HardwareHeapFactory &GetInstance();
128    int32_t GetHardwareHeapFd() const;
129
130private:
131    HardwareHeapFactory();
132    ~HardwareHeapFactory();
133    int32_t dmaHeapFd_ = -1;
134};
135
136class AVBufferFrameworkUnitTest : public testing::Test {
137public:
138    static void SetUpTestCase(void);
139    static void TearDownTestCase(void);
140    void SetUp(void);
141    void TearDown(void);
142
143private:
144    std::shared_ptr<AVBufferMock> buffer_;
145};
146} // namespace AVBufferUT
147} // namespace Media
148} // namespace OHOS
149#endif // AVBUFFER_UNITTEST_H