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
16#include <map>
17#include <gtest/gtest.h>
18
19#include <surface.h>
20#include "buffer_extra_data_impl.h"
21#include "buffer_queue.h"
22#include "buffer_consumer_listener.h"
23#include "buffer_log.h"
24#include "iconsumer_surface.h"
25#include "test_header.h"
26
27using namespace testing;
28using namespace testing::ext;
29
30namespace OHOS::Rosen {
31class BufferSharedTest : public testing::Test, public IBufferConsumerListenerClazz {
32public:
33    static void SetUpTestCase();
34    void OnBufferAvailable() override;
35    static void TearDownTestCase();
36
37    static inline sptr<IConsumerSurface> surf = nullptr;
38    static inline sptr<Surface> producerSurface1 = nullptr;
39    static inline sptr<Surface> producerSurface2 = nullptr;
40    static inline sptr<SurfaceBuffer> buffer1 = nullptr;
41    static inline sptr<SurfaceBuffer> buffer2 = nullptr;
42    static inline sptr<SurfaceBuffer> sbuffer1 = nullptr;
43    static inline sptr<SurfaceBuffer> sbuffer2 = nullptr;
44};
45
46void BufferSharedTest::SetUpTestCase()
47{
48    GTEST_LOG_(INFO) << getpid() << std::endl;
49    surf = IConsumerSurface::Create("shared", true);
50    sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
51    surf->RegisterConsumerListener(listener);
52    auto producer1 = surf->GetProducer();
53    producerSurface1 = Surface::CreateSurfaceAsProducer(producer1);
54    auto producer2 = surf->GetProducer();
55    producerSurface2 = Surface::CreateSurfaceAsProducer(producer2);
56}
57
58void BufferSharedTest::OnBufferAvailable() {}
59
60void BufferSharedTest::TearDownTestCase()
61{
62}
63
64/*
65* Function: RequestBuffer
66* Type: Reliability
67* Rank: Important(2)
68* EnvConditions: N/A
69* CaseDescription: 1. call RequestBuffer with buffer=buffer1, buffer2,the param is same
70*                  2. check ret1 and ret2 are OHOS::GSERROR_OK, check buffer1 and buffer2 is not nullptr
71*                  3. check the addr of buffer1 EQ buffer2
72* */
73
74HWTEST_F(BufferSharedTest, RequestBuffer001, Function | MediumTest | Level2)
75{
76    PART("REQUEST BUFFER TWO TIMES") {
77        GSError ret1, ret2;
78        STEP("1: request buffer") {
79            BufferRequestConfig requestConfig = {
80                .width = 0x100,
81                .height = 0x100,
82                .strideAlignment = 0x8,
83                .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
84                .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
85                .timeout = 0,
86            };
87            int releaseFence = -1;
88            ret1 = producerSurface1->RequestBuffer(buffer1, releaseFence, requestConfig);
89            ret2 = producerSurface2->RequestBuffer(buffer2, releaseFence, requestConfig);
90        }
91        STEP("2: check ret1 ret2 buffer1 buffer2") {
92            STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
93            STEP_ASSERT_NE(buffer1, nullptr);
94            STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
95            STEP_ASSERT_NE(buffer2, nullptr);
96        }
97        STEP("3: check buffer addr") {
98            STEP_ASSERT_EQ(buffer2, buffer1);
99        }
100    }
101}
102/*
103* Function: RequestBuffer with different requestconfig
104* Type: Reliability
105* Rank: Important(2)
106* EnvConditions: N/A
107* CaseDescription: 1. call RequestBuffer with buffer=bufferDiff,
108*                     the requestconfig is not same with buffer1
109*                  2. check ret1 is GSERROR_INVALID_ARGUMENTS
110* */
111
112HWTEST_F(BufferSharedTest, RequestBufferDiff001, Function | MediumTest | Level2)
113{
114    PART("REQUEST BUFFER with different requestconfig") {
115        GSError ret1;
116        sptr<SurfaceBuffer> bufferDiff = nullptr;
117        STEP("1: request buffer") {
118            BufferRequestConfig diffRequestConfig = {
119                .width = 0x200,
120                .height = 0x100,
121                .strideAlignment = 0x8,
122                .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
123                .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
124                .timeout = 0,
125            };
126            int releaseFence = -1;
127            ret1 = producerSurface1->RequestBuffer(bufferDiff, releaseFence, diffRequestConfig);
128        }
129        STEP("2: check ret1") {
130            STEP_ASSERT_EQ(ret1, SURFACE_ERROR_UNKOWN);
131        }
132    }
133}
134/*
135* Function: FlushBuffer
136* Type: Reliability
137* Rank: Important(2)
138* EnvConditions: N/A
139* CaseDescription: 1. call FlushBuffer with buffer=buffer1, buffer2
140*                  2. check ret1 and ret2 is OHOS::GSERROR_OK
141* */
142HWTEST_F(BufferSharedTest, FlushBuffer001,  Function | MediumTest | Level2)
143{
144    PART("FlushBuffer") {
145        GSError ret1, ret2;
146        STEP("1: FlushBuffer two times") {
147            BufferFlushConfig flushConfig = { .damage = { .w = 0x100, .h = 0x100, }, };
148            ret1 = producerSurface1->FlushBuffer(buffer1, -1, flushConfig);
149            ret2 = producerSurface2->FlushBuffer(buffer2, -1, flushConfig);
150        }
151        STEP("2: check ret1 ret2") {
152            STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
153            STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
154        }
155    }
156}
157/*
158* Function: AquiredBuffer
159* Type: Reliability
160* Rank: Important(2)
161* EnvConditions: N/A
162* CaseDescription: 1. call AcquireBuffer with buffer=sbuffer1, sbuffer2
163*                  2. check ret1 and ret2 are GSERROR_INVALID_ARGUMENTS
164* */
165HWTEST_F(BufferSharedTest, AquiredBuffer001, Function | MediumTest | Level2)
166{
167    PART("AquiredBuffer") {
168        GSError ret1, ret2;
169        STEP("1: AcquireBuffer two times") {
170            int64_t timestamp = 0;
171            Rect damage = {};
172            int32_t fence = -1;
173
174            ret1 = surf->AcquireBuffer(sbuffer1, fence, timestamp, damage);
175            ret2 = surf->AcquireBuffer(sbuffer2, fence, timestamp, damage);
176        }
177        STEP("2: check ret1 ret2") {
178            STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
179            STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
180        }
181        STEP("3: check addr sbuffer1 and sbuffer2") {
182            STEP_ASSERT_EQ(sbuffer1, sbuffer2);
183        }
184    }
185}
186/*
187* Function: CancelBuffer
188* Type: Reliability
189* Rank: Important(2)
190* EnvConditions: N/A
191* CaseDescription: 1. call cancelBuffer with buffer=buffer1
192*                  2. check ret1 is GSERROR_INVALID_OPERATING
193*                  3. call cancelBuffer with buffer=buffer2
194*                  4. check ret2 is GSERROR_INVALID_OPERATING
195* */
196HWTEST_F(BufferSharedTest, CancelBuffer001, Function | MediumTest | Level2)
197{
198    PART("CancelBuffer") {
199        GSError ret1, ret2;
200        STEP("1: Cancel buffer1") {
201            ret1 = producerSurface1->CancelBuffer(buffer1);
202        }
203        STEP("2: check ret1") {
204            STEP_ASSERT_EQ(ret1, GSERROR_INVALID_OPERATING);
205        }
206        STEP("3: Cancel buffer2") {
207            ret2 = producerSurface2->CancelBuffer(buffer2);
208        }
209        STEP("4: check ret2") {
210            STEP_ASSERT_EQ(ret2, GSERROR_INVALID_OPERATING);
211        }
212    }
213}
214/*
215* Function: RelaseBuffer
216* Type: Reliability
217* Rank: Important(2)
218* EnvConditions: N/A
219* CaseDescription: 1. releaseBuffer two times
220*                  2. check ret1 is GSERROR_INVALID_OPERATING, check ret1 is OHOS::GSERROR_OK
221* */
222HWTEST_F(BufferSharedTest, ReleaseBuffer001, Function | MediumTest | Level2)
223{
224    PART("ReleaseBuffer") {
225        GSError ret1, ret2;
226        STEP("1: releaseBuffer two times") {
227            ret1 = surf->ReleaseBuffer(sbuffer1, -1);
228            ret2 = surf->ReleaseBuffer(sbuffer2, -1);
229        }
230        STEP("2: check ret1, ret2") {
231            STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
232            STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
233        }
234    }
235}
236}
237