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 <ctime>
19 #include "native_buffer.h"
20 #include "native_window.h"
21 #include "surface_type.h"
22 #include "graphic_common_c.h"
23 #include "graphic_error_code.h"
24
25 using namespace std;
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS::Rosen {
30 class BufferConsumerListener : public IBufferConsumerListener {
31 public:
OnBufferAvailable()32 void OnBufferAvailable() override
33 {
34 }
35 };
36
37 class NativeBufferTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41
42 static inline OH_NativeBuffer_Config config = {
43 .width = 0x100,
44 .height = 0x100,
45 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
46 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
47 };
48 static inline OH_NativeBuffer_Config checkConfig = {};
49 static inline OH_NativeBuffer* buffer = nullptr;
50 };
51
SetUpTestCase()52 void NativeBufferTest::SetUpTestCase()
53 {
54 buffer = nullptr;
55 }
56
TearDownTestCase()57 void NativeBufferTest::TearDownTestCase()
58 {
59 buffer = nullptr;
60 }
61
62 /*
63 * @tc.name OHNativeBufferAlloc001
64 * @tc.desc call OH_NativeBuffer_Alloc by abnormal input
65 * @tc.size : MediumTest
66 * @tc.type : Function
67 * @tc.level : Level 2
68 */
69 HWTEST_F(NativeBufferTest, OHNativeBufferAlloc001, Function | MediumTest | Level2)
70 {
71 buffer = OH_NativeBuffer_Alloc(nullptr);
72 ASSERT_EQ(buffer, nullptr);
73 }
74
75 /*
76 * @tc.name OHNativeBufferAlloc002
77 * @tc.desc call OH_NativeBuffer_Alloc
78 * @tc.size : MediumTest
79 * @tc.type : Function
80 * @tc.level : Level 2
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 * @tc.name OHNativeBufferGetSeqNum001
90 * @tc.desc call OH_NativeBuffer_GetSeqNum by abnormal input
91 * @tc.size : MediumTest
92 * @tc.type : Function
93 * @tc.level : Level 2
94 */
95 HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum001, Function | MediumTest | Level2)
96 {
97 uint32_t id = OH_NativeBuffer_GetSeqNum(nullptr);
98 ASSERT_EQ(id, UINT_MAX);
99 }
100
101 /*
102 * @tc.name OHNativeBufferGetSeqNum002
103 * @tc.desc all OH_NativeBuffer_GetSeqNum
104 * @tc.size : MediumTest
105 * @tc.type : Function
106 * @tc.level : Level 2
107 */
108 HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum002, Function | MediumTest | Level2)
109 {
110 uint32_t id = OH_NativeBuffer_GetSeqNum(buffer);
111 ASSERT_NE(id, NATIVE_ERROR_INVALID_ARGUMENTS);
112 }
113
114 /*
115 * @tc.name OHNativeBufferGetConfig001
116 * @tc.desc call OH_NativeBuffer_GetConfig
117 * @tc.size : MediumTest
118 * @tc.type : Function
119 * @tc.level : Level 2
120 */
121 HWTEST_F(NativeBufferTest, OHNativeBufferGetConfig001, Function | MediumTest | Level2)
122 {
123 OH_NativeBuffer_GetConfig(buffer, &checkConfig);
124 ASSERT_NE(&checkConfig, nullptr);
125 }
126
127 /*
128 * @tc.name OHNativeBufferGetConfig002
129 * @tc.desc call OH_NativeBuffer_GetConfig
130 * @tc.size : MediumTest
131 * @tc.type : Function
132 * @tc.level : Level 2
133 */
134 HWTEST_F(NativeBufferTest, OHNativeBufferGetConfig002, Function | MediumTest | Level2)
135 {
136 checkConfig.width = 0x0;
137 checkConfig.height = 0x0;
138 checkConfig.format = 0x0;
139 checkConfig.usage = 0x0;
140 OH_NativeBuffer_GetConfig(nullptr, &checkConfig);
141 ASSERT_EQ(checkConfig.width, 0x0);
142 ASSERT_EQ(checkConfig.height, 0x0);
143 ASSERT_EQ(checkConfig.format, 0x0);
144 ASSERT_EQ(checkConfig.usage, 0x0);
145 }
146
147 /*
148 * @tc.name OHNativeBufferReference001
149 * @tc.desc call OH_NativeBuffer_Reference
150 * @tc.size : MediumTest
151 * @tc.type : Function
152 * @tc.level : Level 2
153 */
154 HWTEST_F(NativeBufferTest, OHNativeBufferReference001, Function | MediumTest | Level2)
155 {
156 int32_t ret = OH_NativeBuffer_Reference(nullptr);
157 ASSERT_NE(ret, NATIVE_ERROR_OK);
158 }
159
160 /*
161 * @tc.name OHNativeBufferReference002
162 * @tc.desc call OH_NativeBuffer_Reference
163 * @tc.size : MediumTest
164 * @tc.type : Function
165 * @tc.level : Level 2
166 */
167 HWTEST_F(NativeBufferTest, OHNativeBufferReference002, Function | MediumTest | Level2)
168 {
169 int32_t ret = OH_NativeBuffer_Reference(buffer);
170 ASSERT_EQ(ret, NATIVE_ERROR_OK);
171 }
172
173 /*
174 * @tc.name OHNativeBufferUnreference001
175 * @tc.desc call OH_NativeBuffer_Unreference by abnormal input
176 * @tc.size : MediumTest
177 * @tc.type : Function
178 * @tc.level : Level 2
179 */
180 HWTEST_F(NativeBufferTest, OHNativeBufferUnreference001, Function | MediumTest | Level2)
181 {
182 int32_t ret = OH_NativeBuffer_Unreference(nullptr);
183 ASSERT_NE(ret, NATIVE_ERROR_OK);
184 }
185
186 /*
187 * @tc.name OHNativeBufferUnreference002
188 * @tc.desc call OH_NativeBuffer_Unreference
189 * @tc.size : MediumTest
190 * @tc.type : Function
191 * @tc.level : Level 2
192 */
193 HWTEST_F(NativeBufferTest, OHNativeBufferUnreference002, Function | MediumTest | Level2)
194 {
195 int32_t ret = OH_NativeBuffer_Unreference(buffer);
196 ASSERT_EQ(ret, NATIVE_ERROR_OK);
197 }
198
199 /*
200 * @tc.name OHNativeBufferGetSeqNum003
201 * @tc.desc call OH_NativeBuffer_GetSeqNum
202 * @tc.size : MediumTest
203 * @tc.type : Function
204 * @tc.level : Level 2
205 */
206 HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum003, Function | MediumTest | Level2)
207 {
208 uint32_t oldSeq = OH_NativeBuffer_GetSeqNum(buffer);
209 int32_t ret = OH_NativeBuffer_Unreference(buffer);
210 ASSERT_EQ(ret, NATIVE_ERROR_OK);
211 buffer = OH_NativeBuffer_Alloc(&config);
212 ASSERT_EQ(oldSeq + 1, OH_NativeBuffer_GetSeqNum(buffer));
213 }
214
215 /*
216 * @tc.name OHNativeBufferSetColorSpace001
217 * @tc.desc call OH_NativeBuffer_SetColorSpace by abnormal input
218 * @tc.size : MediumTest
219 * @tc.type : Function
220 * @tc.level : Level 2
221 */
222 HWTEST_F(NativeBufferTest, OHNativeBufferSetColorSpace001, Function | MediumTest | Level2)
223 {
224 int32_t ret = OH_NativeBuffer_SetColorSpace(nullptr, OH_COLORSPACE_DISPLAY_BT2020_PQ);
225 ASSERT_NE(ret, NATIVE_ERROR_OK);
226 }
227
228 /*
229 * @tc.name OHNativeBufferSetColorSpace002
230 * @tc.desc call OH_NativeBuffer_SetColorSpace
231 * @tc.size : MediumTest
232 * @tc.type : Function
233 * @tc.level : Level 2
234 */
235 HWTEST_F(NativeBufferTest, OHNativeBufferSetColorSpace002, Function | MediumTest | Level2)
236 {
237 if (buffer == nullptr) {
238 buffer = OH_NativeBuffer_Alloc(&config);
239 ASSERT_NE(buffer, nullptr);
240 }
241
242 int32_t ret = OH_NativeBuffer_SetColorSpace(buffer, OH_COLORSPACE_BT709_LIMIT);
243 if (ret != NATIVE_ERROR_UNSUPPORTED) {
244 ASSERT_EQ(ret, NATIVE_ERROR_OK);
245 }
246 }
247 /*
248 * @tc.name OHNativeBufferGetColorSpace001
249 * @tc.desc call OH_NativeBuffer_GetColorSpace by abnormal input
250 * @tc.size : MediumTest
251 * @tc.type : Function
252 * @tc.level : Level 2
253 */
254 HWTEST_F(NativeBufferTest, OHNativeBufferGetColorSpace001, Function | MediumTest | Level2)
255 {
256 OH_NativeBuffer_ColorSpace *colorSpace = nullptr;
257 int32_t ret = OH_NativeBuffer_GetColorSpace(nullptr, colorSpace);
258 ASSERT_NE(ret, NATIVE_ERROR_OK);
259 }
260 /*
261 * @tc.name OHNativeBufferGetColorSpace002
262 * @tc.desc call OH_NativeBuffer_GetColorSpace
263 * @tc.size : MediumTest
264 * @tc.type : Function
265 * @tc.level : Level 2
266 */
267 HWTEST_F(NativeBufferTest, OHNativeBufferGetColorSpace002, Function | MediumTest | Level2)
268 {
269 if (buffer == nullptr) {
270 buffer = OH_NativeBuffer_Alloc(&config);
271 ASSERT_NE(buffer, nullptr);
272 }
273 OH_NativeBuffer_ColorSpace colorSpace = OH_COLORSPACE_NONE;
274 int32_t ret = OH_NativeBuffer_SetColorSpace(buffer, OH_COLORSPACE_BT709_LIMIT);
275 if (ret != NATIVE_ERROR_UNSUPPORTED) { // some device not support set colorspace
276 ASSERT_EQ(ret, NATIVE_ERROR_OK);
277 }
278 ret = OH_NativeBuffer_GetColorSpace(buffer, &colorSpace);
279 if (ret != NATIVE_ERROR_UNSUPPORTED) {
280 ASSERT_EQ(colorSpace, OH_COLORSPACE_BT709_LIMIT);
281 ASSERT_EQ(ret, NATIVE_ERROR_OK);
282 }
283 }
284 /*
285 * @tc.name OHNativeBufferGetColorSpace003
286 * @tc.desc call OH_NativeBuffer_GetColorSpace
287 * @tc.size : MediumTest
288 * @tc.type : Function
289 * @tc.level : Level 2
290 */
291 HWTEST_F(NativeBufferTest, OHNativeBufferGetColorSpace003, Function | MediumTest | Level2)
292 {
293 if (buffer == nullptr) {
294 buffer = OH_NativeBuffer_Alloc(&config);
295 ASSERT_NE(buffer, nullptr);
296 }
297 OH_NativeBuffer_ColorSpace colorSpace = OH_COLORSPACE_NONE;
298 int32_t ret = OH_NativeBuffer_GetColorSpace(buffer, &colorSpace);
299 if (ret != NATIVE_ERROR_UNSUPPORTED) { // some device not support set colorspace
300 ASSERT_EQ(ret, NATIVE_ERROR_OK);
301 }
302 ret = OH_NativeBuffer_SetColorSpace(buffer, OH_COLORSPACE_BT709_LIMIT);
303 if (ret != NATIVE_ERROR_UNSUPPORTED) { // some device not support set colorspace
304 ASSERT_EQ(colorSpace, OH_COLORSPACE_BT709_LIMIT);
305 ASSERT_EQ(ret, NATIVE_ERROR_OK);
306 }
307 }
308 /*
309 * @tc.name OH_NativeBuffer_SetMetadataValue001
310 * @tc.desc call OH_NativeBuffer_SetMetadataValue by abnormal input
311 * @tc.size : MediumTest
312 * @tc.type : Function
313 * @tc.level : Level 2
314 */
315 HWTEST_F(NativeBufferTest, OH_NativeBuffer_SetMetadataValue001, Function | MediumTest | Level2)
316 {
317 int32_t size = 1024;
318 uint8_t buff[size];
319 int32_t ret = OH_NativeBuffer_SetMetadataValue(nullptr, OH_HDR_STATIC_METADATA, size, buff);
320 ASSERT_NE(ret, NATIVE_ERROR_OK);
321 }
322 /*
323 * @tc.name OH_NativeBuffer_SetMetadataValue002
324 * @tc.desc call OH_NativeBuffer_SetMetadataValue
325 * @tc.size : MediumTest
326 * @tc.type : Function
327 * @tc.level : Level 2
328 */
329 HWTEST_F(NativeBufferTest, OH_NativeBuffer_SetMetadataValue002, Function | MediumTest | Level2)
330 {
331 if (buffer == nullptr) {
332 buffer = OH_NativeBuffer_Alloc(&config);
333 ASSERT_NE(buffer, nullptr);
334 }
335 int32_t len = 60;
336 uint8_t outbuff[len];
337 for (int i = 0; i < 60; ++i) {
338 outbuff[i] = static_cast<uint8_t>(i);
339 }
340 int32_t ret = OH_NativeBuffer_SetMetadataValue(buffer, OH_HDR_STATIC_METADATA, len, outbuff);
341 if (ret != NATIVE_ERROR_UNSUPPORTED) { // some device not support set colorspace
342 ASSERT_EQ(ret, NATIVE_ERROR_OK);
343 }
344 }
345 /*
346 * @tc.name OH_NativeBuffer_SetMetadataValue003
347 * @tc.desc call OH_NativeBuffer_SetMetadataValue by abnormal input
348 * @tc.size : MediumTest
349 * @tc.type : Function
350 * @tc.level : Level 2
351 */
352 HWTEST_F(NativeBufferTest, OH_NativeBuffer_SetMetadataValue003, Function | MediumTest | Level2)
353 {
354 int32_t maxSize = -1;
355 int32_t size = 60;
356 uint8_t buff[size];
357 int32_t ret = OH_NativeBuffer_SetMetadataValue(nullptr, OH_HDR_STATIC_METADATA, maxSize, buff);
358 if (ret != NATIVE_ERROR_UNSUPPORTED) { // some device not support set colorspace
359 ASSERT_NE(ret, NATIVE_ERROR_OK);
360 }
361 }
362 /*
363 * @tc.name OH_NativeBuffer_GetMetadataValue001
364 * @tc.desc call OH_NativeBuffer_GetMetadataValue by abnormal input
365 * @tc.size : MediumTest
366 * @tc.type : Function
367 * @tc.level : Level 2
368 */
369 HWTEST_F(NativeBufferTest, OH_NativeBuffer_GetMetadataValue001, Function | MediumTest | Level2)
370 {
371 int32_t size = 1024;
372 uint8_t *buff;
373 int32_t ret = OH_NativeBuffer_GetMetadataValue(nullptr, OH_HDR_STATIC_METADATA, &size, &buff);
374 if (buff != nullptr) {
375 delete[] buff;
376 }
377 if (ret != NATIVE_ERROR_UNSUPPORTED) { // some device not support set colorspace
378 ASSERT_NE(ret, NATIVE_ERROR_OK);
379 }
380 }
381 /*
382 * @tc.name OH_NativeBuffer_GetMetadataValue002
383 * @tc.desc call OH_NativeBuffer_GetMetadataValue
384 * @tc.size : MediumTest
385 * @tc.type : Function
386 * @tc.level : Level 2
387 */
388 HWTEST_F(NativeBufferTest, OH_NativeBuffer_GetMetadataValue002, Function | MediumTest | Level2)
389 {
390 if (buffer == nullptr) {
391 buffer = OH_NativeBuffer_Alloc(&config);
392 ASSERT_NE(buffer, nullptr);
393 }
394 int32_t len = 60;
395 uint8_t outbuff[len];
396 for (int i = 0; i < 60; ++i) {
397 outbuff[i] = static_cast<uint8_t>(i);
398 }
399 int32_t ret = OH_NativeBuffer_SetMetadataValue(buffer, OH_HDR_STATIC_METADATA, len, outbuff);
400 if (ret != NATIVE_ERROR_UNSUPPORTED) { // some device not support set colorspace
401 ASSERT_EQ(ret, NATIVE_ERROR_OK);
402 }
403 int32_t buffSize = 0;
404 uint8_t *buff;
405 ret = OH_NativeBuffer_GetMetadataValue(buffer, OH_HDR_STATIC_METADATA, &buffSize, &buff);
406 if (buff != nullptr) {
407 ASSERT_EQ(memcmp(outbuff, buff, 60), 0);
408 delete[] buff;
409 }
410 if (ret != NATIVE_ERROR_UNSUPPORTED) { // some device not support set colorspace
411 ASSERT_EQ(ret, NATIVE_ERROR_OK);
412 }
413 }
414 /*
415 * @tc.name OH_NativeBuffer_GetMetadataValue003
416 * @tc.desc call OH_NativeBuffer_GetMetadataValue
417 * @tc.size : MediumTest
418 * @tc.type : Function
419 * @tc.level : Level 2
420 */
421 HWTEST_F(NativeBufferTest, OH_NativeBuffer_GetMetadataValue003, Function | MediumTest | Level2)
422 {
423 if (buffer == nullptr) {
424 buffer = OH_NativeBuffer_Alloc(&config);
425 ASSERT_NE(buffer, nullptr);
426 }
427 uint8_t *buff;
428 int32_t ret = OH_NativeBuffer_GetMetadataValue(buffer, OH_HDR_STATIC_METADATA, nullptr, &buff);
429 if (ret != NATIVE_ERROR_UNSUPPORTED) { // some device not support set colorspace
430 ASSERT_NE(ret, NATIVE_ERROR_OK);
431 }
432 }
433
434 /*
435 * @tc.name OHNativeBufferMap001
436 * @tc.desc call OH_NativeBuffer_Map by abnormal input
437 * @tc.size : MediumTest
438 * @tc.type : Function
439 * @tc.level : Level 2
440 */
441 HWTEST_F(NativeBufferTest, OHNativeBufferMap001, Function | MediumTest | Level2)
442 {
443 void *virAddr = nullptr;
444 int32_t ret = OH_NativeBuffer_Map(nullptr, &virAddr);
445 ASSERT_NE(ret, NATIVE_ERROR_OK);
446 }
447
448 /*
449 * @tc.name OHNativeBufferMap002
450 * @tc.desc call OH_NativeBuffer_Map
451 * @tc.size : MediumTest
452 * @tc.type : Function
453 * @tc.level : Level 2
454 */
455 HWTEST_F(NativeBufferTest, OHNativeBufferMap002, Function | MediumTest | Level2)
456 {
457 void *virAddr = nullptr;
458 int32_t ret = OH_NativeBuffer_Map(buffer, &virAddr);
459 ASSERT_EQ(ret, NATIVE_ERROR_OK);
460 ASSERT_NE(virAddr, nullptr);
461 }
462
463 /*
464 * @tc.name OHNativeBufferUnmap001
465 * @tc.desc call OH_NativeBuffer_Unmap by abnormal input
466 * @tc.size : MediumTest
467 * @tc.type : Function
468 * @tc.level : Level 2
469 */
470 HWTEST_F(NativeBufferTest, OHNativeBufferUnmap001, Function | MediumTest | Level2)
471 {
472 int32_t ret = OH_NativeBuffer_Unmap(nullptr);
473 ASSERT_NE(ret, NATIVE_ERROR_OK);
474 }
475
476 /*
477 * @tc.name OHNativeBufferUnmap002
478 * @tc.desc call OH_NativeBuffer_Unmap
479 * @tc.size : MediumTest
480 * @tc.type : Function
481 * @tc.level : Level 2
482 */
483 HWTEST_F(NativeBufferTest, OHNativeBufferUnmap002, Function | MediumTest | Level2)
484 {
485 int32_t ret = OH_NativeBuffer_Unmap(buffer);
486 ASSERT_EQ(ret, NATIVE_ERROR_OK);
487 ret = OH_NativeBuffer_Unreference(buffer);
488 ASSERT_EQ(ret, NATIVE_ERROR_OK);
489 }
490
491 /*
492 * @tc.name NativeBufferFromNativeWindowBuffer002
493 * @tc.desc call OH_NativeBufferFromNativeWindowBuffer
494 * @tc.size : MediumTest
495 * @tc.type : Function
496 * @tc.level : Level 2
497 */
498 HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer002, Function | MediumTest | Level2)
499 {
500 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create();
501 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
502 cSurface->RegisterConsumerListener(listener);
503 sptr<OHOS::IBufferProducer> producer = cSurface->GetProducer();
504 sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
505 int32_t fence;
506 sptr<OHOS::SurfaceBuffer> sBuffer = nullptr;
507 BufferRequestConfig requestConfig = {
508 .width = 0x100, // small
509 .height = 0x100, // small
510 .strideAlignment = 0x8,
511 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
512 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
513 .timeout = 0,
514 };
515 pSurface->RequestBuffer(sBuffer, fence, requestConfig);
516 NativeWindow* nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
517 ASSERT_NE(nativeWindow, nullptr);
518 NativeWindowBuffer* nativeWindowBuffer = OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(&sBuffer);
519 ASSERT_NE(nativeWindowBuffer, nullptr);
520
521 int32_t ret = OH_NativeBuffer_FromNativeWindowBuffer(nativeWindowBuffer, nullptr);
522 ASSERT_EQ(ret, NATIVE_ERROR_INVALID_ARGUMENTS);
523 OH_NativeBuffer* nativeBufferTmp = nullptr;
524 ret = OH_NativeBuffer_FromNativeWindowBuffer(nativeWindowBuffer, &nativeBufferTmp);
525 ASSERT_EQ(ret, NATIVE_ERROR_OK);
526
527 sBuffer = nullptr;
528 cSurface = nullptr;
529 producer = nullptr;
530 pSurface = nullptr;
531 nativeWindow = nullptr;
532 nativeWindowBuffer = nullptr;
533 }
534
535 /*
536 * @tc.name NativeBufferFromNativeWindowBuffer003
537 * @tc.desc call OH_NativeBuffer_FromNativeWindowBuffer by abnormal input
538 * @tc.size : MediumTest
539 * @tc.type : Function
540 * @tc.level : Level 2
541 */
542 HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer003, Function | MediumTest | Level2)
543 {
544 int32_t ret = OH_NativeBuffer_FromNativeWindowBuffer(nullptr, nullptr);
545 ASSERT_EQ(ret, NATIVE_ERROR_INVALID_ARGUMENTS);
546
547 NativeWindowBuffer nativeWindowBuffer;
548 ret = OH_NativeBuffer_FromNativeWindowBuffer(&nativeWindowBuffer, nullptr);
549 ASSERT_EQ(ret, NATIVE_ERROR_INVALID_ARGUMENTS);
550 }
551
552 /*
553 * @tc.name OHNativeBufferMapPlanes001
554 * @tc.desc call OH_NativeBuffer_MapPlanes by abnormal input
555 * @tc.size : MediumTest
556 * @tc.type : Function
557 * @tc.level : Level 2
558 */
559 HWTEST_F(NativeBufferTest, OHNativeBufferMapPlanes001, Function | MediumTest | Level2)
560 {
561 int32_t ret = OH_NativeBuffer_MapPlanes(nullptr, nullptr, nullptr);
562 ASSERT_EQ(ret, NATIVE_ERROR_INVALID_ARGUMENTS);
563
564 OH_NativeBuffer *buffer = (OH_NativeBuffer *)0xFFFFFFFF;
565 ret = OH_NativeBuffer_MapPlanes(buffer, nullptr, nullptr);
566 ASSERT_EQ(ret, NATIVE_ERROR_INVALID_ARGUMENTS);
567
568 void *virAddr = nullptr;
569 ret = OH_NativeBuffer_MapPlanes(buffer, &virAddr, nullptr);
570 ASSERT_EQ(ret, NATIVE_ERROR_INVALID_ARGUMENTS);
571 }
572
573 /*
574 * @tc.name OHNativeBufferMapPlanes002
575 * @tc.desc call OH_NativeBuffer_MapPlanes
576 * @tc.size : MediumTest
577 * @tc.type : Function
578 * @tc.level : Level 2
579 */
580 HWTEST_F(NativeBufferTest, OHNativeBufferMapPlanes002, Function | MediumTest | Level2)
581 {
582 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create();
583 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
584 cSurface->RegisterConsumerListener(listener);
585 sptr<OHOS::IBufferProducer> producer = cSurface->GetProducer();
586 sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
587 int32_t fence;
588 sptr<OHOS::SurfaceBuffer> sBuffer = nullptr;
589 BufferRequestConfig requestConfig = {.width = 0x100, .height = 0x100, .strideAlignment = 0x8,
590 .format = GRAPHIC_PIXEL_FMT_YCBCR_420_SP,
591 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA, .timeout = 0};
592 pSurface->SetQueueSize(4);
593 int32_t formatType[] = {GRAPHIC_PIXEL_FMT_YCBCR_420_SP, GRAPHIC_PIXEL_FMT_YCRCB_420_SP,
594 GRAPHIC_PIXEL_FMT_YCBCR_420_P, GRAPHIC_PIXEL_FMT_YCRCB_420_P};
595 NativeWindow* nativeWindow;
596 NativeWindowBuffer* nativeWindowBuffer;
597 for (int32_t i = 0; i < sizeof(formatType) / sizeof(int32_t); i++) {
598 requestConfig.format = formatType[i];
599 pSurface->RequestBuffer(sBuffer, fence, requestConfig);
600 nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
601 ASSERT_NE(nativeWindow, nullptr);
602 nativeWindowBuffer = OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(&sBuffer);
603 ASSERT_NE(nativeWindowBuffer, nullptr);
604 }
605
606 sBuffer = nullptr;
607 cSurface = nullptr;
608 producer = nullptr;
609 pSurface = nullptr;
610 nativeWindow = nullptr;
611 nativeWindowBuffer = nullptr;
612 }
613
614 /*
615 * @tc.name OHNativeBufferMapPlanes003
616 * @tc.desc call OH_NativeBuffer_MapPlanes
617 * @tc.size : MediumTest
618 * @tc.type : Function
619 * @tc.level : Level 2
620 */
621 HWTEST_F(NativeBufferTest, OHNativeBufferMapPlanes003, Function | MediumTest | Level2)
622 {
623 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create();
624 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
625 cSurface->RegisterConsumerListener(listener);
626 sptr<OHOS::IBufferProducer> producer = cSurface->GetProducer();
627 sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
628 int32_t fence;
629 sptr<OHOS::SurfaceBuffer> sBuffer = nullptr;
630 BufferRequestConfig requestConfig = {.width = 0x100, .height = 0x100, .strideAlignment = 0x8,
631 .format = GRAPHIC_PIXEL_FMT_YCBCR_420_SP,
632 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA, .timeout = 0};
633 pSurface->SetQueueSize(4);
634 int32_t formatType = GRAPHIC_PIXEL_FMT_YCBCR_420_SP;
635 NativeWindow* nativeWindow;
636 NativeWindowBuffer* nativeWindowBuffer;
637 requestConfig.format = formatType;
638 pSurface->RequestBuffer(sBuffer, fence, requestConfig);
639 nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
640 ASSERT_NE(nativeWindow, nullptr);
641 nativeWindowBuffer = OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(&sBuffer);
642 ASSERT_NE(nativeWindowBuffer, nullptr);
643
644 OH_NativeBuffer* nativeBufferTmp = nullptr;
645 for (int32_t i = 0; i < 1000; i++) {
646 int32_t ret = OH_NativeBuffer_FromNativeWindowBuffer(nativeWindowBuffer, &nativeBufferTmp);
647 ASSERT_EQ(ret, NATIVE_ERROR_OK);
648 }
649
650 sBuffer = nullptr;
651 cSurface = nullptr;
652 producer = nullptr;
653 pSurface = nullptr;
654 nativeWindow = nullptr;
655 nativeWindowBuffer = nullptr;
656 }
657 }