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
35 namespace OHOS {
36 namespace Media {
37 class AVBufferMock;
38 namespace AVBufferUT {
39 const std::string_view INT_CAPI_TESTKEY = "IntKey";
40 const std::string_view LONG_CAPI_TESTKEY = "LongKey";
41 const std::string_view FlOAT_CAPI_TESTKEY = "FloatKey";
42 const std::string_view DOUBLE_CAPI_TESTKEY = "DoubleKey";
43 const std::string_view STRING_CAPI_TESTKEY = "StringKey";
44 
45 constexpr int32_t MEMSIZE = 1024 * 1024;
46 constexpr int32_t POSITION_ONE = 1024 * 64;
47 constexpr int32_t TEST_BUFFER_SIZE = 1048 * 1048 * 8;
48 constexpr int32_t TEST_LOOP_DEPTH = 10;
49 
50 const int32_t INTVALUE = 141;
51 const int64_t LONGVALUE = 115441;
52 const float FLOATVALUE = 1.25;
53 const double DOUBLEVALUE = 1.625;
54 const std::string STRINGVALUE = "STRING_TESTVALUE";
55 
56 const int32_t DEFAULT_OFFSET = 1000;
57 const int64_t DEFAULT_PTS = 33000;
58 const int64_t DEFAULT_DTS = 100;
59 const int64_t DEFAULT_DURATION = 1000;
60 const uint32_t DEFAULT_FLAG = 1 << 0;
61 
62 const int32_t DEFAULT_PIXELSIZE = 4;
63 const 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 
72 class AVBufferInnerUnitTest : public testing::Test {
73 public:
74     static void SetUpTestCase(void);
75     static void TearDownTestCase(void);
76     void SetUp(void);
77     void TearDown(void);
78 
79 private:
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 
125 class HardwareHeapFactory {
126 public:
127     static HardwareHeapFactory &GetInstance();
128     int32_t GetHardwareHeapFd() const;
129 
130 private:
131     HardwareHeapFactory();
132     ~HardwareHeapFactory();
133     int32_t dmaHeapFd_ = -1;
134 };
135 
136 class AVBufferFrameworkUnitTest : public testing::Test {
137 public:
138     static void SetUpTestCase(void);
139     static void TearDownTestCase(void);
140     void SetUp(void);
141     void TearDown(void);
142 
143 private:
144     std::shared_ptr<AVBufferMock> buffer_;
145 };
146 } // namespace AVBufferUT
147 } // namespace Media
148 } // namespace OHOS
149 #endif // AVBUFFER_UNITTEST_H