• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 
196 /**
197  * @tc.name: NativeBufferAdapterImplTest_007.
198  * @tc.desc: test FlowbufferAdapterImpl Allocate.
199  * @tc.type: FUNC.
200  * @tc.require:
201  */
202 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_007, TestSize.Level1)
203 {
204     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
205     EXPECT_TRUE(nativebufferAdapter != nullptr);
206 
207     void* outBuffer = nullptr;
208     nativebufferAdapter->Allocate(nullptr, &outBuffer);
209     EXPECT_EQ(outBuffer, nullptr);
210 }
211 
212 /**
213  * @tc.name: NativeBufferAdapterImplTest_008.
214  * @tc.desc: test FlowbufferAdapterImpl Describe.
215  * @tc.type: FUNC.
216  * @tc.require:
217  */
218 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_008, TestSize.Level1)
219 {
220     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
221     EXPECT_TRUE(nativebufferAdapter != nullptr);
222 
223     void* buffer = nullptr;
224     std::shared_ptr<NativeBufferConfigAdapter> bufferConfig = nullptr;
225     nativebufferAdapter->Describe(bufferConfig, buffer);
226     EXPECT_EQ(bufferConfig, nullptr);
227 }
228 
229 /**
230  * @tc.name: NativeBufferAdapterImplTest_009.
231  * @tc.desc: test FlowbufferAdapterImpl Lock.
232  * @tc.type: FUNC.
233  * @tc.require:
234  */
235 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_009, TestSize.Level1)
236 {
237     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
238     EXPECT_TRUE(nativebufferAdapter != nullptr);
239 
240     void* buffer = nullptr;
241     void* out_virtual_address = nullptr;
242     int ret = nativebufferAdapter->Lock(buffer, 0, 0, &out_virtual_address);
243     EXPECT_EQ(ret, -1);
244 
245     OH_NativeBuffer_Config config = {
246         .width = 1920,
247         .height = 1080,
248         .format = OH_NativeBuffer_Format::NATIVEBUFFER_PIXEL_FMT_RGBA_8888,
249         .usage = OH_NativeBuffer_Usage::NATIVEBUFFER_USAGE_HW_RENDER,
250     };
251     buffer = OH_NativeBuffer_Alloc(&config);
252     ret = nativebufferAdapter->Lock(buffer, 0, 0, &out_virtual_address);
253     EXPECT_EQ(ret, 0);
254 }
255 
256 /**
257  * @tc.name: NativeBufferAdapterImplTest_010.
258  * @tc.desc: test FlowbufferAdapterImpl RecvHandleFromUnixSocket.
259  * @tc.type: FUNC.
260  * @tc.require:
261  */
262 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_010, TestSize.Level1)
263 {
264     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
265     EXPECT_TRUE(nativebufferAdapter != nullptr);
266 
267     void* outBuffer = nullptr;
268     int ret = nativebufferAdapter->RecvHandleFromUnixSocket(-1, &outBuffer);
269     EXPECT_EQ(outBuffer, nullptr);
270     EXPECT_EQ(ret, 0);
271 }
272 
273 /**
274  * @tc.name: NativeBufferAdapterImplTest_011.
275  * @tc.desc: test FlowbufferAdapterImpl SendHandleToUnixSocket.
276  * @tc.type: FUNC.
277  * @tc.require:
278  */
279 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_011, TestSize.Level1)
280 {
281     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
282     EXPECT_TRUE(nativebufferAdapter != nullptr);
283 
284     void* buffer = nullptr;
285     int ret = nativebufferAdapter->SendHandleToUnixSocket(buffer, -1);
286     EXPECT_EQ(ret, -1);
287 }
288 
289 /**
290  * @tc.name: NativeBufferAdapterImplTest_012.
291  * @tc.desc: test FlowbufferAdapterImpl Unlock.
292  * @tc.type: FUNC.
293  * @tc.require:
294  */
295 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_012, TestSize.Level1)
296 {
297     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
298     EXPECT_TRUE(nativebufferAdapter != nullptr);
299 
300     void* buffer = nullptr;
301     int ret = nativebufferAdapter->SendHandleToUnixSocket(buffer, -1);
302     EXPECT_EQ(ret, -1);
303 
304     OH_NativeBuffer_Config config = {
305         .width = 1920,
306         .height = 1080,
307         .format = OH_NativeBuffer_Format::NATIVEBUFFER_PIXEL_FMT_RGBA_8888,
308         .usage = OH_NativeBuffer_Usage::NATIVEBUFFER_USAGE_HW_RENDER,
309     };
310     buffer = OH_NativeBuffer_Alloc(&config);
311     int* fence = new int(-1);
312     ret = nativebufferAdapter->Unlock(buffer, fence);
313     EXPECT_EQ(ret, -1);
314 }
315 
316 /**
317  * @tc.name: NativeBufferAdapterImplTest_013.
318  * @tc.desc: test FlowbufferAdapterImpl FreeNativeBuffer.
319  * @tc.type: FUNC.
320  * @tc.require:
321  */
322 HWTEST_F(NativeBufferAdapterImplTest, NativeBufferAdapterImplTest_013, TestSize.Level1)
323 {
324     std::shared_ptr<OhosNativeBufferAdapterImpl> nativebufferAdapter = std::make_shared<OhosNativeBufferAdapterImpl>();
325     EXPECT_TRUE(nativebufferAdapter != nullptr);
326 
327     void* nativeBuffer = nullptr;
328     int ret = nativebufferAdapter->FreeNativeBuffer(nativeBuffer);
329     EXPECT_EQ(ret, -1);
330 
331     OH_NativeBuffer_Config config = {
332         .width = 1920,
333         .height = 1080,
334         .format = OH_NativeBuffer_Format::NATIVEBUFFER_PIXEL_FMT_RGBA_8888,
335         .usage = OH_NativeBuffer_Usage::NATIVEBUFFER_USAGE_HW_RENDER,
336     };
337     nativeBuffer = OH_NativeBuffer_Alloc(&config);
338     ret = nativebufferAdapter->FreeNativeBuffer(nativeBuffer);
339     EXPECT_EQ(ret, 0);
340 }
341 } // namespace NWeb
342 } // namespace OHOS
343