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