• 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 <surface.h>
17 #include <buffer_queue_consumer.h>
18 #include "buffer_consumer_listener.h"
19 #include "buffer_extra_data_impl.h"
20 #include "sync_fence.h"
21 #include "consumer_surface.h"
22 #include "producer_surface_delegator.h"
23 #include "buffer_client_producer.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS::Rosen {
29 class BufferQueueConsumerTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33 
34     static inline BufferRequestConfig requestConfig = {
35         .width = 0x100,
36         .height = 0x100,
37         .strideAlignment = 0x8,
38         .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
39         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
40         .timeout = 0,
41     };
42 
43     static inline BufferFlushConfigWithDamages flushConfig = {
44         .damages = {
45             {
46                 .w = 0x100,
47                 .h = 0x100,
48             }
49         },
50     };
51     static inline int64_t timestamp = 0;
52     static inline std::vector<Rect> damages = {};
53     static inline sptr<BufferQueue> bq = nullptr;
54     static inline sptr<BufferQueueConsumer> bqc = nullptr;
55     static inline sptr<BufferQueueConsumer> byt = nullptr;
56     static inline sptr<BufferExtraData> bedata = nullptr;
57     static inline sptr<ProducerSurfaceDelegator> surfaceDelegator = nullptr;
58     static inline sptr<IConsumerSurface> csurface = nullptr;
59     static inline sptr<IBufferProducer> bufferProducer = nullptr;
60     static inline sptr<Surface> surface = nullptr;
61 };
62 
SetUpTestCase()63 void BufferQueueConsumerTest::SetUpTestCase()
64 {
65     bq = new BufferQueue("test");
66     bqc = new BufferQueueConsumer(bq);
67     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
68     bqc->RegisterConsumerListener(listener);
69     bedata = new BufferExtraDataImpl;
70     csurface = IConsumerSurface::Create();
71     surfaceDelegator = ProducerSurfaceDelegator::Create();
72 
73     bufferProducer = csurface->GetProducer();
74     surface = Surface::CreateSurfaceAsProducer(bufferProducer);
75 }
76 
TearDownTestCase()77 void BufferQueueConsumerTest::TearDownTestCase()
78 {
79     bq = nullptr;
80     bqc = nullptr;
81 }
82 
83 /*
84  * Function: AcquireBuffer and ReleaseBuffer
85  * Type: Function
86  * Rank: Important(2)
87  * EnvConditions: N/A
88  * CaseDescription: 1. call RequestBuffer and FlushBuffer
89  *                  2. call AcquireBuffer and ReleaseBuffer
90  *                  3. check ret
91  */
92 HWTEST_F(BufferQueueConsumerTest, AcqRel001, TestSize.Level0)
93 {
94     IBufferProducer::RequestBufferReturnValue retval;
95     GSError ret = bq->RequestBuffer(requestConfig, bedata, retval);
96     ASSERT_EQ(ret, OHOS::GSERROR_OK);
97     ASSERT_GE(retval.sequence, 0);
98     ASSERT_NE(retval.buffer, nullptr);
99 
100     uint8_t *addr1 = reinterpret_cast<uint8_t*>(retval.buffer->GetVirAddr());
101     ASSERT_NE(addr1, nullptr);
102 
103     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
104     ret = bq->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
105     ASSERT_EQ(ret, OHOS::GSERROR_OK);
106 
107     ret = bqc->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
108     ASSERT_EQ(ret, OHOS::GSERROR_OK);
109 
110     sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
111     ret = bqc->ReleaseBuffer(retval.buffer, releaseFence);
112     ASSERT_EQ(ret, OHOS::GSERROR_OK);
113 }
114 
115 /*
116  * Function: AcquireBuffer and ReleaseBuffer
117  * Type: Function
118  * Rank: Important(2)
119  * EnvConditions: N/A
120  * CaseDescription: 1. call RequestBuffer and FlushBuffer
121  * 2. call AcquireBuffer and ReleaseBuffer
122  * 3. call ReleaseBuffer again
123  * 4. check ret
124  */
125 HWTEST_F(BufferQueueConsumerTest, AcqRel002, TestSize.Level0)
126 {
127     IBufferProducer::RequestBufferReturnValue retval;
128     GSError ret = bq->RequestBuffer(requestConfig, bedata, retval);
129     ASSERT_EQ(ret, OHOS::GSERROR_OK);
130     ASSERT_GE(retval.sequence, 0);
131     ASSERT_EQ(retval.buffer, nullptr);
132 
133     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
134     ret = bq->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
135     ASSERT_EQ(ret, OHOS::GSERROR_OK);
136 
137     sptr<SurfaceBuffer>& buffer = retval.buffer;
138     ret = bqc->AcquireBuffer(buffer, retval.fence, timestamp, damages);
139     ASSERT_EQ(ret, OHOS::GSERROR_OK);
140 
141     sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
142     ret = bqc->ReleaseBuffer(buffer, releaseFence);
143     ASSERT_EQ(ret, OHOS::GSERROR_OK);
144 
145     ret = bqc->ReleaseBuffer(buffer, releaseFence);
146     ASSERT_NE(ret, OHOS::GSERROR_OK);
147 
148     int32_t timeOut = 1;
149     ret = bqc->AttachBuffer(buffer, timeOut);
150     ASSERT_EQ(ret, OHOS::GSERROR_OK);
151 }
152 
153 /*
154  * Function: GetLastConsumeTime
155  * Type: Function
156  * Rank: Important(2)
157  * EnvConditions: N/A
158  * CaseDescription: 1. call GetLastConsumeTime
159  *                  2. check lastConsumeTime
160  */
161 HWTEST_F(BufferQueueConsumerTest, GetLastConsumeTimeTest, TestSize.Level0)
162 {
163     int64_t lastConsumeTime = 0;
164     bqc->GetLastConsumeTime(lastConsumeTime);
165     std::cout << "lastConsumeTime = " << lastConsumeTime << std::endl;
166     ASSERT_NE(lastConsumeTime, 0);
167 }
168 
169 /*
170  * Function: AttachBuffer001
171  * Type: Function
172  * Rank: Important(2)
173  * EnvConditions: N/A
174  * CaseDescription: 1. check bufferQueue_
175  *    2. call AttachBuffer
176  */
177 HWTEST_F(BufferQueueConsumerTest, AttachBuffer001, TestSize.Level0)
178 {
179     int32_t timeOut = 0;
180     IBufferProducer::RequestBufferReturnValue retval;
181     sptr<SurfaceBuffer> &buffer = retval.buffer;
182 
183     sptr<BufferQueue> bufferqueue = nullptr;
184     auto bqcTmp = new BufferQueueConsumer(bufferqueue);
185 
186     GSError ret = bqcTmp->AttachBuffer(buffer, timeOut);
187     ASSERT_EQ(ret, GSERROR_INVALID_ARGUMENTS);
188 }
189 /* Function: RegisterSurfaceDelegator
190  * Type: Function
191  * Rank: Important(2)
192  * EnvConditions: N/A
193  * CaseDescription: 1. check ret
194  *    2. call RegisterSurfaceDelegator
195  */
196 HWTEST_F(BufferQueueConsumerTest, RegisterSurfaceDelegator001, TestSize.Level0)
197 {
198     sptr<BufferQueue> bufferqueue = nullptr;
199     auto bqcTmp = new BufferQueueConsumer(bufferqueue);
200 
201     sptr<ProducerSurfaceDelegator> surfaceDelegator = ProducerSurfaceDelegator::Create();
202     GSError ret = bqcTmp->RegisterSurfaceDelegator(surfaceDelegator->AsObject(), surface);
203     ASSERT_NE(ret, OHOS::GSERROR_OK);
204 }
205 
206 HWTEST_F(BufferQueueConsumerTest, RegisterSurfaceDelegator002, TestSize.Level0)
207 {
208     sptr<ProducerSurfaceDelegator> surfaceDelegator = ProducerSurfaceDelegator::Create();
209     GSError ret = bqc->RegisterSurfaceDelegator(surfaceDelegator->AsObject(), surface);
210     ASSERT_EQ(ret, OHOS::GSERROR_OK);
211 }
212 
213 HWTEST_F(BufferQueueConsumerTest, RegisterSurfaceDelegator003, TestSize.Level0)
214 {
215     sptr<ProducerSurfaceDelegator> surfaceDelegator = ProducerSurfaceDelegator::Create();
216     GSError ret = bqc->RegisterSurfaceDelegator(surfaceDelegator->AsObject(), nullptr);
217     ASSERT_EQ(ret, OHOS::GSERROR_OK);
218 }
219 
220 HWTEST_F(BufferQueueConsumerTest, AddBranchCoverage001, TestSize.Level0)
221 {
222     sptr<BufferQueue> bufferQueue = nullptr;
223     sptr<BufferQueueConsumer> consumer = new BufferQueueConsumer(bufferQueue);
224 
225     sptr<SurfaceBuffer> buffer = nullptr;
226     sptr<SyncFence> fence = nullptr;
227     int64_t timestamp = 0;
228     std::vector<Rect> damages;
229     sptr<IRemoteObject> client = nullptr;
230     sptr<Surface> cSurface = nullptr;
231     sptr<IBufferConsumerListener> listener = nullptr;
232     IBufferConsumerListenerClazz *listenerClass = nullptr;
233     OnReleaseFunc func;
234     OnDeleteBufferFunc deleteFunc;
235     bool isForUniRedraw = false;
236     std::string result;
237     ScalingMode scalingMode;
238     HDRMetaDataType type;
239     std::vector<GraphicHDRMetaData> metaData;
240     GraphicHDRMetadataKey key;
241     std::vector<uint8_t> metaData1;
242     GraphicPresentTimestamp timestamp1;
243     bool isInCache = false;
244     int32_t alpha = -1;
245     int32_t frameGravity = -1;
246     int32_t fixedRotation = -1;
247     int64_t lastFlushedDesiredPresentTimeStamp = -1;
248     bool bufferSupportFastCompose = false;
249     int64_t frontDesiredPresentTimeStamp = -1;
250     bool isAutoTimeStamp = false;
251     GraphicAlphaType alphaType = GraphicAlphaType::GRAPHIC_ALPHATYPE_UNKNOWN;
252     ASSERT_EQ(consumer->AcquireBuffer(buffer, fence, timestamp, damages), OHOS::SURFACE_ERROR_UNKOWN);
253     ASSERT_EQ(consumer->ReleaseBuffer(buffer, fence), OHOS::SURFACE_ERROR_UNKOWN);
254     ASSERT_EQ(consumer->AttachBufferToQueue(buffer), OHOS::SURFACE_ERROR_UNKOWN);
255     ASSERT_EQ(consumer->DetachBufferFromQueue(buffer, false), OHOS::SURFACE_ERROR_UNKOWN);
256     ASSERT_EQ(consumer->AttachBuffer(buffer), OHOS::GSERROR_INVALID_ARGUMENTS);
257     ASSERT_EQ(consumer->DetachBuffer(buffer), OHOS::GSERROR_INVALID_ARGUMENTS);
258     ASSERT_EQ(consumer->RegisterSurfaceDelegator(client, cSurface), OHOS::GSERROR_INVALID_ARGUMENTS);
259     ASSERT_EQ(consumer->QueryIfBufferAvailable(), false);
260     ASSERT_EQ(consumer->RegisterConsumerListener(listener), OHOS::GSERROR_INVALID_ARGUMENTS);
261     ASSERT_EQ(consumer->RegisterConsumerListener(listenerClass), OHOS::GSERROR_INVALID_ARGUMENTS);
262     ASSERT_EQ(consumer->RegisterReleaseListener(func), OHOS::GSERROR_INVALID_ARGUMENTS);
263     ASSERT_EQ(consumer->RegisterDeleteBufferListener(deleteFunc, isForUniRedraw), OHOS::GSERROR_INVALID_ARGUMENTS);
264     ASSERT_EQ(consumer->UnregisterConsumerListener(), OHOS::GSERROR_INVALID_ARGUMENTS);
265     ASSERT_EQ(consumer->SetDefaultWidthAndHeight(0, 0), OHOS::GSERROR_INVALID_ARGUMENTS);
266     ASSERT_EQ(consumer->SetDefaultUsage(0), OHOS::GSERROR_INVALID_ARGUMENTS);
267     consumer->Dump(result);
268     consumer->DumpCurrentFrameLayer();
269     ASSERT_EQ(consumer->GetTransform(), GraphicTransformType::GRAPHIC_ROTATE_BUTT);
270     ASSERT_EQ(consumer->GetScalingMode(0, scalingMode), OHOS::GSERROR_INVALID_ARGUMENTS);
271     ASSERT_EQ(consumer->QueryMetaDataType(0, type), OHOS::GSERROR_INVALID_ARGUMENTS);
272     ASSERT_EQ(consumer->GetMetaData(0, metaData), OHOS::GSERROR_INVALID_ARGUMENTS);
273     ASSERT_EQ(consumer->GetMetaDataSet(0, key, metaData1), OHOS::GSERROR_INVALID_ARGUMENTS);
274     ASSERT_EQ(consumer->GetTunnelHandle(), nullptr);
275     ASSERT_EQ(consumer->SetPresentTimestamp(0, timestamp1), OHOS::GSERROR_INVALID_ARGUMENTS);
276     consumer->SetBufferHold(0);
277     ASSERT_EQ(consumer->OnConsumerDied(), OHOS::GSERROR_INVALID_ARGUMENTS);
278     ASSERT_EQ(consumer->GoBackground(), OHOS::GSERROR_INVALID_ARGUMENTS);
279     ASSERT_EQ(consumer->GetHdrWhitePointBrightness(), OHOS::SURFACE_ERROR_UNKOWN);
280     ASSERT_EQ(consumer->GetSdrWhitePointBrightness(), OHOS::SURFACE_ERROR_UNKOWN);
281     ASSERT_EQ(consumer->IsSurfaceBufferInCache(0, isInCache), OHOS::SURFACE_ERROR_UNKOWN);
282     ASSERT_EQ(consumer->GetGlobalAlpha(alpha), OHOS::SURFACE_ERROR_UNKOWN);
283     ASSERT_EQ(consumer->GetFrameGravity(frameGravity), OHOS::SURFACE_ERROR_UNKOWN);
284     ASSERT_EQ(consumer->GetFixedRotation(fixedRotation), OHOS::SURFACE_ERROR_UNKOWN);
285     ASSERT_EQ(consumer->GetLastFlushedDesiredPresentTimeStamp(
286         lastFlushedDesiredPresentTimeStamp), OHOS::SURFACE_ERROR_UNKOWN);
287     ASSERT_EQ(consumer->GetBufferSupportFastCompose(bufferSupportFastCompose), OHOS::SURFACE_ERROR_UNKOWN);
288     ASSERT_EQ(consumer->SetMaxQueueSize(1), OHOS::SURFACE_ERROR_UNKOWN);
289     uint32_t maxQueueSize;
290     ASSERT_EQ(consumer->GetMaxQueueSize(maxQueueSize), OHOS::SURFACE_ERROR_UNKOWN);
291     ASSERT_EQ(consumer->GetFrontDesiredPresentTimeStamp(
292         frontDesiredPresentTimeStamp, isAutoTimeStamp), OHOS::SURFACE_ERROR_UNKOWN);
293     ASSERT_EQ(consumer->GetAlphaType(alphaType), OHOS::SURFACE_ERROR_UNKOWN);
294 }
295 
296 /*
297  * Function: AcquireBuffer
298  * Type: Function
299  * Rank: Important(2)
300  * EnvConditions: N/A
301  * CaseDescription: 1. call AcquireBuffer and check ret
302  */
303 HWTEST_F(BufferQueueConsumerTest, AcquireBufferByReturnValue, TestSize.Level0)
304 {
305     sptr<ProducerSurfaceDelegator> surfaceDelegator = ProducerSurfaceDelegator::Create();
306     GSError ret = bqc->RegisterSurfaceDelegator(surfaceDelegator->AsObject(), nullptr);
307     ASSERT_EQ(ret, OHOS::GSERROR_OK);
308     bqc->bufferQueue_ = nullptr;
309     IConsumerSurface::AcquireBufferReturnValue returnValue;
310     ret = bqc->AcquireBuffer(returnValue);
311     ASSERT_EQ(ret, OHOS::SURFACE_ERROR_UNKOWN);
312 }
313 
314 /*
315  * Function: ReleaseBuffer
316  * Type: Function
317  * Rank: Important(2)
318  * EnvConditions: N/A
319  * CaseDescription: 1. call ReleaseBuffer and check ret
320  */
321 HWTEST_F(BufferQueueConsumerTest, ReleaseBufferBySeq, TestSize.Level0)
322 {
323     bqc->bufferQueue_ = nullptr;
324     uint32_t sequence = 0;
325     sptr<SyncFence> fence = nullptr;
326     GSError ret = bqc->ReleaseBuffer(sequence, fence);
327     ASSERT_EQ(ret, OHOS::SURFACE_ERROR_UNKOWN);
328 }
329 
330 /*
331  * Function: SetIsActiveGame
332  * Type: Function
333  * Rank: Important(2)
334  * EnvConditions: N/A
335  * CaseDescription: 1. call SetIsActiveGame and check ret
336  */
337 HWTEST_F(BufferQueueConsumerTest, SetIsActiveGame001, TestSize.Level0)
338 {
339     bqc->bufferQueue_ = nullptr;
340     bqc->SetIsActiveGame(false);
341     // bqc->bufferQueue_ is not nullptr
342     bqc->bufferQueue_ = new BufferQueue("test");
343     bqc->SetIsActiveGame(false);
344     bqc->bufferQueue_ = nullptr;
345 }
346 
347 /*
348  * Function: SetLppDrawSource
349  * Type: Function
350  * Rank: Important(2)
351  * EnvConditions: N/A
352  * CaseDescription: set lpp draw source
353  */
354 HWTEST_F(BufferQueueConsumerTest, SetLppDrawSource001, TestSize.Level0)
355 {
356     bool isShbDrawLpp = false;
357     bool isRsDrawLpp = false;
358     sptr<BufferQueue> bufferQueue = nullptr;
359     sptr<BufferQueueConsumer> consumer = new BufferQueueConsumer(bufferQueue);
360     ASSERT_EQ(consumer->SetLppDrawSource(isShbDrawLpp, isRsDrawLpp), OHOS::SURFACE_ERROR_UNKOWN);
361 
362     consumer->bufferQueue_ = new BufferQueue("test");
363     ASSERT_EQ(consumer->SetLppDrawSource(isShbDrawLpp, isRsDrawLpp), OHOS::GSERROR_TYPE_ERROR);
364 }
365 
366 /*
367  * Function: GetAlphaType
368  * Type: Function
369  * Rank: Important(2)
370  * EnvConditions: N/A
371  * CaseDescription: get alpha type
372  */
373 HWTEST_F(BufferQueueConsumerTest, GetAlphaTypeTest, TestSize.Level0)
374 {
375     sptr<BufferQueue> bufferQueue = new BufferQueue("test");
376     sptr<BufferQueueConsumer> consumer = new BufferQueueConsumer(bufferQueue);
377     GraphicAlphaType alphaType;
378     ASSERT_EQ(consumer->GetAlphaType(alphaType), OHOS::GSERROR_OK);
379 }
380 
381 /*
382  * Function: AcquireBuffer
383  * Type: Function
384  * Rank: Important(2)
385  * EnvConditions: N/A
386  * CaseDescription: acquire lpp buffer
387  */
388 HWTEST_F(BufferQueueConsumerTest, AcqRel003, TestSize.Level0)
389 {
390     sptr<BufferQueue> bufferQueue = new BufferQueue("test");
391     sptr<BufferQueueConsumer> consumer = new BufferQueueConsumer(bufferQueue);
392     sptr<SurfaceBuffer> buffer = nullptr;
393     sptr<SyncFence> acquireFence = SyncFence::InvalidFence();
394     int64_t timestamp = 0;
395     std::vector<Rect> damages;
396     ASSERT_EQ(consumer->AcquireBuffer(buffer, acquireFence, timestamp, damages, true), OHOS::GSERROR_TYPE_ERROR);
397 }
398 
399 /*
400  * Function: SetIsPriorityAlloc
401  * Type: Function
402  * Rank: Important(2)
403  * EnvConditions: N/A
404  * CaseDescription: 1. call SetIsPriorityAlloc and check ret
405  */
406 HWTEST_F(BufferQueueConsumerTest, SetIsPriorityAlloc001, TestSize.Level0)
407 {
408     bqc->bufferQueue_ = nullptr;
409     // bqc->bufferQueue_ is not nullptr
410     bqc->bufferQueue_ = new BufferQueue("test");
411     bqc->SetIsPriorityAlloc(true);
412     ASSERT_EQ(bqc->bufferQueue_->isPriorityAlloc_, true);
413     bqc->SetIsPriorityAlloc(false);
414     ASSERT_EQ(bqc->bufferQueue_->isPriorityAlloc_, false);
415     bqc->bufferQueue_ = nullptr;
416 }
417 }
418