1 /*
2 * Copyright (c) 2024 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 <securec.h>
16 #include <gtest/gtest.h>
17 #include <surface.h>
18 #include <consumer_surface.h>
19 #include <native_window.h>
20 #include "buffer_consumer_listener.h"
21 #include "sync_fence.h"
22 #include <message_option.h>
23 #include <message_parcel.h>
24 #include "transact_surface_delegator_stub.h"
25 #include "consumer_surface.h"
26 #include "producer_surface_delegator.h"
27 #include "buffer_queue_producer.h"
28
29 using namespace testing;
30 using namespace testing::ext;
31
32 namespace OHOS::Rosen {
33 class ProducerSurfaceDelegatorTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37
38 static inline sptr<IConsumerSurface> csurf = nullptr;
39 static inline sptr<ProducerSurfaceDelegator> qwe = nullptr;
40 static inline sptr<IBufferProducer> producer = nullptr;
41 static inline sptr<Surface> pSurface = nullptr;
42 static inline sptr<SurfaceBuffer> pBuffer = nullptr;
43 static inline sptr<SurfaceBuffer> cBuffer = nullptr;
44 };
45
46 class IRemoteObjectMocker : public IRemoteObject {
47 public:
IRemoteObjectMocker()48 IRemoteObjectMocker() : IRemoteObject{u"IRemoteObjectMocker"} {}
~IRemoteObjectMocker()49 ~IRemoteObjectMocker() {}
GetObjectRefCount()50 int32_t GetObjectRefCount() { return 0; }
51
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)52 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
53 {
54 return 0;
55 }
56
IsProxyObject() const57 bool IsProxyObject() const
58 {
59 return true;
60 }
61
CheckObjectLegality() const62 bool CheckObjectLegality() const
63 {
64 return true;
65 }
66
AddDeathRecipient(const sptr<DeathRecipient> & recipient)67 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient)
68 {
69 return true;
70 }
71
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)72 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient)
73 {
74 return true;
75 }
76
AsInterface()77 sptr<IRemoteBroker> AsInterface()
78 {
79 return nullptr;
80 }
81
Dump(int fd,const std::vector<std::u16string> & args)82 int Dump(int fd, const std::vector<std::u16string> &args)
83 {
84 return 0;
85 }
86 };
87
SetUpTestCase()88 void ProducerSurfaceDelegatorTest::SetUpTestCase()
89 {
90 csurf = IConsumerSurface::Create();
91 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
92 csurf->RegisterConsumerListener(listener);
93 producer = csurf->GetProducer();
94 pSurface = Surface::CreateSurfaceAsProducer(producer);
95 qwe = new ProducerSurfaceDelegator();
96 sptr<IRemoteObjectMocker> remoteObjectMocker = new IRemoteObjectMocker();
97 qwe->SetClient(remoteObjectMocker);
98 pBuffer = SurfaceBuffer::Create();
99 }
100
TearDownTestCase()101 void ProducerSurfaceDelegatorTest::TearDownTestCase()
102 {
103 pSurface->UnRegisterReleaseListener();
104 qwe = nullptr;
105 csurf = nullptr;
106 producer = nullptr;
107 pSurface = nullptr;
108 pBuffer = nullptr;
109 }
110
111 /*
112 * Function: QueueBuffer
113 * Type: Function
114 * Rank: Important(2)
115 * EnvConditions: N/A
116 * CaseDescription: 1. call QueueBuffer
117 * 2. check ret
118 */
119 HWTEST_F(ProducerSurfaceDelegatorTest, QueueBuffer002, Function | MediumTest | Level2)
120 {
121 int32_t slot = 1;
122 int32_t acquireFence = 3;
123 sptr<Surface> aSurface = Surface::CreateSurfaceAsProducer(producer);
124 qwe->SetSurface(aSurface);
125 GSError ret = qwe->QueueBuffer(slot, acquireFence);
126 ASSERT_EQ(ret, GSERROR_OK);
127 }
128
129 /*
130 * Function: DequeueBuffer
131 * Type: Function
132 * Rank: Important(2)
133 * EnvConditions: N/A
134 * CaseDescription: 1. call DequeueBuffer
135 * 2. check ret
136 */
137 HWTEST_F(ProducerSurfaceDelegatorTest, DequeueBuffer002, Function | MediumTest | Level2)
138 {
139 int32_t slot = 1;
140 qwe->SetSurface(pSurface);
141 GSError ret = qwe->DequeueBuffer(slot, pBuffer);
142 ASSERT_EQ(ret, GSERROR_OK);
143 }
144
145 /*
146 * Function: QueueBuffer
147 * Type: Function
148 * Rank: Important(2)
149 * EnvConditions: N/A
150 * CaseDescription: 1. call QueueBuffer
151 * 2. check SendMessage ret
152 */
153 HWTEST_F(ProducerSurfaceDelegatorTest, QueueBuffer003, Function | MediumTest | Level2)
154 {
155 int32_t slot = 1;
156 int32_t acquireFence = 3;
157 qwe->SetSurface(pSurface);
158 GSError ret = qwe->QueueBuffer(slot, acquireFence);
159 ASSERT_EQ(ret, GSERROR_OK);
160 }
161
162 /*
163 * Function: ReleaseBuffer
164 * Type: Function
165 * Rank: Important(2)
166 * EnvConditions: N/A
167 * CaseDescription: 1. call ReleaseBuffer
168 * 2. check ret
169 */
170 HWTEST_F(ProducerSurfaceDelegatorTest, ReleaseBuffer001, Function | MediumTest | Level2)
171 {
172 sptr<SyncFence> fence = SyncFence::INVALID_FENCE;
173 GSError ret = qwe->ReleaseBuffer(pBuffer, fence);
174 ASSERT_EQ(ret, GSERROR_OK);
175 }
176
177 /*
178 * Function: DetachBuffer
179 * Type: Function
180 * Rank: Important(2)
181 * EnvConditions: N/A
182 * CaseDescription: 1. call DetachBuffer
183 * 2. check ret
184 */
185 HWTEST_F(ProducerSurfaceDelegatorTest, DetachBuffer001, Function | MediumTest | Level2)
186 {
187 int32_t slot = -1;
188 GSError ret = qwe->DetachBuffer(slot);
189 ASSERT_EQ(ret, GSERROR_OK);
190 }
191
192 /*
193 * Function: CancelBuffer
194 * Type: Function
195 * Rank: Important(2)
196 * EnvConditions: N/A
197 * CaseDescription: 1. call CancelBuffer
198 * 2. check ret
199 */
200 HWTEST_F(ProducerSurfaceDelegatorTest, CancelBuffer001, Function | MediumTest | Level2)
201 {
202 int32_t slot = -1;
203 int32_t fenceFd = -1;
204 GSError ret = qwe->CancelBuffer(slot, fenceFd);
205 ASSERT_EQ(ret, GSERROR_OK);
206 }
207
208 /*
209 * Function: ClearBufferSlot
210 * Type: Function
211 * Rank: Important(2)
212 * EnvConditions: N/A
213 * CaseDescription: 1. call ClearBufferSlot
214 * 2. check ret
215 */
216 HWTEST_F(ProducerSurfaceDelegatorTest, ClearBufferSlot001, Function | MediumTest | Level2)
217 {
218 int32_t slot = -1;
219 GSError ret = qwe->ClearBufferSlot(slot);
220 ASSERT_EQ(ret, GSERROR_OK);
221 }
222
223 /*
224 * Function: OnRemoteRequest
225 * Type: Function
226 * Rank: Important(2)
227 * EnvConditions: N/A
228 * CaseDescription: 1. call OnRemoteRequest
229 * 2. check ret
230 */
231 HWTEST_F(ProducerSurfaceDelegatorTest, OnRemoteRequest001, Function | MediumTest | Level2)
232 {
233 uint32_t code = 1; // QUEUEBUFFER
234 MessageParcel reply;
235 MessageOption option;
236 MessageParcel dataQueue;
237 dataQueue.WriteInt32(10);
238 dataQueue.WriteFileDescriptor(20);
239 int ret2 = qwe->OnRemoteRequest(code, dataQueue, reply, option);
240 ASSERT_EQ(ret2, ERR_NONE);
241 }
242
243 /*
244 * Function: ClearAllBuffers
245 * Type: Function
246 * Rank: Important(2)
247 * EnvConditions: N/A
248 * CaseDescription: 1. call ClearAllBuffers
249 * 2. check ret
250 */
251 HWTEST_F(ProducerSurfaceDelegatorTest, ClearAllBuffers001, Function | MediumTest | Level2)
252 {
253 GSError ret = qwe->ClearAllBuffers();
254 ASSERT_EQ(ret, GSERROR_OK);
255 }
256
257 /*
258 * Function: AddBufferLocked
259 * Type: Function
260 * Rank: Important(2)
261 * EnvConditions: N/A
262 * CaseDescription: 1. call AddBufferLocked
263 * 2. check ret
264 */
265 HWTEST_F(ProducerSurfaceDelegatorTest, AddBufferLocked001, Function | MediumTest | Level2)
266 {
267 ASSERT_NO_FATAL_FAILURE({
268 qwe->AddBufferLocked(nullptr, 0);
269 });
270 }
271
272 /*
273 * Function: GetBufferLocked
274 * Type: Function
275 * Rank: Important(2)
276 * EnvConditions: N/A
277 * CaseDescription: 1. call GetBufferLocked
278 * 2. check ret
279 */
280 HWTEST_F(ProducerSurfaceDelegatorTest, GetBufferLocked001, Function | MediumTest | Level2)
281 {
282 ASSERT_EQ(qwe->GetBufferLocked(0), nullptr);
283 }
284
285 /*
286 * Function: GetSlotLocked
287 * Type: Function
288 * Rank: Important(2)
289 * EnvConditions: N/A
290 * CaseDescription: 1. call GetSlotLocked
291 * 2. check ret
292 */
293 HWTEST_F(ProducerSurfaceDelegatorTest, GetSlotLocked001, Function | MediumTest | Level2)
294 {
295 ASSERT_EQ(qwe->GetSlotLocked(nullptr), 0);
296 }
297
298 /*
299 * Function: RetryFlushBuffer
300 * Type: Function
301 * Rank: Important(2)
302 * EnvConditions: N/A
303 * CaseDescription: 1. call RetryFlushBuffer
304 * 2. check ret
305 */
306 HWTEST_F(ProducerSurfaceDelegatorTest, RetryFlushBuffer001, Function | MediumTest | Level2)
307 {
308 BufferFlushConfig config = {
309 .damage = {
310 .x = 0,
311 .y = 0,
312 .w = 0,
313 .h = 0,
314 },
315 .timestamp = 0
316 };
317 GSError ret = qwe->RetryFlushBuffer(pBuffer, 0, config);
318 ASSERT_EQ(ret, GSERROR_OK);
319 }
320
321 /*
322 * Function: OnSetDataspace
323 * Type: Function
324 * Rank: Important(2)
325 * EnvConditions: N/A
326 * CaseDescription: 1. call OnSetDataspace
327 * 2. check ret
328 */
329 HWTEST_F(ProducerSurfaceDelegatorTest, SetDataspace001, Function | MediumTest | Level2)
330 {
331 MessageParcel reply;
332 MessageParcel data;
333 data.WriteUint32(1);
334 ASSERT_EQ(qwe->OnSetDataspace(data, reply), 0);
335 }
336
337 /*
338 * Function: HasSlotInSet
339 * Type: Function
340 * Rank: Important(2)
341 * EnvConditions: N/A
342 * CaseDescription: 1. call HasSlotInSet
343 * 2. check ret
344 */
345 HWTEST_F(ProducerSurfaceDelegatorTest, HasSlotInSet, Function | MediumTest | Level2)
346 {
347 sptr<ProducerSurfaceDelegator> delegator = new ProducerSurfaceDelegator();
348 bool ret = delegator->HasSlotInSet(0);
349 ASSERT_EQ(ret, false);
350 delegator->InsertSlotIntoSet(0);
351 ret = delegator->HasSlotInSet(0);
352 ASSERT_EQ(ret, true);
353 }
354
355 /*
356 * Function: InsertSlotIntoSet
357 * Type: Function
358 * Rank: Important(2)
359 * EnvConditions: N/A
360 * CaseDescription: 1. call InsertSlotIntoSet
361 * 2. check ret
362 */
363 HWTEST_F(ProducerSurfaceDelegatorTest, InsertSlotIntoSet, Function | MediumTest | Level2)
364 {
365 sptr<ProducerSurfaceDelegator> delegator = new ProducerSurfaceDelegator();
366 delegator->InsertSlotIntoSet(0);
367 delegator->InsertSlotIntoSet(1);
368 ASSERT_EQ(delegator->HasSlotInSet(0), true);
369 ASSERT_EQ(delegator->HasSlotInSet(1), true);
370 ASSERT_EQ(delegator->HasSlotInSet(2), false);
371 }
372
373 /*
374 * Function: EraseSlotFromSet
375 * Type: Function
376 * Rank: Important(2)
377 * EnvConditions: N/A
378 * CaseDescription: 1. call EraseSlotFromSet
379 * 2. check ret
380 */
381 HWTEST_F(ProducerSurfaceDelegatorTest, EraseSlotFromSet, Function | MediumTest | Level2)
382 {
383 sptr<ProducerSurfaceDelegator> delegator = new ProducerSurfaceDelegator();
384 delegator->InsertSlotIntoSet(0);
385 ASSERT_EQ(delegator->HasSlotInSet(0), true);
386 delegator->EraseSlotFromSet(0);
387 ASSERT_EQ(delegator->HasSlotInSet(0), false);
388 }
389 } // namespace OHOS::Rosen
390