• 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_SetColorSpace
268 * Type: Function
269 * Rank: Important(2)
270 * EnvConditions: N/A
271 * CaseDescription: 1. call OH_NativeBuffer_SetColorSpace by abnormal input
272 *                  2. check ret
273 */
274 HWTEST_F(NativeBufferTest, OHNativeBufferSetColorSpace001, Function | MediumTest | Level2)
275 {
276     int32_t ret = OH_NativeBuffer_SetColorSpace(nullptr, OH_COLORSPACE_DISPLAY_BT2020_PQ);
277     ASSERT_NE(ret, GSERROR_OK);
278 }
279 
280 /*
281 * Function: OH_NativeBuffer_SetColorSpace
282 * Type: Function
283 * Rank: Important(2)
284 * EnvConditions: N/A
285 * CaseDescription: 1. call OH_NativeBuffer_SetColorSpace
286 *                  2. check ret
287 */
288 HWTEST_F(NativeBufferTest, OHNativeBufferSetColorSpace002, Function | MediumTest | Level2)
289 {
290     if (buffer == nullptr) {
291         buffer = OH_NativeBuffer_Alloc(&config);
292         ASSERT_NE(buffer, nullptr);
293     }
294 
295     int32_t ret = OH_NativeBuffer_SetColorSpace(buffer, OH_COLORSPACE_BT709_LIMIT);
296     if (ret != GSERROR_NOT_SUPPORT) { // some device not support set colorspace
297         ASSERT_EQ(ret, GSERROR_OK);
298     }
299 }
300 
301 /*
302 * Function: OH_NativeBuffer_Map
303 * Type: Function
304 * Rank: Important(2)
305 * EnvConditions: N/A
306 * CaseDescription: 1. call OH_NativeBuffer_Map by abnormal input
307 *                  2. check ret
308 */
309 HWTEST_F(NativeBufferTest, OHNativeBufferMap001, Function | MediumTest | Level2)
310 {
311     void *virAddr = nullptr;
312     int32_t ret = OH_NativeBuffer_Map(nullptr, &virAddr);
313     ASSERT_NE(ret, GSERROR_OK);
314 }
315 
316 /*
317 * Function: OH_NativeBuffer_Map
318 * Type: Function
319 * Rank: Important(2)
320 * EnvConditions: N/A
321 * CaseDescription: 1. call OH_NativeBuffer_Map
322 *                  2. check ret
323 */
324 HWTEST_F(NativeBufferTest, OHNativeBufferMap002, Function | MediumTest | Level2)
325 {
326     void *virAddr = nullptr;
327     int32_t ret = OH_NativeBuffer_Map(buffer, &virAddr);
328     ASSERT_EQ(ret, GSERROR_OK);
329     ASSERT_NE(virAddr, nullptr);
330 }
331 
332 /*
333 * Function: OH_NativeBuffer_Unmap
334 * Type: Function
335 * Rank: Important(2)
336 * EnvConditions: N/A
337 * CaseDescription: 1. call OH_NativeBuffer_Unmap by abnormal input
338 *                  2. check ret
339 */
340 HWTEST_F(NativeBufferTest, OHNativeBufferUnmap001, Function | MediumTest | Level2)
341 {
342     int32_t ret = OH_NativeBuffer_Unmap(nullptr);
343     ASSERT_NE(ret, GSERROR_OK);
344 }
345 
346 /*
347 * Function: OH_NativeBuffer_Unmap and OH_NativeBuffer_Unreference
348 * Type: Function
349 * Rank: Important(2)
350 * EnvConditions: N/A
351 * CaseDescription: 1. call OH_NativeBuffer_Unmap
352 *                  2. check ret
353 *                  3. call OH_NativeBuffer_Unreference
354 */
355 HWTEST_F(NativeBufferTest, OHNativeBufferUnmap002, Function | MediumTest | Level2)
356 {
357     int32_t ret = OH_NativeBuffer_Unmap(buffer);
358     ASSERT_EQ(ret, GSERROR_OK);
359     ret = OH_NativeBuffer_Unreference(buffer);
360     ASSERT_EQ(ret, GSERROR_OK);
361 }
362 
363 /*
364 * Function: OH_NativeBufferFromNativeWindowBuffer
365 * Type: Function
366 * Rank: Important(2)
367 * EnvConditions: N/A
368 * CaseDescription: 1. call OH_NativeBufferFromNativeWindowBuffer by abnormal input
369 *                  2. check ret
370 */
371 HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer001, Function | MediumTest | Level2)
372 {
373     OH_NativeBuffer* nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(nullptr);
374     ASSERT_EQ(nativeBuffer, nullptr);
375 }
376 
377 /*
378 * Function: OH_NativeBufferFromNativeWindowBuffer
379 * Type: Function
380 * Rank: Important(2)
381 * EnvConditions: N/A
382 * CaseDescription: 1. call OH_NativeBufferFromNativeWindowBuffer
383 *                  2. check ret
384 */
385 HWTEST_F(NativeBufferTest, NativeBufferFromNativeWindowBuffer002, Function | MediumTest | Level2)
386 {
387     sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create();
388     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
389     cSurface->RegisterConsumerListener(listener);
390     sptr<OHOS::IBufferProducer> producer = cSurface->GetProducer();
391     sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
392     int32_t fence;
393     sptr<OHOS::SurfaceBuffer> sBuffer = nullptr;
394     BufferRequestConfig requestConfig = {
395         .width = 0x100,  // small
396         .height = 0x100, // small
397         .strideAlignment = 0x8,
398         .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
399         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
400         .timeout = 0,
401     };
402     pSurface->RequestBuffer(sBuffer, fence, requestConfig);
403     NativeWindow* nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
404     ASSERT_NE(nativeWindow, nullptr);
405     NativeWindowBuffer* nativeWindowBuffer = OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(&sBuffer);
406     ASSERT_NE(nativeWindowBuffer, nullptr);
407     OH_NativeBuffer* nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(nativeWindowBuffer);
408     ASSERT_NE(nativeBuffer, nullptr);
409 
410     sBuffer = nullptr;
411     cSurface = nullptr;
412     producer = nullptr;
413     pSurface = nullptr;
414     nativeWindow = nullptr;
415     nativeWindowBuffer = nullptr;
416 }
417 }