• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include <gtest/gtest.h>
16 #include "iconsumer_surface.h"
17 #include <iservice_registry.h>
18 #include "native_buffer.h"
19 #include "native_buffer_inner.h"
20 #include "native_window.h"
21 #include "surface_type.h"
22 #include "graphic_common_c.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS::Rosen {
28 class BufferConsumerListener : public IBufferConsumerListener {
29 public:
OnBufferAvailable()30     void OnBufferAvailable() override
31     {
32     }
33 };
34 
35 class NativeBufferTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39 
40     static inline OH_NativeBuffer_Config config = {
41         .width = 0x100,
42         .height = 0x100,
43         .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
44         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
45     };
46     static inline OH_NativeBuffer_Config checkConfig = {};
47     static inline OH_NativeBuffer* buffer = nullptr;
48 };
49 
SetUpTestCase()50 void NativeBufferTest::SetUpTestCase()
51 {
52     buffer = nullptr;
53 }
54 
TearDownTestCase()55 void NativeBufferTest::TearDownTestCase()
56 {
57     buffer = nullptr;
58 }
59 
60 /*
61 * Function: OH_NativeBuffer_Alloc
62 * Type: Function
63 * Rank: Important(2)
64 * EnvConditions: N/A
65 * CaseDescription: 1. call OH_NativeBuffer_Alloc by abnormal input
66 *                  2. check ret
67 */
68 HWTEST_F(NativeBufferTest, OHNativeBufferAlloc001, Function | MediumTest | Level2)
69 {
70     buffer = OH_NativeBuffer_Alloc(nullptr);
71     ASSERT_EQ(buffer, nullptr);
72 }
73 
74 /*
75 * Function: OH_NativeBuffer_Alloc
76 * Type: Function
77 * Rank: Important(2)
78 * EnvConditions: N/A
79 * CaseDescription: 1. call OH_NativeBuffer_Alloc
80 *                  2. check ret
81 */
82 HWTEST_F(NativeBufferTest, OHNativeBufferAlloc002, Function | MediumTest | Level2)
83 {
84     buffer = OH_NativeBuffer_Alloc(&config);
85     ASSERT_NE(buffer, nullptr);
86 }
87 
88 /*
89 * Function: OH_NativeBuffer_GetSeqNum
90 * Type: Function
91 * Rank: Important(2)
92 * EnvConditions: N/A
93 * CaseDescription: 1. call OH_NativeBuffer_GetSeqNum by abnormal input
94 *                  2. check ret
95 */
96 HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum001, Function | MediumTest | Level2)
97 {
98     uint32_t id = OH_NativeBuffer_GetSeqNum(nullptr);
99     ASSERT_EQ(id, GSERROR_INVALID_ARGUMENTS);
100 }
101 
102 /*
103 * Function: OH_NativeBuffer_GetSeqNum
104 * Type: Function
105 * Rank: Important(2)
106 * EnvConditions: N/A
107 * CaseDescription: 1. call OH_NativeBuffer_GetSeqNum
108 *                  2. check ret
109 */
110 HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum002, Function | MediumTest | Level2)
111 {
112     uint32_t id = OH_NativeBuffer_GetSeqNum(buffer);
113     ASSERT_NE(id, GSERROR_INVALID_ARGUMENTS);
114 }
115 
116 /*
117 * Function: OH_NativeBuffer_GetConfig
118 * Type: Function
119 * Rank: Important(2)
120 * EnvConditions: N/A
121 * CaseDescription: 1. call OH_NativeBuffer_GetConfig
122 *                  2. check ret
123  */
124 HWTEST_F(NativeBufferTest, OHNativeBufferGetConfig001, Function | MediumTest | Level2)
125 {
126     OH_NativeBuffer_GetConfig(buffer, &checkConfig);
127     ASSERT_NE(&checkConfig, nullptr);
128 }
129 
130 /*
131 * Function: OH_NativeBuffer_GetConfig
132 * Type: Function
133 * Rank: Important(2)
134 * EnvConditions: N/A
135 * CaseDescription: 1. call OH_NativeBuffer_GetConfig by abnormal input
136 *                  2. check ret
137 */
138 HWTEST_F(NativeBufferTest, OHNativeBufferGetConfig002, Function | MediumTest | Level2)
139 {
140     checkConfig.width = 0x0;
141     checkConfig.height = 0x0;
142     checkConfig.format = 0x0;
143     checkConfig.usage = 0x0;
144     OH_NativeBuffer_GetConfig(nullptr, &checkConfig);
145     ASSERT_EQ(checkConfig.width, 0x0);
146     ASSERT_EQ(checkConfig.height, 0x0);
147     ASSERT_EQ(checkConfig.format, 0x0);
148     ASSERT_EQ(checkConfig.usage, 0x0);
149 }
150 
151 /*
152 * Function: OH_NativeBuffer_Reference
153 * Type: Function
154 * Rank: Important(2)
155 * EnvConditions: N/A
156 * CaseDescription: 1. call OH_NativeBuffer_Reference by abnormal input
157 *                  2. check ret
158 */
159 HWTEST_F(NativeBufferTest, OHNativeBufferReference001, Function | MediumTest | Level2)
160 {
161     int32_t ret = OH_NativeBuffer_Reference(nullptr);
162     ASSERT_NE(ret, GSERROR_OK);
163 }
164 
165 /*
166 * Function: OH_NativeBuffer_Reference
167 * Type: Function
168 * Rank: Important(2)
169 * EnvConditions: N/A
170 * CaseDescription: 1. call OH_NativeBuffer_Reference
171 *                  2. check ret
172 */
173 HWTEST_F(NativeBufferTest, OHNativeBufferReference002, Function | MediumTest | Level2)
174 {
175     int32_t ret = OH_NativeBuffer_Reference(buffer);
176     ASSERT_EQ(ret, GSERROR_OK);
177 }
178 
179 /*
180 * Function: OH_NativeBuffer_Unreference
181 * Type: Function
182 * Rank: Important(2)
183 * EnvConditions: N/A
184 * CaseDescription: 1. call OH_NativeBuffer_Unreference by abnormal input
185 *                  2. check ret
186 */
187 HWTEST_F(NativeBufferTest, OHNativeBufferUnreference001, Function | MediumTest | Level2)
188 {
189     int32_t ret = OH_NativeBuffer_Unreference(nullptr);
190     ASSERT_NE(ret, GSERROR_OK);
191 }
192 
193 /*
194 * Function: OH_NativeBuffer_Unreference
195 * Type: Function
196 * Rank: Important(2)
197 * EnvConditions: N/A
198 * CaseDescription: 1. call OH_NativeBuffer_Unreference
199 *                  2. check ret
200 */
201 HWTEST_F(NativeBufferTest, OHNativeBufferUnreference002, Function | MediumTest | Level2)
202 {
203     int32_t ret = OH_NativeBuffer_Unreference(buffer);
204     ASSERT_EQ(ret, GSERROR_OK);
205 }
206 
207 /*
208 * Function: OH_NativeBuffer_GetSeqNum and OH_NativeBuffer_Unreference
209 * Type: Function
210 * Rank: Important(2)
211 * EnvConditions: N/A
212 * CaseDescription: 1. call OH_NativeBuffer_GetSeqNum
213 *                  2. call OH_NativeBuffer_Unreference
214 *                  3. OH_NativeBuffer_Alloc again
215 *                  4. check OH_NativeBuffer_GetSeqNum = oldSeq + 1
216 */
217 HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum003, Function | MediumTest | Level2)
218 {
219     uint32_t oldSeq = OH_NativeBuffer_GetSeqNum(buffer);
220     int32_t ret = OH_NativeBuffer_Unreference(buffer);
221     ASSERT_EQ(ret, GSERROR_OK);
222     buffer = OH_NativeBuffer_Alloc(&config);
223     ASSERT_EQ(oldSeq + 1, OH_NativeBuffer_GetSeqNum(buffer));
224 }
225 
226 /*
227 * Function: OH_NativeBuffer_GetBufferHandle
228 * Type: Function
229 * Rank: Important(2)
230 * EnvConditions: N/A
231 * CaseDescription: 1. call OH_NativeBuffer_GetBufferHandle
232 *                  2. check result
233 */
234 HWTEST_F(NativeBufferTest, OHNativeBufferGetBufferHandle001, Function | MediumTest | Level2)
235 {
236     const BufferHandle* handle = OH_NativeBuffer_GetBufferHandle(buffer);
237     ASSERT_NE(handle, nullptr);
238     int32_t ret = FreeBufferHandle(nullptr);
239     ASSERT_EQ(ret, 0);
240     BufferHandle* cloneHandle = CloneBufferHandle(nullptr);
241     ASSERT_EQ(cloneHandle, nullptr);
242     cloneHandle = CloneBufferHandle(handle);
243     ASSERT_NE(cloneHandle, nullptr);
244     ASSERT_NE(handle, nullptr);
245     ASSERT_EQ(cloneHandle->width, handle->width);
246 }
247 
248 /*
249 * Function: OH_NativeBuffer_GetNativeBufferConfig
250 * Type: Function
251 * Rank: Important(2)
252 * EnvConditions: N/A
253 * CaseDescription: 1. call OH_NativeBuffer_GetNativeBufferConfig
254 *                  2. check result
255 */
256 HWTEST_F(NativeBufferTest, OHNativeBufferGetNativeBufferConfig001, Function | MediumTest | Level2)
257 {
258     OH_NativeBuffer_Config testConfig = {};
259     OH_NativeBuffer_GetNativeBufferConfig(buffer, &testConfig);
260     ASSERT_EQ(testConfig.width, config.width);
261     ASSERT_EQ(testConfig.height, config.height);
262     ASSERT_EQ(testConfig.format, config.format);
263     ASSERT_EQ(testConfig.usage, config.usage);
264 }
265 
266 /*
267 * Function: OH_NativeBuffer_Map
268 * Type: Function
269 * Rank: Important(2)
270 * EnvConditions: N/A
271 * CaseDescription: 1. call OH_NativeBuffer_Map by abnormal input
272 *                  2. check ret
273 */
274 HWTEST_F(NativeBufferTest, OHNativeBufferMap001, Function | MediumTest | Level2)
275 {
276     void *virAddr = nullptr;
277     int32_t ret = OH_NativeBuffer_Map(nullptr, &virAddr);
278     ASSERT_NE(ret, GSERROR_OK);
279 }
280 
281 /*
282 * Function: OH_NativeBuffer_Map
283 * Type: Function
284 * Rank: Important(2)
285 * EnvConditions: N/A
286 * CaseDescription: 1. call OH_NativeBuffer_Map
287 *                  2. check ret
288 */
289 HWTEST_F(NativeBufferTest, OHNativeBufferMap002, Function | MediumTest | Level2)
290 {
291     void *virAddr = nullptr;
292     int32_t ret = OH_NativeBuffer_Map(buffer, &virAddr);
293     ASSERT_EQ(ret, GSERROR_OK);
294     ASSERT_NE(virAddr, nullptr);
295 }
296 
297 /*
298 * Function: OH_NativeBuffer_Unmap
299 * Type: Function
300 * Rank: Important(2)
301 * EnvConditions: N/A
302 * CaseDescription: 1. call OH_NativeBuffer_Unmap by abnormal input
303 *                  2. check ret
304 */
305 HWTEST_F(NativeBufferTest, OHNativeBufferUnmap001, Function | MediumTest | Level2)
306 {
307     int32_t ret = OH_NativeBuffer_Unmap(nullptr);
308     ASSERT_NE(ret, GSERROR_OK);
309 }
310 
311 /*
312 * Function: OH_NativeBuffer_Unmap and OH_NativeBuffer_Unreference
313 * Type: Function
314 * Rank: Important(2)
315 * EnvConditions: N/A
316 * CaseDescription: 1. call OH_NativeBuffer_Unmap
317 *                  2. check ret
318 *                  3. call OH_NativeBuffer_Unreference
319 */
320 HWTEST_F(NativeBufferTest, OHNativeBufferUnmap002, Function | MediumTest | Level2)
321 {
322     int32_t ret = OH_NativeBuffer_Unmap(buffer);
323     ASSERT_EQ(ret, GSERROR_OK);
324     ret = OH_NativeBuffer_Unreference(buffer);
325     ASSERT_EQ(ret, GSERROR_OK);
326 }
327 
328 /*
329 * Function: OH_NativeBufferFromNativeWindowBuffer
330 * Type: Function
331 * Rank: Important(2)
332 * EnvConditions: N/A
333 * CaseDescription: 1. call OH_NativeBufferFromNativeWindowBuffer by abnormal input
334 *                  2. check ret
335 */
336 HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer001, Function | MediumTest | Level2)
337 {
338     OH_NativeBuffer* nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(nullptr);
339     ASSERT_EQ(nativeBuffer, nullptr);
340 }
341 
342 /*
343 * Function: OH_NativeBufferFromNativeWindowBuffer
344 * Type: Function
345 * Rank: Important(2)
346 * EnvConditions: N/A
347 * CaseDescription: 1. call OH_NativeBufferFromNativeWindowBuffer
348 *                  2. check ret
349 */
350 HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer002, Function | MediumTest | Level2)
351 {
352     sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create();
353     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
354     cSurface->RegisterConsumerListener(listener);
355     sptr<OHOS::IBufferProducer> producer = cSurface->GetProducer();
356     sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
357     int32_t fence;
358     sptr<OHOS::SurfaceBuffer> sBuffer = nullptr;
359     BufferRequestConfig requestConfig = {
360         .width = 0x100,  // small
361         .height = 0x100, // small
362         .strideAlignment = 0x8,
363         .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
364         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
365         .timeout = 0,
366     };
367     pSurface->RequestBuffer(sBuffer, fence, requestConfig);
368     NativeWindow* nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
369     ASSERT_NE(nativeWindow, nullptr);
370     NativeWindowBuffer* nativeWindowBuffer = OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(&sBuffer);
371     ASSERT_NE(nativeWindowBuffer, nullptr);
372     OH_NativeBuffer* nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(nativeWindowBuffer);
373     ASSERT_NE(nativeBuffer, nullptr);
374 
375     sBuffer = nullptr;
376     cSurface = nullptr;
377     producer = nullptr;
378     pSurface = nullptr;
379     nativeWindow = nullptr;
380     nativeWindowBuffer = nullptr;
381 }
382 }