• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <iservice_registry.h>
17 #include <native_window.h>
18 #include <display_type.h>
19 #include <surface_type.h>
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS::Rosen {
25 class BufferConsumerListener : public IBufferConsumerListener {
26 public:
OnBufferAvailable()27     void OnBufferAvailable() override
28     {
29     }
30 };
31 
32 class NativeWindowTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36 
37     static inline BufferRequestConfig requestConfig = {};
38     static inline BufferFlushConfig flushConfig = {};
39     static inline sptr<OHOS::Surface> cSurface = nullptr;
40     static inline sptr<OHOS::IBufferProducer> producer = nullptr;
41     static inline sptr<OHOS::Surface> pSurface = nullptr;
42     static inline sptr<OHOS::SurfaceBuffer> sBuffer = nullptr;
43     static inline NativeWindow* nativeWindow = nullptr;
44     static inline NativeWindowBuffer* nativeWindowBuffer = nullptr;
45 };
46 
SetUpTestCase()47 void NativeWindowTest::SetUpTestCase()
48 {
49     requestConfig = {
50         .width = 0x100,  // small
51         .height = 0x100, // small
52         .strideAlignment = 0x8,
53         .format = PIXEL_FMT_RGBA_8888,
54         .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
55         .timeout = 0,
56     };
57 
58     cSurface = Surface::CreateSurfaceAsConsumer();
59     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
60     cSurface->RegisterConsumerListener(listener);
61     producer = cSurface->GetProducer();
62     pSurface = Surface::CreateSurfaceAsProducer(producer);
63     int32_t fence;
64     pSurface->RequestBuffer(sBuffer, fence, requestConfig);
65 }
66 
TearDownTestCase()67 void NativeWindowTest::TearDownTestCase()
68 {
69     flushConfig = { .damage = {
70         .w = 0x100,
71         .h = 0x100,
72     } };
73     pSurface->FlushBuffer(sBuffer, -1, flushConfig);
74     sBuffer = nullptr;
75     cSurface = nullptr;
76     producer = nullptr;
77     pSurface = nullptr;
78     nativeWindow = nullptr;
79     nativeWindowBuffer = nullptr;
80 }
81 
82 /*
83 * Function: CreateNativeWindowFromSurface
84 * Type: Function
85 * Rank: Important(2)
86 * EnvConditions: N/A
87 * CaseDescription: 1. call CreateNativeWindowFromSurface by abnormal input
88 *                  2. check ret
89  */
90 HWTEST_F(NativeWindowTest, CreateNativeWindow001, Function | MediumTest | Level2)
91 {
92     ASSERT_EQ(CreateNativeWindowFromSurface(nullptr), nullptr);
93 }
94 
95 /*
96 * Function: CreateNativeWindowFromSurface
97 * Type: Function
98 * Rank: Important(2)
99 * EnvConditions: N/A
100 * CaseDescription: 1. call CreateNativeWindowFromSurface
101 *                  2. check ret
102  */
103 HWTEST_F(NativeWindowTest, CreateNativeWindow002, Function | MediumTest | Level2)
104 {
105     nativeWindow = CreateNativeWindowFromSurface(&pSurface);
106     ASSERT_NE(nativeWindow, nullptr);
107 }
108 
109 /*
110 * Function: NativeWindowHandleOpt
111 * Type: Function
112 * Rank: Important(2)
113 * EnvConditions: N/A
114 * CaseDescription: 1. call NativeWindowHandleOpt by abnormal input
115 *                  2. check ret
116  */
117 HWTEST_F(NativeWindowTest, HandleOpt001, Function | MediumTest | Level2)
118 {
119     int code = SET_USAGE;
120     int32_t usage = HBM_USE_CPU_READ;
121     ASSERT_EQ(NativeWindowHandleOpt(nullptr, code, usage), OHOS::GSERROR_INVALID_ARGUMENTS);
122 }
123 
124 /*
125 * Function: NativeWindowHandleOpt
126 * Type: Function
127 * Rank: Important(2)
128 * EnvConditions: N/A
129 * CaseDescription: 1. call NativeWindowHandleOpt by different param
130 *                  2. check ret
131  */
132 HWTEST_F(NativeWindowTest, HandleOpt002, Function | MediumTest | Level2)
133 {
134     int code = SET_USAGE;
135     int32_t usage = HBM_USE_CPU_READ;
136     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, usage), OHOS::GSERROR_OK);
137 
138     code = GET_USAGE;
139     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, &usage), OHOS::GSERROR_OK);
140 }
141 
142 /*
143 * Function: NativeWindowHandleOpt
144 * Type: Function
145 * Rank: Important(2)
146 * EnvConditions: N/A
147 * CaseDescription: 1. call NativeWindowHandleOpt by different param
148 *                  2. check ret
149  */
150 HWTEST_F(NativeWindowTest, HandleOpt003, Function | MediumTest | Level2)
151 {
152     int code = SET_BUFFER_GEOMETRY;
153     int32_t height = 0x100;
154     int32_t width = 0x100;
155     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, height, width), OHOS::GSERROR_OK);
156 
157     code = GET_BUFFER_GEOMETRY;
158     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, &height, &width), OHOS::GSERROR_OK);
159 }
160 
161 /*
162 * Function: NativeWindowHandleOpt
163 * Type: Function
164 * Rank: Important(2)
165 * EnvConditions: N/A
166 * CaseDescription: 1. call NativeWindowHandleOpt by different param
167 *                  2. check ret
168  */
169 HWTEST_F(NativeWindowTest, HandleOpt004, Function | MediumTest | Level2)
170 {
171     int code = SET_FORMAT;
172     int32_t format = PIXEL_FMT_RGBA_8888;
173     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, format), OHOS::GSERROR_OK);
174 
175     code = GET_FORMAT;
176     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, &format), OHOS::GSERROR_OK);
177 }
178 
179 /*
180 * Function: NativeWindowHandleOpt
181 * Type: Function
182 * Rank: Important(2)
183 * EnvConditions: N/A
184 * CaseDescription: 1. call NativeWindowHandleOpt by different param
185 *                  2. check ret
186  */
187 HWTEST_F(NativeWindowTest, HandleOpt005, Function | MediumTest | Level2)
188 {
189     int code = SET_STRIDE;
190     int32_t stride = 0x8;
191     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, stride), OHOS::GSERROR_OK);
192 
193     code = GET_STRIDE;
194     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, &stride), OHOS::GSERROR_OK);
195 }
196 
197 /*
198 * Function: NativeWindowHandleOpt
199 * Type: Function
200 * Rank: Important(2)
201 * EnvConditions: N/A
202 * CaseDescription: 1. call NativeWindowHandleOpt by different param
203 *                  2. check ret
204  */
205 HWTEST_F(NativeWindowTest, HandleOpt006, Function | MediumTest | Level2)
206 {
207     int code = SET_COLOR_GAMUT;
208     int32_t colorGamut = static_cast<int32_t>(SurfaceColorGamut::COLOR_GAMUT_DCI_P3);
209     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, colorGamut), OHOS::GSERROR_OK);
210 
211     code = GET_COLOR_GAMUT;
212     ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, &colorGamut), OHOS::GSERROR_OK);
213 }
214 
215 /*
216 * Function: CreateNativeWindowBufferFromSurfaceBuffer
217 * Type: Function
218 * Rank: Important(2)
219 * EnvConditions: N/A
220 * CaseDescription: 1. call CreateNativeWindowBufferFromSurfaceBuffer by abnormal input
221 *                  2. check ret
222  */
223 HWTEST_F(NativeWindowTest, CreateNativeWindowBuffer001, Function | MediumTest | Level2)
224 {
225     ASSERT_EQ(CreateNativeWindowBufferFromSurfaceBuffer(nullptr), nullptr);
226 }
227 
228 /*
229 * Function: CreateNativeWindowBufferFromSurfaceBuffer
230 * Type: Function
231 * Rank: Important(2)
232 * EnvConditions: N/A
233 * CaseDescription: 1. call CreateNativeWindowBufferFromSurfaceBuffer
234 *                  2. check ret
235  */
236 HWTEST_F(NativeWindowTest, CreateNativeWindowBuffer002, Function | MediumTest | Level2)
237 {
238     nativeWindowBuffer = CreateNativeWindowBufferFromSurfaceBuffer(&sBuffer);
239     ASSERT_NE(nativeWindowBuffer, nullptr);
240 }
241 
242 /*
243 * Function: NativeWindowRequestBuffer
244 * Type: Function
245 * Rank: Important(2)
246 * EnvConditions: N/A
247 * CaseDescription: 1. call NativeWindowRequestBuffer by abnormal input
248 *                  2. check ret
249  */
250 HWTEST_F(NativeWindowTest, RequestBuffer001, Function | MediumTest | Level2)
251 {
252     ASSERT_EQ(NativeWindowRequestBuffer(nullptr, &nativeWindowBuffer, nullptr), OHOS::GSERROR_INVALID_ARGUMENTS);
253 }
254 
255 /*
256 * Function: NativeWindowRequestBuffer
257 * Type: Function
258 * Rank: Important(2)
259 * EnvConditions: N/A
260 * CaseDescription: 1. call NativeWindowRequestBuffer by abnormal input
261 *                  2. check ret
262  */
263 HWTEST_F(NativeWindowTest, RequestBuffer002, Function | MediumTest | Level2)
264 {
265     ASSERT_EQ(NativeWindowRequestBuffer(nativeWindow, nullptr, nullptr), OHOS::GSERROR_INVALID_ARGUMENTS);
266 }
267 
268 /*
269 * Function: GetBufferHandleFromNative
270 * Type: Function
271 * Rank: Important(2)
272 * EnvConditions: N/A
273 * CaseDescription: 1. call GetBufferHandleFromNative by abnormal input
274 *                  2. check ret
275  */
276 HWTEST_F(NativeWindowTest, GetBufferHandle001, Function | MediumTest | Level2)
277 {
278     ASSERT_EQ(GetBufferHandleFromNative(nullptr), nullptr);
279 }
280 
281 /*
282 * Function: GetBufferHandleFromNative
283 * Type: Function
284 * Rank: Important(2)
285 * EnvConditions: N/A
286 * CaseDescription: 1. call GetBufferHandleFromNative
287 *                  2. check ret
288  */
289 HWTEST_F(NativeWindowTest, GetBufferHandle002, Function | MediumTest | Level2)
290 {
291     struct NativeWindowBuffer *buffer = new NativeWindowBuffer();
292     buffer->sfbuffer = sBuffer;
293     ASSERT_NE(GetBufferHandleFromNative(nativeWindowBuffer), nullptr);
294     delete buffer;
295 }
296 
297 /*
298 * Function: NativeWindowFlushBuffer
299 * Type: Function
300 * Rank: Important(2)
301 * EnvConditions: N/A
302 * CaseDescription: 1. call NativeWindowFlushBuffer by abnormal input
303 *                  2. check ret
304  */
305 HWTEST_F(NativeWindowTest, FlushBuffer001, Function | MediumTest | Level2)
306 {
307     int fenceFd = -1;
308     struct Region *region = new Region();
309     struct Region::Rect * rect = new Region::Rect();
310     rect->x = 0x100;
311     rect->y = 0x100;
312     rect->w = 0x100;
313     rect->h = 0x100;
314     region->rects = rect;
315 
316     ASSERT_EQ(NativeWindowFlushBuffer(nullptr, nullptr, fenceFd, *region), OHOS::GSERROR_INVALID_ARGUMENTS);
317     delete region;
318 }
319 
320 /*
321 * Function: NativeWindowFlushBuffer
322 * Type: Function
323 * Rank: Important(2)
324 * EnvConditions: N/A
325 * CaseDescription: 1. call NativeWindowFlushBuffer by abnormal input
326 *                  2. check ret
327  */
328 HWTEST_F(NativeWindowTest, FlushBuffer002, Function | MediumTest | Level2)
329 {
330     int fenceFd = -1;
331     struct Region *region = new Region();
332     struct Region::Rect * rect = new Region::Rect();
333     rect->x = 0x100;
334     rect->y = 0x100;
335     rect->w = 0x100;
336     rect->h = 0x100;
337     region->rects = rect;
338 
339     ASSERT_EQ(NativeWindowFlushBuffer(nativeWindow, nullptr, fenceFd, *region), OHOS::GSERROR_INVALID_ARGUMENTS);
340     delete region;
341 }
342 
343 /*
344 * Function: NativeWindowFlushBuffer
345 * Type: Function
346 * Rank: Important(2)
347 * EnvConditions: N/A
348 * CaseDescription: 1. call NativeWindowFlushBuffer
349 *                  2. check ret
350  */
351 HWTEST_F(NativeWindowTest, FlushBuffer003, Function | MediumTest | Level2)
352 {
353     int fenceFd = -1;
354     struct Region *region = new Region();
355     struct Region::Rect * rect = new Region::Rect();
356     rect->x = 0x100;
357     rect->y = 0x100;
358     rect->w = 0x100;
359     rect->h = 0x100;
360     region->rects = rect;
361 
362     ASSERT_EQ(NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region), OHOS::GSERROR_OK);
363     delete region;
364 }
365 
366 /*
367 * Function: NativeWindowCancelBuffer
368 * Type: Function
369 * Rank: Important(2)
370 * EnvConditions: N/A
371 * CaseDescription: 1. call NativeWindowCancelBuffer by abnormal input
372 *                  2. check ret
373  */
374 HWTEST_F(NativeWindowTest, CancelBuffer001, Function | MediumTest | Level2)
375 {
376     ASSERT_EQ(NativeWindowCancelBuffer(nullptr, nullptr), OHOS::GSERROR_INVALID_ARGUMENTS);
377 }
378 
379 /*
380 * Function: NativeWindowCancelBuffer
381 * Type: Function
382 * Rank: Important(2)
383 * EnvConditions: N/A
384 * CaseDescription: 1. call NativeWindowCancelBuffer by abnormal input
385 *                  2. check ret
386  */
387 HWTEST_F(NativeWindowTest, CancelBuffer002, Function | MediumTest | Level2)
388 {
389     ASSERT_EQ(NativeWindowCancelBuffer(nativeWindow, nullptr), OHOS::GSERROR_INVALID_ARGUMENTS);
390 }
391 
392 /*
393 * Function: NativeWindowCancelBuffer
394 * Type: Function
395 * Rank: Important(2)
396 * EnvConditions: N/A
397 * CaseDescription: 1. call NativeWindowCancelBuffer
398 *                  2. check ret
399  */
400 HWTEST_F(NativeWindowTest, CancelBuffer003, Function | MediumTest | Level2)
401 {
402     ASSERT_EQ(NativeWindowCancelBuffer(nativeWindow, nativeWindowBuffer), OHOS::GSERROR_OK);
403 }
404 
405 /*
406 * Function: NativeObjectReference
407 * Type: Function
408 * Rank: Important(2)
409 * EnvConditions: N/A
410 * CaseDescription: 1. call NativeObjectReference
411 *                  2. check ret
412  */
413 HWTEST_F(NativeWindowTest, Reference001, Function | MediumTest | Level2)
414 {
415     struct NativeWindowBuffer *buffer = new NativeWindowBuffer();
416     buffer->sfbuffer = sBuffer;
417     ASSERT_EQ(NativeObjectReference(reinterpret_cast<void *>(buffer)), OHOS::GSERROR_OK);
418     delete buffer;
419 }
420 
421 /*
422 * Function: NativeObjectUnreference
423 * Type: Function
424 * Rank: Important(2)
425 * EnvConditions: N/A
426 * CaseDescription: 1. call NativeObjectUnreference
427 *                  2. check ret
428  */
429 HWTEST_F(NativeWindowTest, Unreference001, Function | MediumTest | Level2)
430 {
431     struct NativeWindowBuffer *buffer = new NativeWindowBuffer();
432     buffer->sfbuffer = sBuffer;
433     ASSERT_EQ(NativeObjectUnreference(reinterpret_cast<void *>(buffer)), OHOS::GSERROR_OK);
434     delete buffer;
435 }
436 
437 /*
438 * Function: DestoryNativeWindow
439 * Type: Function
440 * Rank: Important(2)
441 * EnvConditions: N/A
442 * CaseDescription: 1. call DestoryNativeWindow by abnormal input
443 *                  2. check ret
444  */
445 HWTEST_F(NativeWindowTest, DestoryNativeWindow001, Function | MediumTest | Level2)
446 {
447     DestoryNativeWindow(nullptr);
448 }
449 
450 /*
451 * Function: DestoryNativeWindow
452 * Type: Function
453 * Rank: Important(2)
454 * EnvConditions: N/A
455 * CaseDescription: 1. call DestoryNativeWindow
456 *                  2. check ret
457  */
458 HWTEST_F(NativeWindowTest, DestoryNativeWindow002, Function | MediumTest | Level2)
459 {
460     DestoryNativeWindow(nativeWindow);
461 }
462 
463 /*
464 * Function: DestoryNativeWindowBuffer
465 * Type: Function
466 * Rank: Important(2)
467 * EnvConditions: N/A
468 * CaseDescription: 1. call DestoryNativeWindowBuffer by abnormal and normal input
469 *                  2. check ret
470  */
471 HWTEST_F(NativeWindowTest, DestoryNativeWindowBuffer001, Function | MediumTest | Level2)
472 {
473     DestoryNativeWindowBuffer(nullptr);
474     DestoryNativeWindowBuffer(nativeWindowBuffer);
475 }
476 
477 /*
478 * Function: DestoryNativeWindowBuffer
479 * Type: Function
480 * Rank: Important(2)
481 * EnvConditions: N/A
482 * CaseDescription: 1. call DestoryNativeWindowBuffer again
483 *                  2. check ret
484  */
485 HWTEST_F(NativeWindowTest, DestoryNativeWindowBuffer002, Function | MediumTest | Level2)
486 {
487     DestoryNativeWindowBuffer(nativeWindowBuffer);
488 }
489 }
490