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_extra_data_impl.h>
18 #include <buffer_queue_producer.h>
19 #include "buffer_consumer_listener.h"
20 #include "sync_fence.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS::Rosen {
26 class BufferQueueProducerTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30
31 static inline BufferRequestConfig requestConfig = {
32 .width = 0x100,
33 .height = 0x100,
34 .strideAlignment = 0x8,
35 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
36 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
37 .timeout = 0,
38 };
39 static inline BufferFlushConfig flushConfig = {
40 .damage = {
41 .w = 0x100,
42 .h = 0x100,
43 },
44 };
45 static inline std::vector<int32_t> deletingBuffers;
46 static inline int64_t timestamp = 0;
47 static inline Rect damage = {};
48 static inline sptr<BufferQueue> bq = nullptr;
49 static inline sptr<BufferQueueProducer> bqp = nullptr;
50 static inline sptr<BufferExtraData> bedata = nullptr;
51 };
52
SetUpTestCase()53 void BufferQueueProducerTest::SetUpTestCase()
54 {
55 bq = new BufferQueue("test");
56 bq->Init();
57 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
58 bq->RegisterConsumerListener(listener);
59 bqp = new BufferQueueProducer(bq);
60 bedata = new OHOS::BufferExtraDataImpl;
61 }
62
TearDownTestCase()63 void BufferQueueProducerTest::TearDownTestCase()
64 {
65 bq = nullptr;
66 bqp = nullptr;
67 }
68
69 /*
70 * Function: SetQueueSize and GetQueueSize
71 * Type: Function
72 * Rank: Important(2)
73 * EnvConditions: N/A
74 * CaseDescription: 1. call GetQueueSize and get default value
75 * 2. call SetQueueSize
76 * 3. call SetQueueSize again with abnormal value
77 * 4. call GetQueueSize for BufferQueueProducer and BufferQueue
78 * 5. check ret
79 */
80 HWTEST_F(BufferQueueProducerTest, QueueSize001, Function | MediumTest | Level2)
81 {
82 ASSERT_EQ(bqp->GetQueueSize(), (uint32_t)SURFACE_DEFAULT_QUEUE_SIZE);
83
84 GSError ret = bqp->SetQueueSize(2);
85 ASSERT_EQ(ret, OHOS::GSERROR_OK);
86
87 ret = bqp->SetQueueSize(SURFACE_MAX_QUEUE_SIZE + 1);
88 ASSERT_NE(ret, OHOS::GSERROR_OK);
89
90 ASSERT_EQ(bqp->GetQueueSize(), 2u);
91 ASSERT_EQ(bq->GetQueueSize(), 2u);
92 }
93
94 /*
95 * Function: RequestBuffer and CancelBuffer
96 * Type: Function
97 * Rank: Important(2)
98 * EnvConditions: N/A
99 * CaseDescription: 1. call RequestBuffer
100 * 2. call CancelBuffer
101 * 3. check ret
102 */
103 HWTEST_F(BufferQueueProducerTest, ReqCan001, Function | MediumTest | Level2)
104 {
105 IBufferProducer::RequestBufferReturnValue retval;
106 GSError ret = bqp->RequestBuffer(requestConfig, bedata, retval);
107 ASSERT_EQ(ret, OHOS::GSERROR_OK);
108
109 ret = bqp->CancelBuffer(retval.sequence, bedata);
110 ASSERT_EQ(ret, OHOS::GSERROR_OK);
111 }
112
113 /*
114 * Function: RequestBuffer and CancelBuffer
115 * Type: Function
116 * Rank: Important(2)
117 * EnvConditions: N/A
118 * CaseDescription: 1. call RequestBuffer
119 * 2. call CancelBuffer 2 times
120 * 3. check ret
121 */
122 HWTEST_F(BufferQueueProducerTest, ReqCan002, Function | MediumTest | Level2)
123 {
124 IBufferProducer::RequestBufferReturnValue retval;
125 GSError ret = bqp->RequestBuffer(requestConfig, bedata, retval);
126 ASSERT_EQ(ret, OHOS::GSERROR_OK);
127
128 ret = bqp->CancelBuffer(retval.sequence, bedata);
129 ASSERT_EQ(ret, OHOS::GSERROR_OK);
130
131 ret = bqp->CancelBuffer(retval.sequence, bedata);
132 ASSERT_NE(ret, OHOS::GSERROR_OK);
133 }
134
135 /*
136 * Function: RequestBuffer, and CancelBuffer
137 * Type: Function
138 * Rank: Important(2)
139 * EnvConditions: N/A
140 * CaseDescription: 1. call RequestBuffer and CancelBuffer by different retval
141 * 2. check ret
142 */
143 HWTEST_F(BufferQueueProducerTest, ReqCan003, Function | MediumTest | Level2)
144 {
145 IBufferProducer::RequestBufferReturnValue retval1;
146 IBufferProducer::RequestBufferReturnValue retval2;
147 IBufferProducer::RequestBufferReturnValue retval3;
148
149 auto ret = bqp->RequestBuffer(requestConfig, bedata, retval1);
150 ASSERT_EQ(ret, OHOS::GSERROR_OK);
151 ASSERT_EQ(retval1.buffer, nullptr);
152
153 ret = bqp->RequestBuffer(requestConfig, bedata, retval2);
154 ASSERT_EQ(ret, OHOS::GSERROR_OK);
155 ASSERT_NE(retval2.buffer, nullptr);
156
157 ret = bqp->RequestBuffer(requestConfig, bedata, retval3);
158 ASSERT_NE(ret, OHOS::GSERROR_OK);
159 ASSERT_EQ(retval3.buffer, nullptr);
160
161 ret = bqp->CancelBuffer(retval1.sequence, bedata);
162 ASSERT_EQ(ret, OHOS::GSERROR_OK);
163
164 ret = bqp->CancelBuffer(retval2.sequence, bedata);
165 ASSERT_EQ(ret, OHOS::GSERROR_OK);
166
167 ret = bqp->CancelBuffer(retval3.sequence, bedata);
168 ASSERT_NE(ret, OHOS::GSERROR_OK);
169 }
170
171 /*
172 * Function: RequestBuffer, FlushBuffer, AcquireBuffer and ReleaseBuffer
173 * Type: Function
174 * Rank: Important(2)
175 * EnvConditions: N/A
176 * CaseDescription: 1. call RequestBuffer and FlushBuffer
177 * 2. call AcquireBuffer and ReleaseBuffer
178 * 3. check ret
179 */
180 HWTEST_F(BufferQueueProducerTest, ReqFlu001, Function | MediumTest | Level2)
181 {
182 IBufferProducer::RequestBufferReturnValue retval;
183 GSError ret = bqp->RequestBuffer(requestConfig, bedata, retval);
184 ASSERT_EQ(ret, OHOS::GSERROR_OK);
185
186 sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
187 ret = bqp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
188 ASSERT_EQ(ret, OHOS::GSERROR_OK);
189
190 ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damage);
191 ASSERT_EQ(ret, OHOS::GSERROR_OK);
192
193 sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
194 ret = bq->ReleaseBuffer(retval.buffer, releaseFence);
195 ASSERT_EQ(ret, OHOS::GSERROR_OK);
196 }
197
198 /*
199 * Function: RequestBuffer, FlushBuffer, AcquireBuffer and ReleaseBuffer
200 * Type: Function
201 * Rank: Important(2)
202 * EnvConditions: N/A
203 * CaseDescription: 1. call RequestBuffer and FlushBuffer
204 * 2. call FlushBuffer again
205 * 3. call AcquireBuffer and ReleaseBuffer
206 * 4. check ret
207 */
208 HWTEST_F(BufferQueueProducerTest, ReqFlu002, Function | MediumTest | Level2)
209 {
210 IBufferProducer::RequestBufferReturnValue retval;
211 GSError ret = bqp->RequestBuffer(requestConfig, bedata, retval);
212 ASSERT_EQ(ret, OHOS::GSERROR_OK);
213
214 sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
215 ret = bqp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
216 ASSERT_EQ(ret, OHOS::GSERROR_OK);
217
218 ret = bqp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
219 ASSERT_NE(ret, OHOS::GSERROR_OK);
220
221 ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damage);
222 ASSERT_EQ(ret, OHOS::GSERROR_OK);
223
224 sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
225 ret = bq->ReleaseBuffer(retval.buffer, releaseFence);
226 ASSERT_EQ(ret, OHOS::GSERROR_OK);
227 }
228 }
229