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_buffer_inner.h"
21 #include "native_window.h"
22 #include "surface_type.h"
23 #include "graphic_common_c.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:
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 * Function: OH_NativeBufferFromSurfaceBuffer
64 * Type: Function
65 * Rank: Important(2)
66 * EnvConditions: N/A
67 * CaseDescription: 1. call OH_NativeBufferFromSurfaceBuffer by abnormal input
68 *                  2. check ret
69 */
HWTEST_F(NativeBufferTest, OH_NativeBufferFromSurfaceBuffer001, Function | MediumTest | Level2)70 HWTEST_F(NativeBufferTest, OH_NativeBufferFromSurfaceBuffer001, Function | MediumTest | Level2)
71 {
72     sptr<OHOS::SurfaceBuffer> surfaceBuffer = OHOS::SurfaceBuffer::Create();
73     NativeWindowBuffer* nativeWindowBuffer = OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(&surfaceBuffer);
74     ASSERT_NE(nativeWindowBuffer, nullptr);
75     nativeWindowBuffer->sfbuffer = nullptr;
76     OH_NativeBuffer* nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(nativeWindowBuffer);
77     ASSERT_EQ(nativeBuffer, nullptr);
78 }
79 
80 /*
81 * Function: OH_NativeBuffer_Alloc
82 * Type: Function
83 * Rank: Important(2)
84 * EnvConditions: N/A
85 * CaseDescription: 1. call OH_NativeBuffer_Alloc by abnormal input
86 *                  2. check ret
87 */
HWTEST_F(NativeBufferTest, OHNativeBufferAlloc001, Function | MediumTest | Level2)88 HWTEST_F(NativeBufferTest, OHNativeBufferAlloc001, Function | MediumTest | Level2)
89 {
90     buffer = OH_NativeBuffer_Alloc(nullptr);
91     ASSERT_EQ(buffer, nullptr);
92 }
93 
94 /*
95 * Function: OH_NativeBuffer_Alloc
96 * Type: Function
97 * Rank: Important(2)
98 * EnvConditions: N/A
99 * CaseDescription: 1. call OH_NativeBuffer_Alloc
100 *                  2. check ret
101 */
HWTEST_F(NativeBufferTest, OHNativeBufferAlloc002, Function | MediumTest | Level2)102 HWTEST_F(NativeBufferTest, OHNativeBufferAlloc002, Function | MediumTest | Level2)
103 {
104     buffer = OH_NativeBuffer_Alloc(&config);
105     ASSERT_NE(buffer, nullptr);
106 }
107 
108 /*
109 * Function: OH_NativeBuffer_GetSeqNum
110 * Type: Function
111 * Rank: Important(2)
112 * EnvConditions: N/A
113 * CaseDescription: 1. call OH_NativeBuffer_GetSeqNum by abnormal input
114 *                  2. check ret
115 */
HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum001, Function | MediumTest | Level2)116 HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum001, Function | MediumTest | Level2)
117 {
118     uint32_t id = OH_NativeBuffer_GetSeqNum(nullptr);
119     ASSERT_EQ(id, UINT_MAX);
120 }
121 
122 /*
123 * Function: OH_NativeBuffer_GetSeqNum
124 * Type: Function
125 * Rank: Important(2)
126 * EnvConditions: N/A
127 * CaseDescription: 1. call OH_NativeBuffer_GetSeqNum
128 *                  2. check ret
129 */
HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum002, Function | MediumTest | Level2)130 HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum002, Function | MediumTest | Level2)
131 {
132     uint32_t id = OH_NativeBuffer_GetSeqNum(buffer);
133     ASSERT_NE(id, GSERROR_INVALID_ARGUMENTS);
134 }
135 
136 /*
137 * Function: OH_NativeBuffer_GetConfig
138 * Type: Function
139 * Rank: Important(2)
140 * EnvConditions: N/A
141 * CaseDescription: 1. call OH_NativeBuffer_GetConfig
142 *                  2. check ret
143  */
HWTEST_F(NativeBufferTest, OHNativeBufferGetConfig001, Function | MediumTest | Level2)144 HWTEST_F(NativeBufferTest, OHNativeBufferGetConfig001, Function | MediumTest | Level2)
145 {
146     OH_NativeBuffer_GetConfig(buffer, &checkConfig);
147     ASSERT_NE(&checkConfig, nullptr);
148 }
149 
150 /*
151 * Function: OH_NativeBuffer_GetConfig
152 * Type: Function
153 * Rank: Important(2)
154 * EnvConditions: N/A
155 * CaseDescription: 1. call OH_NativeBuffer_GetConfig by abnormal input
156 *                  2. check ret
157 */
HWTEST_F(NativeBufferTest, OHNativeBufferGetConfig002, Function | MediumTest | Level2)158 HWTEST_F(NativeBufferTest, OHNativeBufferGetConfig002, Function | MediumTest | Level2)
159 {
160     checkConfig.width = 0x0;
161     checkConfig.height = 0x0;
162     checkConfig.format = 0x0;
163     checkConfig.usage = 0x0;
164     OH_NativeBuffer_GetConfig(nullptr, &checkConfig);
165     ASSERT_EQ(checkConfig.width, 0x0);
166     ASSERT_EQ(checkConfig.height, 0x0);
167     ASSERT_EQ(checkConfig.format, 0x0);
168     ASSERT_EQ(checkConfig.usage, 0x0);
169 }
170 
171 /*
172 * Function: OH_NativeBuffer_Reference
173 * Type: Function
174 * Rank: Important(2)
175 * EnvConditions: N/A
176 * CaseDescription: 1. call OH_NativeBuffer_Reference by abnormal input
177 *                  2. check ret
178 */
HWTEST_F(NativeBufferTest, OHNativeBufferReference001, Function | MediumTest | Level2)179 HWTEST_F(NativeBufferTest, OHNativeBufferReference001, Function | MediumTest | Level2)
180 {
181     int32_t ret = OH_NativeBuffer_Reference(nullptr);
182     ASSERT_NE(ret, GSERROR_OK);
183 }
184 
185 /*
186 * Function: OH_NativeBuffer_Reference
187 * Type: Function
188 * Rank: Important(2)
189 * EnvConditions: N/A
190 * CaseDescription: 1. call OH_NativeBuffer_Reference
191 *                  2. check ret
192 */
HWTEST_F(NativeBufferTest, OHNativeBufferReference002, Function | MediumTest | Level2)193 HWTEST_F(NativeBufferTest, OHNativeBufferReference002, Function | MediumTest | Level2)
194 {
195     int32_t ret = OH_NativeBuffer_Reference(buffer);
196     ASSERT_EQ(ret, GSERROR_OK);
197 }
198 
199 /*
200 * Function: OH_NativeBuffer_Unreference
201 * Type: Function
202 * Rank: Important(2)
203 * EnvConditions: N/A
204 * CaseDescription: 1. call OH_NativeBuffer_Unreference by abnormal input
205 *                  2. check ret
206 */
HWTEST_F(NativeBufferTest, OHNativeBufferUnreference001, Function | MediumTest | Level2)207 HWTEST_F(NativeBufferTest, OHNativeBufferUnreference001, Function | MediumTest | Level2)
208 {
209     int32_t ret = OH_NativeBuffer_Unreference(nullptr);
210     ASSERT_NE(ret, GSERROR_OK);
211 }
212 
213 /*
214 * Function: OH_NativeBuffer_Unreference
215 * Type: Function
216 * Rank: Important(2)
217 * EnvConditions: N/A
218 * CaseDescription: 1. call OH_NativeBuffer_Unreference
219 *                  2. check ret
220 */
HWTEST_F(NativeBufferTest, OHNativeBufferUnreference002, Function | MediumTest | Level2)221 HWTEST_F(NativeBufferTest, OHNativeBufferUnreference002, Function | MediumTest | Level2)
222 {
223     int32_t ret = OH_NativeBuffer_Unreference(buffer);
224     ASSERT_EQ(ret, GSERROR_OK);
225 }
226 
227 /*
228 * Function: OH_NativeBuffer_GetSeqNum and OH_NativeBuffer_Unreference
229 * Type: Function
230 * Rank: Important(2)
231 * EnvConditions: N/A
232 * CaseDescription: 1. call OH_NativeBuffer_GetSeqNum
233 *                  2. call OH_NativeBuffer_Unreference
234 *                  3. OH_NativeBuffer_Alloc again
235 *                  4. check OH_NativeBuffer_GetSeqNum = oldSeq + 1
236 */
HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum003, Function | MediumTest | Level2)237 HWTEST_F(NativeBufferTest, OHNativeBufferGetSeqNum003, Function | MediumTest | Level2)
238 {
239     uint32_t oldSeq = OH_NativeBuffer_GetSeqNum(buffer);
240     int32_t ret = OH_NativeBuffer_Unreference(buffer);
241     ASSERT_EQ(ret, GSERROR_OK);
242     buffer = OH_NativeBuffer_Alloc(&config);
243     ASSERT_EQ(oldSeq + 1, OH_NativeBuffer_GetSeqNum(buffer));
244 }
245 
246 /*
247 * Function: OH_NativeBuffer_GetNativeBufferConfig
248 * Type: Function
249 * Rank: Important(2)
250 * EnvConditions: N/A
251 * CaseDescription: 1. call OH_NativeBuffer_GetNativeBufferConfig
252 *                  2. check result
253 */
HWTEST_F(NativeBufferTest, OHNativeBufferGetNativeBufferConfig001, Function | MediumTest | Level2)254 HWTEST_F(NativeBufferTest, OHNativeBufferGetNativeBufferConfig001, Function | MediumTest | Level2)
255 {
256     OH_NativeBuffer_Config testConfig = {};
257     OH_NativeBuffer_GetNativeBufferConfig(buffer, &testConfig);
258     ASSERT_EQ(testConfig.width, config.width);
259     ASSERT_EQ(testConfig.height, config.height);
260     ASSERT_EQ(testConfig.format, config.format);
261     ASSERT_EQ(testConfig.usage, config.usage);
262 }
263 
264 /*
265 * Function: OH_NativeBuffer_SetColorSpace
266 * Type: Function
267 * Rank: Important(2)
268 * EnvConditions: N/A
269 * CaseDescription: 1. call OH_NativeBuffer_SetColorSpace by abnormal input
270 *                  2. check ret
271 */
HWTEST_F(NativeBufferTest, OHNativeBufferSetColorSpace001, Function | MediumTest | Level2)272 HWTEST_F(NativeBufferTest, OHNativeBufferSetColorSpace001, Function | MediumTest | Level2)
273 {
274     int32_t ret = OH_NativeBuffer_SetColorSpace(nullptr, OH_COLORSPACE_DISPLAY_BT2020_PQ);
275     ASSERT_NE(ret, GSERROR_OK);
276 }
277 
278 /*
279 * Function: OH_NativeBuffer_SetColorSpace
280 * Type: Function
281 * Rank: Important(2)
282 * EnvConditions: N/A
283 * CaseDescription: 1. call OH_NativeBuffer_SetColorSpace
284 *                  2. check ret
285 */
HWTEST_F(NativeBufferTest, OHNativeBufferSetColorSpace002, Function | MediumTest | Level2)286 HWTEST_F(NativeBufferTest, OHNativeBufferSetColorSpace002, Function | MediumTest | Level2)
287 {
288     if (buffer == nullptr) {
289         buffer = OH_NativeBuffer_Alloc(&config);
290         ASSERT_NE(buffer, nullptr);
291     }
292 
293     int32_t ret = OH_NativeBuffer_SetColorSpace(buffer, OH_COLORSPACE_BT709_LIMIT);
294     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
295         ASSERT_EQ(ret, GSERROR_OK);
296     }
297 }
298 
299 /*
300 * Function: OH_NativeBuffer_GetColorSpace
301 * Type: Function
302 * Rank: Important(2)
303 * EnvConditions: N/A
304 * CaseDescription: 1. call OH_NativeBuffer_GetColorSpace by abnormal input
305 *                  2. check ret
306 */
HWTEST_F(NativeBufferTest, OHNativeBufferGetColorSpace001, Function | MediumTest | Level2)307 HWTEST_F(NativeBufferTest, OHNativeBufferGetColorSpace001, Function | MediumTest | Level2)
308 {
309     OH_NativeBuffer_ColorSpace *colorSpace = nullptr;
310     int32_t ret = OH_NativeBuffer_GetColorSpace(nullptr, colorSpace);
311     ASSERT_NE(ret, GSERROR_OK);
312 }
313 
314 /*
315 * Function: OH_NativeBuffer_GetColorSpace
316 * Type: Function
317 * Rank: Important(2)
318 * EnvConditions: N/A
319 * CaseDescription: 1. call OH_NativeBuffer_GetColorSpace
320 *                  2. check ret
321 */
HWTEST_F(NativeBufferTest, OHNativeBufferGetColorSpace002, Function | MediumTest | Level2)322 HWTEST_F(NativeBufferTest, OHNativeBufferGetColorSpace002, Function | MediumTest | Level2)
323 {
324     if (buffer == nullptr) {
325         buffer = OH_NativeBuffer_Alloc(&config);
326         ASSERT_NE(buffer, nullptr);
327     }
328     OH_NativeBuffer_ColorSpace colorSpace = OH_COLORSPACE_NONE;
329     int32_t ret = OH_NativeBuffer_SetColorSpace(buffer, OH_COLORSPACE_BT709_LIMIT);
330     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
331         ASSERT_EQ(ret, GSERROR_OK);
332     }
333     ret = OH_NativeBuffer_GetColorSpace(buffer, &colorSpace);
334     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
335         ASSERT_EQ(colorSpace, OH_COLORSPACE_BT709_LIMIT);
336         ASSERT_EQ(ret, GSERROR_OK);
337     }
338 }
339 
340 /*
341 * Function: OH_NativeBuffer_GetColorSpace
342 * Type: Function
343 * Rank: Important(2)
344 * EnvConditions: N/A
345 * CaseDescription: 1. call OH_NativeBuffer_GetColorSpace
346 *                  2. check ret
347 */
HWTEST_F(NativeBufferTest, OHNativeBufferGetColorSpace003, Function | MediumTest | Level2)348 HWTEST_F(NativeBufferTest, OHNativeBufferGetColorSpace003, Function | MediumTest | Level2)
349 {
350     if (buffer == nullptr) {
351         buffer = OH_NativeBuffer_Alloc(&config);
352         ASSERT_NE(buffer, nullptr);
353     }
354     OH_NativeBuffer_ColorSpace colorSpace = OH_COLORSPACE_NONE;
355     int32_t ret = OH_NativeBuffer_GetColorSpace(buffer, &colorSpace);
356     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
357         ASSERT_EQ(ret, GSERROR_OK);
358     }
359     ret = OH_NativeBuffer_SetColorSpace(buffer, OH_COLORSPACE_BT709_LIMIT);
360     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
361         ASSERT_EQ(colorSpace, OH_COLORSPACE_BT709_LIMIT);
362         ASSERT_EQ(ret, GSERROR_OK);
363     }
364 }
365 
366 /*
367 * Function: OH_NativeBuffer_SetMetadataValue
368 * Type: Function
369 * Rank: Important(2)
370 * EnvConditions: N/A
371 * CaseDescription: 1. call OH_NativeBuffer_SetMetadataValue by abnormal input
372 *                  2. check ret
373 */
HWTEST_F(NativeBufferTest, OH_NativeBuffer_SetMetadataValue001, Function | MediumTest | Level2)374 HWTEST_F(NativeBufferTest, OH_NativeBuffer_SetMetadataValue001, Function | MediumTest | Level2)
375 {
376     int32_t size = 1024;
377     uint8_t buff[size];
378     int32_t ret = OH_NativeBuffer_SetMetadataValue(nullptr, OH_HDR_STATIC_METADATA, size, buff);
379     ASSERT_NE(ret, GSERROR_OK);
380 }
381 
382 /*
383 * Function: OH_NativeBuffer_SetMetadataValue
384 * Type: Function
385 * Rank: Important(2)
386 * EnvConditions: N/A
387 * CaseDescription: 1. call OH_NativeBuffer_SetMetadataValue
388 *                  2. check ret
389 */
HWTEST_F(NativeBufferTest, OH_NativeBuffer_SetMetadataValue002, Function | MediumTest | Level2)390 HWTEST_F(NativeBufferTest, OH_NativeBuffer_SetMetadataValue002, Function | MediumTest | Level2)
391 {
392     if (buffer == nullptr) {
393         buffer = OH_NativeBuffer_Alloc(&config);
394         ASSERT_NE(buffer, nullptr);
395     }
396     int32_t len = 60;
397     uint8_t outbuff[len];
398     for (int i = 0; i < 60; ++i) {
399         outbuff[i] = static_cast<uint8_t>(i);
400     }
401     int32_t ret = OH_NativeBuffer_SetMetadataValue(buffer, OH_HDR_STATIC_METADATA, len, outbuff);
402     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
403         ASSERT_EQ(ret, GSERROR_OK);
404     }
405     ret = OH_NativeBuffer_SetMetadataValue(buffer, OH_HDR_DYNAMIC_METADATA, len, outbuff);
406     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
407         ASSERT_EQ(ret, GSERROR_OK);
408     }
409 }
410 
411 /*
412 * Function: OH_NativeBuffer_SetMetadataValue
413 * Type: Function
414 * Rank: Important(2)
415 * EnvConditions: N/A
416 * CaseDescription: 1. call OH_NativeBuffer_SetMetadataValue by abnormal input
417 *                  2. check ret
418 */
HWTEST_F(NativeBufferTest, OH_NativeBuffer_SetMetadataValue003, Function | MediumTest | Level2)419 HWTEST_F(NativeBufferTest, OH_NativeBuffer_SetMetadataValue003, Function | MediumTest | Level2)
420 {
421     int32_t max_size = -1;
422     int32_t size = 60;
423     uint8_t buff[size];
424     int32_t ret = OH_NativeBuffer_SetMetadataValue(nullptr, OH_HDR_STATIC_METADATA, max_size, buff);
425     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
426         ASSERT_NE(ret, GSERROR_OK);
427     }
428 }
429 
430 /*
431 * Function: OH_NativeBuffer_GetMetadataValue
432 * Type: Function
433 * Rank: Important(2)
434 * EnvConditions: N/A
435 * CaseDescription: 1. call OH_NativeBuffer_GetMetadataValue by abnormal input
436 *                  2. check ret
437 */
HWTEST_F(NativeBufferTest, OH_NativeBuffer_GetMetadataValue001, Function | MediumTest | Level2)438 HWTEST_F(NativeBufferTest, OH_NativeBuffer_GetMetadataValue001, Function | MediumTest | Level2)
439 {
440     int32_t size = 1024;
441     uint8_t *buff;
442     int32_t ret = OH_NativeBuffer_GetMetadataValue(nullptr, OH_HDR_STATIC_METADATA, &size, &buff);
443     if (buff != nullptr) {
444         delete[] buff;
445         buff = nullptr;
446     }
447     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
448         ASSERT_NE(ret, GSERROR_OK);
449     }
450 }
451 
452 /*
453 * Function: OH_NativeBuffer_GetMetadataValue
454 * Type: Function
455 * Rank: Important(2)
456 * EnvConditions: N/A
457 * CaseDescription: 1. call OH_NativeBuffer_GetMetadataValue
458 *                  2. check ret
459 */
HWTEST_F(NativeBufferTest, OH_NativeBuffer_GetMetadataValue002, Function | MediumTest | Level2)460 HWTEST_F(NativeBufferTest, OH_NativeBuffer_GetMetadataValue002, Function | MediumTest | Level2)
461 {
462     if (buffer == nullptr) {
463         buffer = OH_NativeBuffer_Alloc(&config);
464         ASSERT_NE(buffer, nullptr);
465     }
466     int32_t len = 60;
467     uint8_t outbuff[len];
468     for (int i = 0; i < 60; ++i) {
469         outbuff[i] = static_cast<uint8_t>(i);
470     }
471     int32_t ret = OH_NativeBuffer_SetMetadataValue(buffer, OH_HDR_STATIC_METADATA, len, outbuff);
472     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
473         ASSERT_EQ(ret, GSERROR_OK);
474     }
475     int32_t buffSize = 0;
476     uint8_t *buff;
477     ret = OH_NativeBuffer_GetMetadataValue(buffer, OH_HDR_STATIC_METADATA, &buffSize, &buff);
478     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
479         if (buff != nullptr) {
480             ASSERT_EQ(memcmp(outbuff, buff, 60), 0);
481             delete[] buff;
482             buff = nullptr;
483         }
484         ASSERT_EQ(ret, GSERROR_OK);
485     }
486     ret = OH_NativeBuffer_SetMetadataValue(buffer, OH_HDR_DYNAMIC_METADATA, len, outbuff);
487     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
488         ASSERT_EQ(ret, GSERROR_OK);
489     }
490     ret = OH_NativeBuffer_GetMetadataValue(buffer, OH_HDR_DYNAMIC_METADATA, &buffSize, &buff);
491     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
492         if (buff != nullptr) {
493             ASSERT_EQ(memcmp(outbuff, buff, 60), 0);
494             delete[] buff;
495             buff = nullptr;
496         }
497         ASSERT_EQ(ret, GSERROR_OK);
498     }
499 }
500 
501 /*
502 * Function: OH_NativeBuffer_GetMetadataValue
503 * Type: Function
504 * Rank: Important(2)
505 * EnvConditions: N/A
506 * CaseDescription: 1. call OH_NativeBuffer_GetMetadataValue
507 *                  2. check ret
508 */
HWTEST_F(NativeBufferTest, OH_NativeBuffer_GetMetadataValue003, Function | MediumTest | Level2)509 HWTEST_F(NativeBufferTest, OH_NativeBuffer_GetMetadataValue003, Function | MediumTest | Level2)
510 {
511     if (buffer == nullptr) {
512         buffer = OH_NativeBuffer_Alloc(&config);
513         ASSERT_NE(buffer, nullptr);
514     }
515     uint8_t *buff;
516     int32_t ret = OH_NativeBuffer_GetMetadataValue(buffer, OH_HDR_STATIC_METADATA, nullptr, &buff);
517     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
518         ASSERT_NE(ret, GSERROR_OK);
519     }
520 }
521 
522 /*
523 * Function: OH_NativeBuffer_Map
524 * Type: Function
525 * Rank: Important(2)
526 * EnvConditions: N/A
527 * CaseDescription: 1. call OH_NativeBuffer_Map by abnormal input
528 *                  2. check ret
529 */
HWTEST_F(NativeBufferTest, OHNativeBufferMap001, Function | MediumTest | Level2)530 HWTEST_F(NativeBufferTest, OHNativeBufferMap001, Function | MediumTest | Level2)
531 {
532     void *virAddr = nullptr;
533     int32_t ret = OH_NativeBuffer_Map(nullptr, &virAddr);
534     ASSERT_NE(ret, GSERROR_OK);
535 }
536 
537 /*
538 * Function: OH_NativeBuffer_Map
539 * Type: Function
540 * Rank: Important(2)
541 * EnvConditions: N/A
542 * CaseDescription: 1. call OH_NativeBuffer_Map
543 *                  2. check ret
544 */
HWTEST_F(NativeBufferTest, OHNativeBufferMap002, Function | MediumTest | Level2)545 HWTEST_F(NativeBufferTest, OHNativeBufferMap002, Function | MediumTest | Level2)
546 {
547     void *virAddr = nullptr;
548     int32_t ret = OH_NativeBuffer_Map(buffer, &virAddr);
549     ASSERT_EQ(ret, GSERROR_OK);
550     ASSERT_NE(virAddr, nullptr);
551 }
552 
553 /*
554 * Function: OH_NativeBuffer_Unmap
555 * Type: Function
556 * Rank: Important(2)
557 * EnvConditions: N/A
558 * CaseDescription: 1. call OH_NativeBuffer_Unmap by abnormal input
559 *                  2. check ret
560 */
HWTEST_F(NativeBufferTest, OHNativeBufferUnmap001, Function | MediumTest | Level2)561 HWTEST_F(NativeBufferTest, OHNativeBufferUnmap001, Function | MediumTest | Level2)
562 {
563     int32_t ret = OH_NativeBuffer_Unmap(nullptr);
564     ASSERT_NE(ret, GSERROR_OK);
565 }
566 
567 /*
568 * Function: OH_NativeBuffer_Unmap and OH_NativeBuffer_Unreference
569 * Type: Function
570 * Rank: Important(2)
571 * EnvConditions: N/A
572 * CaseDescription: 1. call OH_NativeBuffer_Unmap
573 *                  2. check ret
574 *                  3. call OH_NativeBuffer_Unreference
575 */
HWTEST_F(NativeBufferTest, OHNativeBufferUnmap002, Function | MediumTest | Level2)576 HWTEST_F(NativeBufferTest, OHNativeBufferUnmap002, Function | MediumTest | Level2)
577 {
578     int32_t ret = OH_NativeBuffer_Unmap(buffer);
579     ASSERT_EQ(ret, GSERROR_OK);
580     ret = OH_NativeBuffer_Unreference(buffer);
581     ASSERT_EQ(ret, GSERROR_OK);
582 }
583 
584 /*
585 * Function: OH_NativeBufferFromNativeWindowBuffer
586 * Type: Function
587 * Rank: Important(2)
588 * EnvConditions: N/A
589 * CaseDescription: 1. call OH_NativeBufferFromNativeWindowBuffer by abnormal input
590 *                  2. check ret
591 */
HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer001, Function | MediumTest | Level2)592 HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer001, Function | MediumTest | Level2)
593 {
594     OH_NativeBuffer* nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(nullptr);
595     ASSERT_EQ(nativeBuffer, nullptr);
596 }
597 
598 /*
599 * Function: OH_NativeBufferFromNativeWindowBuffer
600 * Type: Function
601 * Rank: Important(2)
602 * EnvConditions: N/A
603 * CaseDescription: 1. call OH_NativeBufferFromNativeWindowBuffer
604 *                  2. check ret
605 */
HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer002, Function | MediumTest | Level2)606 HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer002, Function | MediumTest | Level2)
607 {
608     sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create();
609     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
610     cSurface->RegisterConsumerListener(listener);
611     sptr<OHOS::IBufferProducer> producer = cSurface->GetProducer();
612     sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
613     int32_t fence;
614     sptr<OHOS::SurfaceBuffer> sBuffer = nullptr;
615     BufferRequestConfig requestConfig = {
616         .width = 0x100,  // small
617         .height = 0x100, // small
618         .strideAlignment = 0x8,
619         .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
620         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
621         .timeout = 0,
622     };
623     pSurface->RequestBuffer(sBuffer, fence, requestConfig);
624     NativeWindow* nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
625     ASSERT_NE(nativeWindow, nullptr);
626     NativeWindowBuffer* nativeWindowBuffer = OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(&sBuffer);
627     ASSERT_NE(nativeWindowBuffer, nullptr);
628     OH_NativeBuffer* nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(nativeWindowBuffer);
629     ASSERT_NE(nativeBuffer, nullptr);
630 
631     int32_t ret = OH_NativeBuffer_FromNativeWindowBuffer(nativeWindowBuffer, nullptr);
632     ASSERT_EQ(ret, OHOS::GSERROR_INVALID_ARGUMENTS);
633     OH_NativeBuffer* nativeBufferTmp = nullptr;
634     ret = OH_NativeBuffer_FromNativeWindowBuffer(nativeWindowBuffer, &nativeBufferTmp);
635     ASSERT_EQ(ret, OHOS::GSERROR_OK);
636     ASSERT_EQ(nativeBuffer, nativeBufferTmp);
637 
638     void *virAddr = nullptr;
639     OH_NativeBuffer_Planes outPlanes;
640     ret = OH_NativeBuffer_MapPlanes(nativeBuffer, &virAddr, &outPlanes);
641     if (ret != GSERROR_HDI_ERROR) {
642         ASSERT_EQ(ret, OHOS::GSERROR_OK);
643     }
644 
645     sBuffer = nullptr;
646     cSurface = nullptr;
647     producer = nullptr;
648     pSurface = nullptr;
649     nativeWindow = nullptr;
650     nativeWindowBuffer = nullptr;
651 }
652 
653 /*
654 * Function: OH_NativeBuffer_FromNativeWindowBuffer
655 * Type: Function
656 * Rank: Important(2)
657 * EnvConditions: N/A
658 * CaseDescription: 1. call OH_NativeBuffer_FromNativeWindowBuffer by abnormal input
659 *                  2. check ret
660 */
HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer003, Function | MediumTest | Level2)661 HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer003, Function | MediumTest | Level2)
662 {
663     int32_t ret = OH_NativeBuffer_FromNativeWindowBuffer(nullptr, nullptr);
664     ASSERT_EQ(ret, OHOS::GSERROR_INVALID_ARGUMENTS);
665 
666     NativeWindowBuffer nativeWindowBuffer;
667     ret = OH_NativeBuffer_FromNativeWindowBuffer(&nativeWindowBuffer, nullptr);
668     ASSERT_EQ(ret, OHOS::GSERROR_INVALID_ARGUMENTS);
669 
670     OH_NativeBuffer_GetNativeBufferConfig(nullptr, nullptr);
671     OH_NativeBuffer_GetNativeBufferConfig(buffer, nullptr);
672     ASSERT_EQ(OH_NativeBuffer_GetBufferHandle(nullptr), nullptr);
673 }
674 
675 /*
676 * Function: OH_NativeBuffer_MapPlanes
677 * Type: Function
678 * Rank: Important(2)
679 * EnvConditions: N/A
680 * CaseDescription: 1. call OH_NativeBuffer_MapPlanes by abnormal input
681 *                  2. check ret
682 */
HWTEST_F(NativeBufferTest, OHNativeBufferMapPlanes001, Function | MediumTest | Level2)683 HWTEST_F(NativeBufferTest, OHNativeBufferMapPlanes001, Function | MediumTest | Level2)
684 {
685     int32_t ret = OH_NativeBuffer_MapPlanes(nullptr, nullptr, nullptr);
686     ASSERT_EQ(ret, OHOS::GSERROR_INVALID_ARGUMENTS);
687 
688     OH_NativeBuffer *buffer = (OH_NativeBuffer *)0xFFFFFFFF;
689     ret = OH_NativeBuffer_MapPlanes(buffer, nullptr, nullptr);
690     ASSERT_EQ(ret, OHOS::GSERROR_INVALID_ARGUMENTS);
691 
692     void *virAddr = nullptr;
693     ret = OH_NativeBuffer_MapPlanes(buffer, &virAddr, nullptr);
694     ASSERT_EQ(ret, OHOS::GSERROR_INVALID_ARGUMENTS);
695 }
696 
697 /*
698 * Function: OH_NativeBuffer_MapPlanes
699 * Type: Function
700 * Rank: Important(2)
701 * EnvConditions: N/A
702 * CaseDescription: 1. call OH_NativeBuffer_MapPlanes
703 *                  2. check ret
704 */
HWTEST_F(NativeBufferTest, OHNativeBufferMapPlanes002, Function | MediumTest | Level2)705 HWTEST_F(NativeBufferTest, OHNativeBufferMapPlanes002, Function | MediumTest | Level2)
706 {
707     sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create();
708     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
709     cSurface->RegisterConsumerListener(listener);
710     sptr<OHOS::IBufferProducer> producer = cSurface->GetProducer();
711     sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
712     int32_t fence;
713     sptr<OHOS::SurfaceBuffer> sBuffer = nullptr;
714     BufferRequestConfig requestConfig = {.width = 0x100, .height = 0x100, .strideAlignment = 0x8,
715         .format = GRAPHIC_PIXEL_FMT_YCBCR_420_SP,
716         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA, .timeout = 0};
717     pSurface->SetQueueSize(4);
718     int32_t formatType[] = {GRAPHIC_PIXEL_FMT_YCBCR_420_SP, GRAPHIC_PIXEL_FMT_YCRCB_420_SP,
719         GRAPHIC_PIXEL_FMT_YCBCR_420_P, GRAPHIC_PIXEL_FMT_YCRCB_420_P};
720     NativeWindow* nativeWindow;
721     NativeWindowBuffer* nativeWindowBuffer;
722     for (int32_t i = 0; i < sizeof(formatType) / sizeof(int32_t); i++) {
723         requestConfig.format = formatType[i];
724         pSurface->RequestBuffer(sBuffer, fence, requestConfig);
725         nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
726         ASSERT_NE(nativeWindow, nullptr);
727         nativeWindowBuffer = OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(&sBuffer);
728         ASSERT_NE(nativeWindowBuffer, nullptr);
729         OH_NativeBuffer* nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(nativeWindowBuffer);
730         ASSERT_NE(nativeBuffer, nullptr);
731 
732         void *virAddr = nullptr;
733         OH_NativeBuffer_Planes outPlanes;
734         int32_t ret = OH_NativeBuffer_MapPlanes(nativeBuffer, &virAddr, &outPlanes);
735         if (ret != GSERROR_HDI_ERROR) {
736             ASSERT_EQ(ret, OHOS::GSERROR_OK);
737             ASSERT_NE(virAddr, nullptr);
738         }
739     }
740 
741     sBuffer = nullptr;
742     cSurface = nullptr;
743     producer = nullptr;
744     pSurface = nullptr;
745     nativeWindow = nullptr;
746     nativeWindowBuffer = nullptr;
747 }
748 
749 /*
750 * Function: OH_NativeBuffer_MapPlanes
751 * Type: Function
752 * Rank: Important(2)
753 * EnvConditions: N/A
754 * CaseDescription: 1. call OH_NativeBuffer_MapPlanes
755 *                  2. check ret
756 */
HWTEST_F(NativeBufferTest, OHNativeBufferMapPlanes003, Function | MediumTest | Level2)757 HWTEST_F(NativeBufferTest, OHNativeBufferMapPlanes003, Function | MediumTest | Level2)
758 {
759     sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create();
760     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
761     cSurface->RegisterConsumerListener(listener);
762     sptr<OHOS::IBufferProducer> producer = cSurface->GetProducer();
763     sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
764     int32_t fence;
765     sptr<OHOS::SurfaceBuffer> sBuffer = nullptr;
766     BufferRequestConfig requestConfig = {.width = 0x100, .height = 0x100, .strideAlignment = 0x8,
767         .format = GRAPHIC_PIXEL_FMT_YCBCR_420_SP,
768         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA, .timeout = 0};
769     pSurface->SetQueueSize(4);
770     int32_t formatType = GRAPHIC_PIXEL_FMT_YCBCR_420_SP;
771     NativeWindow* nativeWindow;
772     NativeWindowBuffer* nativeWindowBuffer;
773     requestConfig.format = formatType;
774     pSurface->RequestBuffer(sBuffer, fence, requestConfig);
775     nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
776     ASSERT_NE(nativeWindow, nullptr);
777     nativeWindowBuffer = OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(&sBuffer);
778     ASSERT_NE(nativeWindowBuffer, nullptr);
779     OH_NativeBuffer* nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(nativeWindowBuffer);
780     ASSERT_NE(nativeBuffer, nullptr);
781     OH_NativeBuffer* nativeBufferTmp = nullptr;
782     for (int32_t i = 0; i < 1000; i++) {
783         int32_t ret = OH_NativeBuffer_FromNativeWindowBuffer(nativeWindowBuffer, &nativeBufferTmp);
784         ASSERT_EQ(ret, OHOS::GSERROR_OK);
785         ASSERT_EQ(nativeBuffer, nativeBufferTmp);
786     }
787     void *virAddr = nullptr;
788     OH_NativeBuffer_Planes outPlanes;
789     clock_t startTime, endTime;
790     startTime = clock();
791     for (int32_t i = 0; i < 1000; i++) {
792         int32_t ret = OH_NativeBuffer_MapPlanes(nativeBuffer, &virAddr, &outPlanes);
793         if (ret != GSERROR_HDI_ERROR) {
794             ASSERT_EQ(ret, OHOS::GSERROR_OK);
795             ASSERT_NE(virAddr, nullptr);
796         }
797     }
798     endTime = clock();
799     cout << "OH_NativeBuffer_MapPlanes 1000 times cost time: " << (endTime - startTime) << "ms" << endl;
800     sBuffer = nullptr;
801     cSurface = nullptr;
802     producer = nullptr;
803     pSurface = nullptr;
804     nativeWindow = nullptr;
805     nativeWindowBuffer = nullptr;
806 }
807 }