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 <securec.h>
19 #include "surface_type.h"
20 #include "buffer_log.h"
21 #include "external_window.h"
22 #include "iconsumer_surface.h"
23 #include <native_buffer.h>
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS::Rosen {
29 class NativeWindowTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33
34 static inline OHNativeWindow* nativeWindow = nullptr;
35 static inline OHNativeWindowBuffer* nativeWindowBuffer = nullptr;
36 static inline uint32_t sequence = 0;
37 static inline BufferRequestConfig requestConfig = {};
38 static inline BufferFlushConfig flushConfig = {};
39 static inline sptr<OHOS::IConsumerSurface> 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 };
44 class BufferConsumerListener : public IBufferConsumerListener {
45 public:
OnBufferAvailable()46 void OnBufferAvailable() override
47 {
48 }
49 };
50
SetUpTestCase()51 void NativeWindowTest::SetUpTestCase()
52 {
53 }
54
TearDownTestCase()55 void NativeWindowTest::TearDownTestCase()
56 {
57 }
58
59 /*
60 * @tc.name CreateNativeWindow001
61 * @tc.desc test for call OH_NativeWindow_CreateNativeWindow by abnormal input and check ret
62 * @tc.type FUNC
63 */
64 HWTEST_F(NativeWindowTest, CreateNativeWindow001, Function | MediumTest | Level2)
65 {
66 ASSERT_EQ(OH_NativeWindow_CreateNativeWindow(nullptr), nullptr);
67 }
68 /*
69 * @tc.name CreateNativeWindow002
70 * @tc.desc test for call OH_NativeWindow_CreateNativeWindow
71 */
72 HWTEST_F(NativeWindowTest, CreateNativeWindow002, Function | MediumTest | Level2)
73 {
74 requestConfig = {
75 .width = 0x100, // small
76 .height = 0x100, // small
77 .strideAlignment = 0x8,
78 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
79 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
80 .timeout = 0,
81 };
82
83 cSurface = IConsumerSurface::Create();
84 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
85 cSurface->RegisterConsumerListener(listener);
86 producer = cSurface->GetProducer();
87 pSurface = Surface::CreateSurfaceAsProducer(producer);
88 int32_t fence;
89 pSurface->RequestBuffer(sBuffer, fence, requestConfig);
90
91 nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
92 ASSERT_NE(nativeWindow, nullptr);
93 }
94 /*
95 * @tc.name OH_NativeWindow_GetNativeObjectMagic
96 * @tc.desc test for call OH_NativeWindow_GetNativeObjectMagic and check ret
97 * @tc.type FUNC
98 */
99 HWTEST_F(NativeWindowTest, GetNativeObjectMagic001, Function | MediumTest | Level2)
100 {
101 int32_t ret = OH_NativeWindow_GetNativeObjectMagic(nativeWindow);
102 ASSERT_NE(ret, 0);
103 }
104
105 /*
106 * @tc.name HandleOpt001
107 * @tc.desc test for call OH_NativeWindow_NativeWindowHandleOpt by different param and check ret
108 * @tc.type FUNC
109 */
110 HWTEST_F(NativeWindowTest, HandleOpt001, Function | MediumTest | Level2)
111 {
112 int code = SET_BUFFER_GEOMETRY;
113 int32_t heightSet = 0x100;
114 int32_t widthSet = 0x100;
115 ASSERT_EQ(OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, heightSet, widthSet), 0);
116
117 code = GET_BUFFER_GEOMETRY;
118 int32_t heightGet = 0;
119 int32_t widthGet = 0;
120 ASSERT_EQ(OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, &heightGet, &widthGet), 0);
121 ASSERT_EQ(heightSet, heightGet);
122 ASSERT_EQ(widthSet, widthGet);
123 }
124
125
126 /*
127 * @tc.name HandleOpt005
128 * @tc.desc test for call OH_NativeWindow_NativeWindowHandleOpt by different param and check ret
129 * @tc.type FUNC
130 */
131 HWTEST_F(NativeWindowTest, HandleOpt005, Function | MediumTest | Level2)
132 {
133 int code = SET_STRIDE;
134 int32_t strideSet = 0x8;
135 ASSERT_EQ(OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, strideSet), 0);
136
137 code = GET_STRIDE;
138 int32_t strideGet = 0;
139 ASSERT_EQ(OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, &strideGet), 0);
140 ASSERT_EQ(strideSet, strideGet);
141 }
142
143 /*
144 * @tc.name HandleOpt007
145 * @tc.desc test for call OH_NativeWindow_NativeWindowHandleOpt by different param and check ret
146 * @tc.type FUNC
147 */
148 HWTEST_F(NativeWindowTest, HandleOpt007, Function | MediumTest | Level2)
149 {
150 int code = SET_TIMEOUT;
151 int32_t timeoutSet = 10; // 10: for test
152 ASSERT_EQ(OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, timeoutSet), 0);
153
154 code = GET_TIMEOUT;
155 int32_t timeoutGet = 0;
156 ASSERT_EQ(OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, &timeoutGet), 0);
157 ASSERT_EQ(timeoutSet, timeoutGet);
158 }
159
160 /*
161 * @tc.name CreateNativeWindowBuffer001
162 * @tc.desc test for call OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer by abnormal input and check ret
163 * @tc.type FUNC
164 */
165 HWTEST_F(NativeWindowTest, CreateNativeWindowBuffer001, Function | MediumTest | Level2)
166 {
167 ASSERT_EQ(OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer(nullptr), nullptr);
168 }
169 /*
170 * @tc.name CreateNativeWindowBufferFromNativeBuffer001
171 * @tc.desc test for call OH_NativeWindow_CreateNativeWindowBufferFromNativeBuffer
172 * @tc.type FUNC
173 */
174 HWTEST_F(NativeWindowTest, CreateNativeWindowBufferFromNativeBuffer001, Function | MediumTest | Level2)
175 {
176 ASSERT_EQ(OH_NativeWindow_CreateNativeWindowBufferFromNativeBuffer(nullptr), nullptr);
177 }
178
179 /*
180 * @tc.name CreateNativeWindowBufferFromNativeBuffer002
181 * @tc.desc test for call OH_NativeWindow_CreateNativeWindowBufferFromNativeBuffer
182 * @tc.type FUNC
183 */
184 HWTEST_F(NativeWindowTest, CreateNativeWindowBufferFromNativeBuffer002, Function | MediumTest | Level2)
185 {
186 OH_NativeBuffer_Config config = {
187 .width = 0x100,
188 .height = 0x100,
189 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
190 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
191 };
192 OH_NativeBuffer* nativeBuffer = OH_NativeBuffer_Alloc(&config);
193 ASSERT_NE(nativeBuffer, nullptr);
194 NativeWindowBuffer* nwBuffer = OH_NativeWindow_CreateNativeWindowBufferFromNativeBuffer(nativeBuffer);
195 ASSERT_NE(nwBuffer, nullptr);
196 OH_NativeWindow_DestroyNativeWindowBuffer(nwBuffer);
197 }
198
199 /*
200 * @tc.name RequestBuffer001
201 * @tc.desc test for call OH_NativeWindow_NativeWindowRequestBuffer by abnormal input and check ret
202 * @tc.type FUNC
203 */
204 HWTEST_F(NativeWindowTest, RequestBuffer001, Function | MediumTest | Level2)
205 {
206 ASSERT_NE(OH_NativeWindow_NativeWindowRequestBuffer(nullptr, &nativeWindowBuffer, nullptr), 0);
207 }
208
209 /*
210 * @tc.name RequestBuffer002
211 * @tc.desc test for call OH_NativeWindow_NativeWindowRequestBuffer by abnormal input and check ret
212 * @tc.type FUNC
213 */
214 HWTEST_F(NativeWindowTest, RequestBuffer002, Function | MediumTest | Level2)
215 {
216 ASSERT_NE(OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, nullptr, nullptr), 0);
217 }
218
219 /*
220 * @tc.name GetBufferHandle001
221 * @tc.desc test for call OH_NativeWindow_GetBufferHandleFromNative by abnormal input and check ret
222 * @tc.type FUNC
223 */
224 HWTEST_F(NativeWindowTest, GetBufferHandle001, Function | MediumTest | Level2)
225 {
226 ASSERT_EQ(OH_NativeWindow_GetBufferHandleFromNative(nullptr), nullptr);
227 }
228
229 /*
230 * @tc.name FlushBuffer001
231 * @tc.desc test for call OH_NativeWindow_NativeWindowFlushBuffer by abnormal input and check ret
232 * @tc.type FUNC
233 */
234 HWTEST_F(NativeWindowTest, FlushBuffer001, Function | MediumTest | Level2)
235 {
236 int fenceFd = -1;
237 struct Region *region = new Region();
238 struct Region::Rect * rect = new Region::Rect();
239 rect->x = 0x100;
240 rect->y = 0x100;
241 rect->w = 0x100;
242 rect->h = 0x100;
243 region->rects = rect;
244
245 ASSERT_NE(OH_NativeWindow_NativeWindowFlushBuffer(nullptr, nullptr, fenceFd, *region), 0);
246 delete region;
247 }
248
249 /*
250 * @tc.name FlushBuffer002
251 * @tc.desc test for call OH_NativeWindow_NativeWindowFlushBuffer by abnormal input and check ret
252 * @tc.type FUNC
253 */
254 HWTEST_F(NativeWindowTest, FlushBuffer002, Function | MediumTest | Level2)
255 {
256 int fenceFd = -1;
257 struct Region *region = new Region();
258 struct Region::Rect * rect = new Region::Rect();
259 rect->x = 0x100;
260 rect->y = 0x100;
261 rect->w = 0x100;
262 rect->h = 0x100;
263 region->rects = rect;
264
265 ASSERT_NE(OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nullptr, fenceFd, *region), 0);
266 delete region;
267 }
268 /*
269 * @tc.name GetLastFlushedBuffer001
270 * @tc.desc test for call OH_NativeWindow_GetLastFlushedBuffer
271 * @tc.type FUNC
272 */
273 HWTEST_F(NativeWindowTest, GetLastFlushedBuffer001, Function | MediumTest | Level2)
274 {
275 NativeWindowBuffer *nativeWindowBuffer = nullptr;
276 int fenceFd = -1;
277 ASSERT_NE(nativeWindow, nullptr);
278 int32_t ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
279 ASSERT_EQ(ret, GSERROR_OK);
280
281 struct Region *region = new Region();
282 struct Region::Rect *rect = new Region::Rect();
283 rect->x = 0x100;
284 rect->y = 0x100;
285 rect->w = 0x100;
286 rect->h = 0x100;
287 region->rects = rect;
288 BufferHandle *bufferHanlde = OH_NativeWindow_GetBufferHandleFromNative(nativeWindowBuffer);
289 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region);
290 ASSERT_EQ(ret, GSERROR_OK);
291 NativeWindowBuffer *lastFlushedBuffer;
292 int lastFlushedFenceFd;
293 float matrix[16];
294 ASSERT_EQ(OH_NativeWindow_GetLastFlushedBuffer(nativeWindow, &lastFlushedBuffer, &lastFlushedFenceFd, matrix),
295 OHOS::GSERROR_OK);
296 BufferHandle *lastFlushedHanlde = OH_NativeWindow_GetBufferHandleFromNative(lastFlushedBuffer);
297 ASSERT_EQ(bufferHanlde->virAddr, lastFlushedHanlde->virAddr);
298 }
299
300 /*
301 * @tc.name GetLastFlushedBuffer002
302 * @tc.desc test for call OH_NativeWindow_GetLastFlushedBuffer
303 * @tc.type FUNC
304 */
305 HWTEST_F(NativeWindowTest, GetLastFlushedBuffer002, Function | MediumTest | Level2)
306 {
307 NativeWindowBuffer *lastFlushedBuffer;
308 int lastFlushedFenceFd;
309 float matrix[16];
310 ASSERT_EQ(OH_NativeWindow_GetLastFlushedBuffer(nullptr, &lastFlushedBuffer, &lastFlushedFenceFd, matrix),
311 OHOS::GSERROR_INVALID_ARGUMENTS);
312 }
313
314 /*
315 * @tc.name GetLastFlushedBuffer003
316 * @tc.desc test for call OH_NativeWindow_GetLastFlushedBuffer
317 * @tc.type FUNC
318 */
319 HWTEST_F(NativeWindowTest, GetLastFlushedBuffer003, Function | MediumTest | Level2)
320 {
321 int code = SET_USAGE;
322 uint64_t usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_PROTECTED;
323 ASSERT_EQ(NativeWindowHandleOpt(nativeWindow, code, usage), OHOS::GSERROR_OK);
324
325 NativeWindowBuffer* nativeWindowBuffer = nullptr;
326 int fenceFd = -1;
327 int32_t ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
328 ASSERT_EQ(ret, GSERROR_OK);
329
330 struct Region *region = new Region();
331 struct Region::Rect *rect = new Region::Rect();
332 rect->x = 0x100;
333 rect->y = 0x100;
334 rect->w = 0x100;
335 rect->h = 0x100;
336 region->rects = rect;
337 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region);
338 ASSERT_EQ(ret, GSERROR_OK);
339 NativeWindowBuffer* lastFlushedBuffer;
340 int lastFlushedFenceFd;
341 float matrix[16];
342 ASSERT_EQ(OH_NativeWindow_GetLastFlushedBuffer(nativeWindow, &lastFlushedBuffer, &lastFlushedFenceFd, matrix),
343 OHOS::GSERROR_NO_PERMISSION);
344 }
345 /*
346 * @tc.name CancelBuffer001
347 * @tc.desc test for call OH_NativeWindow_NativeWindowAbortBuffer by abnormal input and check ret
348 * @tc.type FUNC
349 */
350 HWTEST_F(NativeWindowTest, CancelBuffer001, Function | MediumTest | Level2)
351 {
352 ASSERT_NE(OH_NativeWindow_NativeWindowAbortBuffer(nullptr, nullptr), 0);
353 }
354
355 /*
356 * @tc.name Reference001
357 * @tc.desc test for call OH_NativeWindow_NativeObjectReference and check ret
358 * @tc.type FUNC
359 */
360 HWTEST_F(NativeWindowTest, Reference001, Function | MediumTest | Level2)
361 {
362 ASSERT_NE(OH_NativeWindow_NativeObjectReference(nullptr), 0);
363 }
364
365 /*
366 * @tc.name Unreference001
367 * @tc.desc test for call OH_NativeWindow_NativeObjectUnreference and check ret
368 * @tc.type FUNC
369 */
370 HWTEST_F(NativeWindowTest, Unreference001, Function | MediumTest | Level2)
371 {
372 ASSERT_NE(OH_NativeWindow_NativeObjectUnreference(nullptr), 0);
373 }
374
375 /*
376 * @tc.name DestroyNativeWindow001
377 * @tc.desc test for call OH_NativeWindow_DestroyNativeWindow by abnormal input and check ret
378 * @tc.type FUNC
379 */
380 HWTEST_F(NativeWindowTest, DestroyNativeWindow001, Function | MediumTest | Level2)
381 {
382 OH_NativeWindow_DestroyNativeWindow(nullptr);
383 }
384
385 /*
386 * @tc.name OH_NativeWindow_DestroyNativeWindowBuffer001
387 * @tc.desc test for call OH_NativeWindow_DestroyNativeWindowBuffer by abnormal input and check ret
388 * @tc.type FUNC
389 */
390 HWTEST_F(NativeWindowTest, OH_NativeWindow_DestroyNativeWindowBuffer001, Function | MediumTest | Level2)
391 {
392 OH_NativeWindow_DestroyNativeWindowBuffer(nullptr);
393 }
394
395 /*
396 * @tc.name SetScalingMode001
397 * @tc.desc test for call OH_NativeWindow_NativeWindowSetScalingMode with abnormal parameters and check ret
398 * @tc.type FUNC
399 */
400 HWTEST_F(NativeWindowTest, SetScalingMode001, Function | MediumTest | Level2)
401 {
402 OHScalingMode scalingMode = OHScalingMode::OH_SCALING_MODE_SCALE_TO_WINDOW;
403 ASSERT_NE(OH_NativeWindow_NativeWindowSetScalingMode(nullptr, -1, scalingMode), 0);
404 }
405
406 /*
407 * @tc.name SetScalingMode002
408 * @tc.desc test for call OH_NativeWindow_NativeWindowSetScalingMode with abnormal parameters and check ret
409 * @tc.type FUNC
410 */
411 HWTEST_F(NativeWindowTest, SetScalingMode002, Function | MediumTest | Level2)
412 {
413 OHScalingMode scalingMode = OHScalingMode::OH_SCALING_MODE_SCALE_TO_WINDOW;
414 ASSERT_NE(OH_NativeWindow_NativeWindowSetScalingMode(nativeWindow, -1, scalingMode), 0);
415 }
416
417
418 /*
419 * @tc.name SetMetaData001
420 * @tc.desc test for call OH_NativeWindow_NativeWindowSetMetaData with abnormal parameters and check ret
421 * @tc.type FUNC
422 */
423 HWTEST_F(NativeWindowTest, SetMetaData001, Function | MediumTest | Level2)
424 {
425 ASSERT_NE(OH_NativeWindow_NativeWindowSetMetaData(nullptr, -1, 0, nullptr), 0);
426 }
427
428 /*
429 * @tc.name SetMetaData002
430 * @tc.desc test for call OH_NativeWindow_NativeWindowSetMetaData with abnormal parameters and check ret
431 * @tc.type FUNC
432 */
433 HWTEST_F(NativeWindowTest, SetMetaData002, Function | MediumTest | Level2)
434 {
435 ASSERT_NE(OH_NativeWindow_NativeWindowSetMetaData(nativeWindow, -1, 0, nullptr), 0);
436 }
437
438 /*
439 * @tc.name SetMetaDataSet001
440 * @tc.desc test for call OH_NativeWindow_NativeWindowSetMetaDataSet with abnormal parameters and check ret
441 * @tc.type FUNC
442 */
443 HWTEST_F(NativeWindowTest, SetMetaDataSet001, Function | MediumTest | Level2)
444 {
445 OHHDRMetadataKey key = OHHDRMetadataKey::OH_METAKEY_HDR10_PLUS;
446 ASSERT_NE(OH_NativeWindow_NativeWindowSetMetaDataSet(nullptr, -1, key, 0, nullptr), 0);
447 }
448
449 /*
450 * @tc.name SetMetaDataSet002
451 * @tc.desc test for call OH_NativeWindow_NativeWindowSetMetaDataSet with abnormal parameters and check ret
452 * @tc.type FUNC
453 */
454 HWTEST_F(NativeWindowTest, SetMetaDataSet002, Function | MediumTest | Level2)
455 {
456 OHHDRMetadataKey key = OHHDRMetadataKey::OH_METAKEY_HDR10_PLUS;
457 ASSERT_NE(OH_NativeWindow_NativeWindowSetMetaDataSet(nativeWindow, -1, key, 0, nullptr), 0);
458 }
459
460 /*
461 * @tc.name SetTunnelHandle001
462 * @tc.desc test for call OH_NativeWindow_NativeWindowSetTunnelHandle with abnormal parameters and check ret
463 * @tc.type FUNC
464 */
465 HWTEST_F(NativeWindowTest, SetTunnelHandle001, Function | MediumTest | Level2)
466 {
467 ASSERT_NE(OH_NativeWindow_NativeWindowSetTunnelHandle(nullptr, nullptr), 0);
468 }
469
470 /*
471 * @tc.name SetTunnelHandle002
472 * @tc.desc test for call OH_NativeWindow_NativeWindowSetTunnelHandle with abnormal parameters and check ret
473 * @tc.type FUNC
474 */
475 HWTEST_F(NativeWindowTest, SetTunnelHandle002, Function | MediumTest | Level2)
476 {
477 ASSERT_NE(OH_NativeWindow_NativeWindowSetTunnelHandle(nativeWindow, nullptr), 0);
478 }
479 /*
480 * @tc.name DestroyNativeWindow002
481 * @tc.desc test for call OH_NativeWindow_DestroyNativeWindow
482 * @tc.type FUNC
483 */
484 HWTEST_F(NativeWindowTest, DestroyNativeWindow002, Function | MediumTest | Level2)
485 {
486 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
487 }
488 }