132a6e48fSopenharmony_ci/*
232a6e48fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
332a6e48fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
432a6e48fSopenharmony_ci * you may not use this file except in compliance with the License.
532a6e48fSopenharmony_ci * You may obtain a copy of the License at
632a6e48fSopenharmony_ci *
732a6e48fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
832a6e48fSopenharmony_ci *
932a6e48fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1032a6e48fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1132a6e48fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1232a6e48fSopenharmony_ci * See the License for the specific language governing permissions and
1332a6e48fSopenharmony_ci * limitations under the License.
1432a6e48fSopenharmony_ci */
1532a6e48fSopenharmony_ci
1632a6e48fSopenharmony_ci#include <map>
1732a6e48fSopenharmony_ci#include <gtest/gtest.h>
1832a6e48fSopenharmony_ci
1932a6e48fSopenharmony_ci#include <surface.h>
2032a6e48fSopenharmony_ci#include "buffer_extra_data_impl.h"
2132a6e48fSopenharmony_ci#include "buffer_queue.h"
2232a6e48fSopenharmony_ci#include "buffer_consumer_listener.h"
2332a6e48fSopenharmony_ci#include "buffer_log.h"
2432a6e48fSopenharmony_ci#include "iconsumer_surface.h"
2532a6e48fSopenharmony_ci#include "test_header.h"
2632a6e48fSopenharmony_ci
2732a6e48fSopenharmony_ciusing namespace testing;
2832a6e48fSopenharmony_ciusing namespace testing::ext;
2932a6e48fSopenharmony_ci
3032a6e48fSopenharmony_cinamespace OHOS::Rosen {
3132a6e48fSopenharmony_ciclass BufferSharedTest : public testing::Test, public IBufferConsumerListenerClazz {
3232a6e48fSopenharmony_cipublic:
3332a6e48fSopenharmony_ci    static void SetUpTestCase();
3432a6e48fSopenharmony_ci    void OnBufferAvailable() override;
3532a6e48fSopenharmony_ci    static void TearDownTestCase();
3632a6e48fSopenharmony_ci
3732a6e48fSopenharmony_ci    static inline sptr<IConsumerSurface> surf = nullptr;
3832a6e48fSopenharmony_ci    static inline sptr<Surface> producerSurface1 = nullptr;
3932a6e48fSopenharmony_ci    static inline sptr<Surface> producerSurface2 = nullptr;
4032a6e48fSopenharmony_ci    static inline sptr<SurfaceBuffer> buffer1 = nullptr;
4132a6e48fSopenharmony_ci    static inline sptr<SurfaceBuffer> buffer2 = nullptr;
4232a6e48fSopenharmony_ci    static inline sptr<SurfaceBuffer> sbuffer1 = nullptr;
4332a6e48fSopenharmony_ci    static inline sptr<SurfaceBuffer> sbuffer2 = nullptr;
4432a6e48fSopenharmony_ci};
4532a6e48fSopenharmony_ci
4632a6e48fSopenharmony_civoid BufferSharedTest::SetUpTestCase()
4732a6e48fSopenharmony_ci{
4832a6e48fSopenharmony_ci    GTEST_LOG_(INFO) << getpid() << std::endl;
4932a6e48fSopenharmony_ci    surf = IConsumerSurface::Create("shared", true);
5032a6e48fSopenharmony_ci    sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
5132a6e48fSopenharmony_ci    surf->RegisterConsumerListener(listener);
5232a6e48fSopenharmony_ci    auto producer1 = surf->GetProducer();
5332a6e48fSopenharmony_ci    producerSurface1 = Surface::CreateSurfaceAsProducer(producer1);
5432a6e48fSopenharmony_ci    auto producer2 = surf->GetProducer();
5532a6e48fSopenharmony_ci    producerSurface2 = Surface::CreateSurfaceAsProducer(producer2);
5632a6e48fSopenharmony_ci}
5732a6e48fSopenharmony_ci
5832a6e48fSopenharmony_civoid BufferSharedTest::OnBufferAvailable() {}
5932a6e48fSopenharmony_ci
6032a6e48fSopenharmony_civoid BufferSharedTest::TearDownTestCase()
6132a6e48fSopenharmony_ci{
6232a6e48fSopenharmony_ci}
6332a6e48fSopenharmony_ci
6432a6e48fSopenharmony_ci/*
6532a6e48fSopenharmony_ci* Function: RequestBuffer
6632a6e48fSopenharmony_ci* Type: Reliability
6732a6e48fSopenharmony_ci* Rank: Important(2)
6832a6e48fSopenharmony_ci* EnvConditions: N/A
6932a6e48fSopenharmony_ci* CaseDescription: 1. call RequestBuffer with buffer=buffer1, buffer2,the param is same
7032a6e48fSopenharmony_ci*                  2. check ret1 and ret2 are OHOS::GSERROR_OK, check buffer1 and buffer2 is not nullptr
7132a6e48fSopenharmony_ci*                  3. check the addr of buffer1 EQ buffer2
7232a6e48fSopenharmony_ci* */
7332a6e48fSopenharmony_ci
7432a6e48fSopenharmony_ciHWTEST_F(BufferSharedTest, RequestBuffer001, Function | MediumTest | Level2)
7532a6e48fSopenharmony_ci{
7632a6e48fSopenharmony_ci    PART("REQUEST BUFFER TWO TIMES") {
7732a6e48fSopenharmony_ci        GSError ret1, ret2;
7832a6e48fSopenharmony_ci        STEP("1: request buffer") {
7932a6e48fSopenharmony_ci            BufferRequestConfig requestConfig = {
8032a6e48fSopenharmony_ci                .width = 0x100,
8132a6e48fSopenharmony_ci                .height = 0x100,
8232a6e48fSopenharmony_ci                .strideAlignment = 0x8,
8332a6e48fSopenharmony_ci                .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
8432a6e48fSopenharmony_ci                .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
8532a6e48fSopenharmony_ci                .timeout = 0,
8632a6e48fSopenharmony_ci            };
8732a6e48fSopenharmony_ci            int releaseFence = -1;
8832a6e48fSopenharmony_ci            ret1 = producerSurface1->RequestBuffer(buffer1, releaseFence, requestConfig);
8932a6e48fSopenharmony_ci            ret2 = producerSurface2->RequestBuffer(buffer2, releaseFence, requestConfig);
9032a6e48fSopenharmony_ci        }
9132a6e48fSopenharmony_ci        STEP("2: check ret1 ret2 buffer1 buffer2") {
9232a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
9332a6e48fSopenharmony_ci            STEP_ASSERT_NE(buffer1, nullptr);
9432a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
9532a6e48fSopenharmony_ci            STEP_ASSERT_NE(buffer2, nullptr);
9632a6e48fSopenharmony_ci        }
9732a6e48fSopenharmony_ci        STEP("3: check buffer addr") {
9832a6e48fSopenharmony_ci            STEP_ASSERT_EQ(buffer2, buffer1);
9932a6e48fSopenharmony_ci        }
10032a6e48fSopenharmony_ci    }
10132a6e48fSopenharmony_ci}
10232a6e48fSopenharmony_ci/*
10332a6e48fSopenharmony_ci* Function: RequestBuffer with different requestconfig
10432a6e48fSopenharmony_ci* Type: Reliability
10532a6e48fSopenharmony_ci* Rank: Important(2)
10632a6e48fSopenharmony_ci* EnvConditions: N/A
10732a6e48fSopenharmony_ci* CaseDescription: 1. call RequestBuffer with buffer=bufferDiff,
10832a6e48fSopenharmony_ci*                     the requestconfig is not same with buffer1
10932a6e48fSopenharmony_ci*                  2. check ret1 is GSERROR_INVALID_ARGUMENTS
11032a6e48fSopenharmony_ci* */
11132a6e48fSopenharmony_ci
11232a6e48fSopenharmony_ciHWTEST_F(BufferSharedTest, RequestBufferDiff001, Function | MediumTest | Level2)
11332a6e48fSopenharmony_ci{
11432a6e48fSopenharmony_ci    PART("REQUEST BUFFER with different requestconfig") {
11532a6e48fSopenharmony_ci        GSError ret1;
11632a6e48fSopenharmony_ci        sptr<SurfaceBuffer> bufferDiff = nullptr;
11732a6e48fSopenharmony_ci        STEP("1: request buffer") {
11832a6e48fSopenharmony_ci            BufferRequestConfig diffRequestConfig = {
11932a6e48fSopenharmony_ci                .width = 0x200,
12032a6e48fSopenharmony_ci                .height = 0x100,
12132a6e48fSopenharmony_ci                .strideAlignment = 0x8,
12232a6e48fSopenharmony_ci                .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
12332a6e48fSopenharmony_ci                .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
12432a6e48fSopenharmony_ci                .timeout = 0,
12532a6e48fSopenharmony_ci            };
12632a6e48fSopenharmony_ci            int releaseFence = -1;
12732a6e48fSopenharmony_ci            ret1 = producerSurface1->RequestBuffer(bufferDiff, releaseFence, diffRequestConfig);
12832a6e48fSopenharmony_ci        }
12932a6e48fSopenharmony_ci        STEP("2: check ret1") {
13032a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret1, SURFACE_ERROR_UNKOWN);
13132a6e48fSopenharmony_ci        }
13232a6e48fSopenharmony_ci    }
13332a6e48fSopenharmony_ci}
13432a6e48fSopenharmony_ci/*
13532a6e48fSopenharmony_ci* Function: FlushBuffer
13632a6e48fSopenharmony_ci* Type: Reliability
13732a6e48fSopenharmony_ci* Rank: Important(2)
13832a6e48fSopenharmony_ci* EnvConditions: N/A
13932a6e48fSopenharmony_ci* CaseDescription: 1. call FlushBuffer with buffer=buffer1, buffer2
14032a6e48fSopenharmony_ci*                  2. check ret1 and ret2 is OHOS::GSERROR_OK
14132a6e48fSopenharmony_ci* */
14232a6e48fSopenharmony_ciHWTEST_F(BufferSharedTest, FlushBuffer001,  Function | MediumTest | Level2)
14332a6e48fSopenharmony_ci{
14432a6e48fSopenharmony_ci    PART("FlushBuffer") {
14532a6e48fSopenharmony_ci        GSError ret1, ret2;
14632a6e48fSopenharmony_ci        STEP("1: FlushBuffer two times") {
14732a6e48fSopenharmony_ci            BufferFlushConfig flushConfig = { .damage = { .w = 0x100, .h = 0x100, }, };
14832a6e48fSopenharmony_ci            ret1 = producerSurface1->FlushBuffer(buffer1, -1, flushConfig);
14932a6e48fSopenharmony_ci            ret2 = producerSurface2->FlushBuffer(buffer2, -1, flushConfig);
15032a6e48fSopenharmony_ci        }
15132a6e48fSopenharmony_ci        STEP("2: check ret1 ret2") {
15232a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
15332a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
15432a6e48fSopenharmony_ci        }
15532a6e48fSopenharmony_ci    }
15632a6e48fSopenharmony_ci}
15732a6e48fSopenharmony_ci/*
15832a6e48fSopenharmony_ci* Function: AquiredBuffer
15932a6e48fSopenharmony_ci* Type: Reliability
16032a6e48fSopenharmony_ci* Rank: Important(2)
16132a6e48fSopenharmony_ci* EnvConditions: N/A
16232a6e48fSopenharmony_ci* CaseDescription: 1. call AcquireBuffer with buffer=sbuffer1, sbuffer2
16332a6e48fSopenharmony_ci*                  2. check ret1 and ret2 are GSERROR_INVALID_ARGUMENTS
16432a6e48fSopenharmony_ci* */
16532a6e48fSopenharmony_ciHWTEST_F(BufferSharedTest, AquiredBuffer001, Function | MediumTest | Level2)
16632a6e48fSopenharmony_ci{
16732a6e48fSopenharmony_ci    PART("AquiredBuffer") {
16832a6e48fSopenharmony_ci        GSError ret1, ret2;
16932a6e48fSopenharmony_ci        STEP("1: AcquireBuffer two times") {
17032a6e48fSopenharmony_ci            int64_t timestamp = 0;
17132a6e48fSopenharmony_ci            Rect damage = {};
17232a6e48fSopenharmony_ci            int32_t fence = -1;
17332a6e48fSopenharmony_ci
17432a6e48fSopenharmony_ci            ret1 = surf->AcquireBuffer(sbuffer1, fence, timestamp, damage);
17532a6e48fSopenharmony_ci            ret2 = surf->AcquireBuffer(sbuffer2, fence, timestamp, damage);
17632a6e48fSopenharmony_ci        }
17732a6e48fSopenharmony_ci        STEP("2: check ret1 ret2") {
17832a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
17932a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
18032a6e48fSopenharmony_ci        }
18132a6e48fSopenharmony_ci        STEP("3: check addr sbuffer1 and sbuffer2") {
18232a6e48fSopenharmony_ci            STEP_ASSERT_EQ(sbuffer1, sbuffer2);
18332a6e48fSopenharmony_ci        }
18432a6e48fSopenharmony_ci    }
18532a6e48fSopenharmony_ci}
18632a6e48fSopenharmony_ci/*
18732a6e48fSopenharmony_ci* Function: CancelBuffer
18832a6e48fSopenharmony_ci* Type: Reliability
18932a6e48fSopenharmony_ci* Rank: Important(2)
19032a6e48fSopenharmony_ci* EnvConditions: N/A
19132a6e48fSopenharmony_ci* CaseDescription: 1. call cancelBuffer with buffer=buffer1
19232a6e48fSopenharmony_ci*                  2. check ret1 is GSERROR_INVALID_OPERATING
19332a6e48fSopenharmony_ci*                  3. call cancelBuffer with buffer=buffer2
19432a6e48fSopenharmony_ci*                  4. check ret2 is GSERROR_INVALID_OPERATING
19532a6e48fSopenharmony_ci* */
19632a6e48fSopenharmony_ciHWTEST_F(BufferSharedTest, CancelBuffer001, Function | MediumTest | Level2)
19732a6e48fSopenharmony_ci{
19832a6e48fSopenharmony_ci    PART("CancelBuffer") {
19932a6e48fSopenharmony_ci        GSError ret1, ret2;
20032a6e48fSopenharmony_ci        STEP("1: Cancel buffer1") {
20132a6e48fSopenharmony_ci            ret1 = producerSurface1->CancelBuffer(buffer1);
20232a6e48fSopenharmony_ci        }
20332a6e48fSopenharmony_ci        STEP("2: check ret1") {
20432a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret1, GSERROR_INVALID_OPERATING);
20532a6e48fSopenharmony_ci        }
20632a6e48fSopenharmony_ci        STEP("3: Cancel buffer2") {
20732a6e48fSopenharmony_ci            ret2 = producerSurface2->CancelBuffer(buffer2);
20832a6e48fSopenharmony_ci        }
20932a6e48fSopenharmony_ci        STEP("4: check ret2") {
21032a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret2, GSERROR_INVALID_OPERATING);
21132a6e48fSopenharmony_ci        }
21232a6e48fSopenharmony_ci    }
21332a6e48fSopenharmony_ci}
21432a6e48fSopenharmony_ci/*
21532a6e48fSopenharmony_ci* Function: RelaseBuffer
21632a6e48fSopenharmony_ci* Type: Reliability
21732a6e48fSopenharmony_ci* Rank: Important(2)
21832a6e48fSopenharmony_ci* EnvConditions: N/A
21932a6e48fSopenharmony_ci* CaseDescription: 1. releaseBuffer two times
22032a6e48fSopenharmony_ci*                  2. check ret1 is GSERROR_INVALID_OPERATING, check ret1 is OHOS::GSERROR_OK
22132a6e48fSopenharmony_ci* */
22232a6e48fSopenharmony_ciHWTEST_F(BufferSharedTest, ReleaseBuffer001, Function | MediumTest | Level2)
22332a6e48fSopenharmony_ci{
22432a6e48fSopenharmony_ci    PART("ReleaseBuffer") {
22532a6e48fSopenharmony_ci        GSError ret1, ret2;
22632a6e48fSopenharmony_ci        STEP("1: releaseBuffer two times") {
22732a6e48fSopenharmony_ci            ret1 = surf->ReleaseBuffer(sbuffer1, -1);
22832a6e48fSopenharmony_ci            ret2 = surf->ReleaseBuffer(sbuffer2, -1);
22932a6e48fSopenharmony_ci        }
23032a6e48fSopenharmony_ci        STEP("2: check ret1, ret2") {
23132a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
23232a6e48fSopenharmony_ci            STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
23332a6e48fSopenharmony_ci        }
23432a6e48fSopenharmony_ci    }
23532a6e48fSopenharmony_ci}
23632a6e48fSopenharmony_ci}
237