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 #include <sys/mman.h>
18 
19 #include "ohos_adapter_helper.h"
20 
21 #define private public
22 #include "ohos_native_buffer_adapter_impl.h"
23 #include "ohos_native_buffer_adapter.h"
24 #include "foundation/graphic/graphic_surface/interfaces/inner_api/surface/external_window.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace NWeb {
31 
32 class NativeBufferAdapterImplTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38 };
39 
SetUpTestCase()40 void NativeBufferAdapterImplTest::SetUpTestCase() {}
41 
TearDownTestCase()42 void NativeBufferAdapterImplTest::TearDownTestCase() {}
43 
SetUp()44 void NativeBufferAdapterImplTest::SetUp() {}
45 
TearDown()46 void NativeBufferAdapterImplTest::TearDown() {}
47 
48 
TestAllocate(void ** outBuffer)49 void TestAllocate(void** outBuffer)
50 {
51     OH_NativeBuffer_Config config = {
52         .width = 10,
53         .height = 10,
54         .format = OH_NativeBuffer_Format::NATIVEBUFFER_PIXEL_FMT_RGBA_8888,
55         .usage = 1,
56     };
57 
58     OH_NativeBuffer* buffer = OH_NativeBuffer_Alloc(&config);
59     if (buffer != nullptr) {
60         *outBuffer = buffer;
61     } else {
62         *outBuffer = nullptr;
63     }
64 }
65 
66 /**
67  * @tc.name: NativeBufferAdapterImplTest_001.
68  * @tc.desc: test FlowbufferAdapterImpl AcquireBuffer.
69  * @tc.type: FUNC.
70  * @tc.require:
71  */
72 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_001, TestSize.Level1)
73 {
74     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
75     EXPECT_TRUE(nativebufferAdapter != nullptr);
76 
77     void* buffer = nullptr;
78     nativebufferAdapter->AcquireBuffer(buffer);
79 
80     void* nativeBuffer = nullptr;
81     TestAllocate(&nativeBuffer);
82     EXPECT_NE(nativeBuffer, nullptr);
83     nativebufferAdapter->AcquireBuffer(nativeBuffer);
84 }
85 
86 
87 /**
88  * @tc.name: NativeBufferAdapterImplTest_002.
89  * @tc.desc: test FlowbufferAdapterImpl Release.
90  * @tc.type: FUNC.
91  * @tc.require:
92  */
93 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_002, TestSize.Level1)
94 {
95     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
96     EXPECT_TRUE(nativebufferAdapter != nullptr);
97 
98     void* buffer = nullptr;
99     nativebufferAdapter->Release(buffer);
100 
101     void* nativeBuffer = nullptr;
102     TestAllocate(&nativeBuffer);
103     EXPECT_NE(nativeBuffer, nullptr);
104     nativebufferAdapter->Release(nativeBuffer);
105 }
106 
107 
108 /**
109  * @tc.name: NativeBufferAdapterImplTest_003.
110  * @tc.desc: test FlowbufferAdapterImpl GetEGLBuffer.
111  * @tc.type: FUNC.
112  * @tc.require:
113  */
114 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_003, TestSize.Level1)
115 {
116     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
117     EXPECT_TRUE(nativebufferAdapter != nullptr);
118 
119     void* buffer = nullptr;
120     void* eglBuffer = nullptr;
121     int ret = nativebufferAdapter->GetEGLBuffer(buffer, &eglBuffer);
122     EXPECT_EQ(ret, -1);
123 
124     void* nativeBuffer = nullptr;
125     TestAllocate(&nativeBuffer);
126     EXPECT_NE(nativeBuffer, nullptr);
127     ret = nativebufferAdapter->GetEGLBuffer(nativeBuffer, &eglBuffer);
128     EXPECT_NE(eglBuffer, nullptr);
129     EXPECT_EQ(ret, 0);
130 
131     nativebufferAdapter->FreeEGLBuffer(eglBuffer);
132 }
133 
134 /**
135  * @tc.name: NativeBufferAdapterImplTest_004.
136  * @tc.desc: test FlowbufferAdapterImpl FreeEGLBuffer.
137  * @tc.type: FUNC.
138  * @tc.require:
139  */
140 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_004, TestSize.Level1)
141 {
142     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
143     EXPECT_TRUE(nativebufferAdapter != nullptr);
144 
145     void* eglBuffer = nullptr;
146     int ret = nativebufferAdapter->FreeEGLBuffer(eglBuffer);
147     EXPECT_EQ(ret, -1);
148 
149     void* nativeBuffer = nullptr;
150     TestAllocate(&nativeBuffer);
151     EXPECT_NE(nativeBuffer, nullptr);
152     nativebufferAdapter->GetEGLBuffer(nativeBuffer, &eglBuffer);
153     ret = nativebufferAdapter->FreeEGLBuffer(eglBuffer);
154     EXPECT_EQ(ret, 0);
155 }
156 
157 /**
158  * @tc.name: NativeBufferAdapterImplTest_005.
159  * @tc.desc: test FlowbufferAdapterImpl NativeBufferFromNativeWindowBuffer.
160  * @tc.type: FUNC.
161  * @tc.require:
162  */
163 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_005, TestSize.Level1)
164 {
165     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
166     EXPECT_TRUE(nativebufferAdapter != nullptr);
167 
168     void* nativeWindowBuffer = nullptr;
169     void* nativeBuffer = nullptr;
170     int ret = nativebufferAdapter->NativeBufferFromNativeWindowBuffer(nativeWindowBuffer, &nativeBuffer);
171     EXPECT_EQ(ret, -1);
172 }
173 
174 /**
175  * @tc.name: NativeBufferAdapterImplTest_006.
176  * @tc.desc: test FlowbufferAdapterImpl GetSeqNum.
177  * @tc.type: FUNC.
178  * @tc.require:
179  */
180 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_006, TestSize.Level1)
181 {
182     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
183     EXPECT_TRUE(nativebufferAdapter != nullptr);
184 
185     void* buffer = nullptr;
186     int seqnum = nativebufferAdapter->GetSeqNum(buffer);
187     EXPECT_EQ(seqnum, 0);
188 
189     void* nativeBuffer = nullptr;
190     TestAllocate(&nativeBuffer);
191     EXPECT_NE(nativeBuffer, nullptr);
192     seqnum = nativebufferAdapter->GetSeqNum(nativeBuffer);
193     EXPECT_NE(seqnum, 0);
194 }
195 } // namespace NWeb
196 } // namespace OHOS
197