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
16 #include "pixel_map_from_surface.h"
17 #include "iconsumer_surface.h"
18 #include "ibuffer_consumer_listener.h"
19 #include <gtest/gtest.h>
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 class BufferConsumerListener : public ::OHOS::IBufferConsumerListener {
27 public:
OnBufferAvailable()28 void OnBufferAvailable() override
29 {
30 }
31 };
32 class PixelMapFromSurfaceTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
OnBufferRelease(sptr<SurfaceBuffer> & buffer)36 static inline GSError OnBufferRelease(sptr<SurfaceBuffer> &buffer)
37 {
38 return GSERROR_OK;
39 }
40 };
41
SetUpTestCase()42 void PixelMapFromSurfaceTest::SetUpTestCase() {}
43
TearDownTestCase()44 void PixelMapFromSurfaceTest::TearDownTestCase() {}
45
46 namespace {
47 /*
48 * Function: CreatePixelMapFromSurface001
49 * Type: Function
50 * Rank: Important(2)
51 * EnvConditions: N/A
52 * CaseDescription:
53 1. call CreatePixelMapFromSurface with nullptr suface and should return nullptr
54 2. call CreatePixelMapFromSurface with incorrect rect.left and should return nullptr
55 3. call CreatePixelMapFromSurface with incorrect rect.top and should return nullptr
56 4. call CreatePixelMapFromSurface with incorrect rect.width and should return nullptr
57 5. call CreatePixelMapFromSurface with incorrect rect.height and should return nullptr
58 */
59 HWTEST_F(PixelMapFromSurfaceTest, CreatePixelMapFromSurface001, Function | MediumTest| Level3)
60 {
61 OHOS::Media::Rect srcRect = {0, 0, 100, 100};
62 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurface(nullptr, srcRect), nullptr);
63 auto cSurface = IConsumerSurface::Create();
64 ASSERT_NE(cSurface, nullptr);
65 auto producer = cSurface->GetProducer();
66 auto pSurface = Surface::CreateSurfaceAsProducer(producer);
67 ASSERT_NE(pSurface, nullptr);
68 srcRect = {-1, 0, 100, 100};
69 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurface(pSurface, srcRect), nullptr);
70 srcRect = {0, -1, 100, 100};
71 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurface(pSurface, srcRect), nullptr);
72 srcRect = {0, 0, 0, 100};
73 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurface(pSurface, srcRect), nullptr);
74 srcRect = {0, 0, 100, 0};
75 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurface(pSurface, srcRect), nullptr);
76 }
77
78 /*
79 * Function: CreatePixelMapFromSurface002
80 * Type: Function
81 * Rank: Important(2)
82 * EnvConditions: N/A
83 * CaseDescription:
84 1. call CreatePixelMapFromSurface should fail because this Process is not render_service
85 */
86 HWTEST_F(PixelMapFromSurfaceTest, CreatePixelMapFromSurface002, Function | MediumTest| Level3)
87 {
88 BufferRequestConfig requestConfig = {
89 .width = 100,
90 .height = 100,
91 .strideAlignment = 0x8,
92 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
93 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
94 .timeout = 0,
95 };
96 BufferFlushConfig flushConfig = {
97 .damage = {
98 .w = 100,
99 .h = 100,
100 },
101 };
102
103 auto csurf = IConsumerSurface::Create();
104 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
105 csurf->RegisterConsumerListener(listener);
106 auto producer = csurf->GetProducer();
107 auto pSurface = Surface::CreateSurfaceAsProducer(producer);
108 pSurface->RegisterReleaseListener(OnBufferRelease);
109
110 sptr<SurfaceBuffer> buffer = nullptr;
111 int releaseFence = -1;
112 GSError ret = pSurface->RequestBuffer(buffer, releaseFence, requestConfig);
113 ASSERT_EQ(ret, OHOS::GSERROR_OK);
114 ASSERT_NE(buffer, nullptr);
115 ret = pSurface->FlushBuffer(buffer, -1, flushConfig);
116 ASSERT_EQ(ret, OHOS::GSERROR_OK);
117
118 // CreatePixelMapFromSurface should fail because this Process is not render_service
119 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurface(pSurface, {0, 0, 100, 100}), nullptr);
120 }
121
122 /*
123 * Function: CreatePixelMapFromSurfaceBuffer001
124 * Type: Function
125 * Rank: Important(2)
126 * EnvConditions: N/A
127 * CaseDescription:
128 1. call CreatePixelMapFromSurfaceBuffer with nullptr suface and should return nullptr
129 2. call CreatePixelMapFromSurfaceBuffer with incorrect rect.left and should return nullptr
130 3. call CreatePixelMapFromSurfaceBuffer with incorrect rect.top and should return nullptr
131 4. call CreatePixelMapFromSurfaceBuffer with incorrect rect.width and should return nullptr
132 5. call CreatePixelMapFromSurfaceBuffer with incorrect rect.height and should return nullptr
133 6. call CreatePixelMapFromSurfaceBuffer without GPUContext and should return nullptr
134 */
135 HWTEST_F(PixelMapFromSurfaceTest, CreatePixelMapFromSurfaceBuffer001, Function | MediumTest| Level3)
136 {
137 OHOS::Media::Rect srcRect = {0, 0, 100, 100};
138 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurfaceBuffer(nullptr, srcRect), nullptr);
139 sptr<SurfaceBuffer> surfaceBuffer = nullptr;
140 ASSERT_EQ(surfaceBuffer, nullptr);
141 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurfaceBuffer(surfaceBuffer, srcRect), nullptr);
142 surfaceBuffer = SurfaceBuffer::Create().GetRefPtr();
143 ASSERT_NE(surfaceBuffer, nullptr);
144 srcRect = {-1, 0, 100, 100};
145 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurfaceBuffer(surfaceBuffer, srcRect), nullptr);
146 srcRect = {0, -1, 100, 100};
147 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurfaceBuffer(surfaceBuffer, srcRect), nullptr);
148 srcRect = {0, 0, 0, 100};
149 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurfaceBuffer(surfaceBuffer, srcRect), nullptr);
150 srcRect = {0, 0, 100, 0};
151 ASSERT_EQ(OHOS::Rosen::CreatePixelMapFromSurfaceBuffer(surfaceBuffer, srcRect), nullptr);
152 }
153 } // namespace
154 } // namespace Rosen
155 } // namespace OHOS