• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 <surface_buffer_impl.h>
19 #include <buffer_utils.h>
20 #include <metadata_helper.h>
21 #include "v1_1/buffer_handle_meta_key_type.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::Rosen {
27 class SurfaceBufferImplTest : public testing::Test {
28 public:
29     static void SetUpTestCase();
30     static void TearDownTestCase();
31 
32     static inline BufferRequestConfig requestConfig = {
33         .width = 0x100,
34         .height = 0x100,
35         .strideAlignment = 0x8,
36         .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
37         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
38         .timeout = 0,
39         .colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_DCI_P3,
40     };
41     static inline sptr<SurfaceBuffer> buffer = nullptr;
42     static inline int32_t val32 = 0;
43     static inline int64_t val64 = 0;
44 };
45 
SetUpTestCase()46 void SurfaceBufferImplTest::SetUpTestCase()
47 {
48     buffer = nullptr;
49     val32 = 0;
50     val64 = 0;
51 }
52 
TearDownTestCase()53 void SurfaceBufferImplTest::TearDownTestCase()
54 {
55     buffer = nullptr;
56 }
57 
58 /*
59 * Function: GetSeqNum
60 * Type: Function
61 * Rank: Important(2)
62 * EnvConditions: N/A
63 * CaseDescription: 1. new SurfaceBufferImpl and GetSeqNum
64 *                  2. new SurfaceBufferImpl again and check GetSeqNum = oldSeq + 1
65 *                  3. set and verify the value of parameter isConsumerAttachBufferFlag_ is false
66 *                  4. set and verify the value of parameter isConsumerAttachBufferFlag_ is true
67  */
68 HWTEST_F(SurfaceBufferImplTest, NewSeqIncrease001, Function | MediumTest | Level2)
69 {
70     buffer = new SurfaceBufferImpl();
71     int oldSeq = buffer->GetSeqNum();
72 
73     buffer = new SurfaceBufferImpl();
74     ASSERT_EQ(oldSeq + 1, buffer->GetSeqNum());
75 
76     buffer->SetConsumerAttachBufferFlag(false);
77     ASSERT_EQ(buffer->GetConsumerAttachBufferFlag(), false);
78     buffer->SetConsumerAttachBufferFlag(true);
79     ASSERT_EQ(buffer->GetConsumerAttachBufferFlag(), true);
80 }
81 
82 /*
83 * Function: check buffer state
84 * Type: Function
85 * Rank: Important(2)
86 * EnvConditions: N/A
87 * CaseDescription: 1. check buffer state, such as bufferhandle, virAddr, fileDescriptor and size
88  */
89 HWTEST_F(SurfaceBufferImplTest, State001, Function | MediumTest | Level2)
90 {
91     ASSERT_EQ(buffer->GetBufferHandle(), nullptr);
92     ASSERT_EQ(buffer->GetVirAddr(), nullptr);
93     ASSERT_EQ(buffer->GetFileDescriptor(), -1);
94     ASSERT_EQ(buffer->GetSize(), 0u);
95 }
96 
97 /*
98 * Function: check buffer state
99 * Type: Function
100 * Rank: Important(2)
101 * EnvConditions: N/A
102 * CaseDescription: 1. call GetBufferHandle and Alloc
103 *                  2. check buffer state, such as bufferhandle, virAddr and size
104 *                  3. call Free
105 *                  4. check ret
106  */
107 HWTEST_F(SurfaceBufferImplTest, State002, Function | MediumTest | Level2)
108 {
109     ASSERT_EQ(buffer->GetBufferHandle(), nullptr);
110     ASSERT_EQ(buffer->GetPhyAddr(), 0);
111     ASSERT_EQ(buffer->GetStride(), -1);
112     vector<uint32_t> keys;
113     ASSERT_EQ(buffer->ListMetadataKeys(keys), GSERROR_NOT_INIT);
114     ASSERT_EQ(buffer->EraseMetadataKey(1), GSERROR_NOT_INIT);
115     GSError ret = buffer->Alloc(requestConfig);
116     ASSERT_EQ(ret, OHOS::GSERROR_OK);
117 
118     ASSERT_NE(buffer->GetBufferHandle(), nullptr);
119     ASSERT_NE(buffer->GetVirAddr(), nullptr);
120     ASSERT_NE(buffer->GetSize(), 0u);
121     ASSERT_EQ(buffer->GetFormat(), GRAPHIC_PIXEL_FMT_RGBA_8888);
122     ASSERT_EQ(buffer->GetUsage(), BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA);
123     ASSERT_EQ(buffer->GetSurfaceBufferColorGamut(), GraphicColorGamut::GRAPHIC_COLOR_GAMUT_DCI_P3);
124 
125     buffer->SetBufferHandle(nullptr);
126 }
127 
128 /*
129 * Function: parcel
130 * Type: Function
131 * Rank: Important(2)
132 * EnvConditions: N/A
133 * CaseDescription: 1. new SurfaceBufferImpl and Alloc
134 *                  2. call Set data interface
135 *                  3. call WriteSurfaceBufferImpl and ReadSurfaceBufferImpl
136 *                  4. call Get data interface
137 *                  5. check ret
138  */
139 HWTEST_F(SurfaceBufferImplTest, Parcel001, Function | MediumTest | Level2)
140 {
141     sptr<SurfaceBuffer> sbi = new SurfaceBufferImpl(0);
142     auto sret = sbi->Alloc(requestConfig);
143     ASSERT_EQ(sret, OHOS::GSERROR_OK);
144 
145     MessageParcel parcel;
146     WriteSurfaceBufferImpl(parcel, sbi->GetSeqNum(), sbi);
147 
148     sptr<SurfaceBuffer> buffer = nullptr;
149     uint32_t seq;
150     ReadSurfaceBufferImpl(parcel, seq, buffer);
151     ASSERT_NE(buffer, nullptr);
152 }
153 
154 /*
155 * Function: Create
156 * Type: Function
157 * Rank: Important(2)
158 * EnvConditions: N/A
159 * CaseDescription: 1. Call SurfaceBuffer::Create()
160 *                  2. check ret
161  */
162 HWTEST_F(SurfaceBufferImplTest, Create001, Function | MediumTest | Level2)
163 {
164     sptr<SurfaceBuffer> buffer = SurfaceBuffer::Create();
165     ASSERT_NE(buffer, nullptr);
166 }
167 
168 /*
169 * Function: Set/Get/List/Erase Metadata
170 * Type: Function
171 * Rank: Important(2)
172 * EnvConditions: N/A
173 * CaseDescription: 1. new SurfaceBufferImpl and Alloc
174 *                  2. call Set Metadata interface
175 *                  3. call Get Metadata interface
176 *                  4. check ret
177 *                  5. call List Metadata keys interface
178 *                  6. check ret
179 *                  7. call Erase Metadata key interface
180 *                  8. call List Metadata keys interface again
181 *                  9. check ret
182 */
183 HWTEST_F(SurfaceBufferImplTest, Metadata001, Function | MediumTest | Level2)
184 {
185     using namespace HDI::Display::Graphic::Common::V1_0;
186 
187     sptr<SurfaceBuffer> sbi = new SurfaceBufferImpl(0);
188     auto sret = sbi->Alloc(requestConfig);
189     ASSERT_EQ(sret, OHOS::GSERROR_OK);
190 
191     uint32_t metadataKey = 2;
192 
193     uint32_t setMetadata = 4260097;
194     std::vector<uint8_t> setData;
195     ASSERT_EQ(MetadataHelper::ConvertMetadataToVec(setMetadata, setData), OHOS::GSERROR_OK);
196     ASSERT_EQ(sbi->SetMetadata(0, setData), GSERROR_INVALID_ARGUMENTS);
197     ASSERT_EQ(sbi->SetMetadata(HDI::Display::Graphic::Common::V1_1::ATTRKEY_END, setData), GSERROR_INVALID_ARGUMENTS);
198     sret = sbi->SetMetadata(metadataKey, setData);
199     ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
200 
201     std::vector<uint8_t> getData;
202     ASSERT_EQ(sbi->GetMetadata(0, getData), GSERROR_INVALID_ARGUMENTS);
203     ASSERT_EQ(sbi->GetMetadata(HDI::Display::Graphic::Common::V1_1::ATTRKEY_END, getData), GSERROR_INVALID_ARGUMENTS);
204     sret = sbi->GetMetadata(metadataKey, getData);
205     ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
206 
207     if (sret == OHOS::GSERROR_OK) {
208         uint32_t getMetadata;
209         ASSERT_EQ(MetadataHelper::ConvertVecToMetadata(getData, getMetadata), OHOS::GSERROR_OK);
210         ASSERT_EQ(setMetadata, getMetadata);
211     }
212 
213     std::vector<uint32_t> keys;
214 
215     sret = sbi->ListMetadataKeys(keys);
216     ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
217     if (sret == OHOS::GSERROR_OK) {
218         ASSERT_EQ(sret, OHOS::GSERROR_OK);
219         ASSERT_EQ(keys.size(), 1);
220         ASSERT_EQ(keys[0], metadataKey);
221     }
222 
223     ASSERT_EQ(sbi->EraseMetadataKey(0), GSERROR_INVALID_ARGUMENTS);
224     ASSERT_EQ(sbi->EraseMetadataKey(HDI::Display::Graphic::Common::V1_1::ATTRKEY_END), GSERROR_INVALID_ARGUMENTS);
225     sret = sbi->EraseMetadataKey(metadataKey);
226     ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
227 
228     sret = sbi->ListMetadataKeys(keys);
229     ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
230     if (sret == OHOS::GSERROR_OK) {
231         ASSERT_EQ(keys.size(), 0);
232     }
233 }
234 
235 /*
236 * Function: BufferRequestConfig
237 * Type: Function
238 * Rank: Important(2)
239 * EnvConditions: N/A
240 * CaseDescription: 1. new SurfaceBufferImpl
241 *                  2. call SetBufferRequestConfig interface using requestConfig and check ret
242 *                  3. call GetBufferRequestConfig interface using requestConfig and check ret
243 *                  4. call WriteBufferRequestConfig interface and check ret
244 *                  5. call ReadBufferRequestConfig interface and check ret
245  */
246 HWTEST_F(SurfaceBufferImplTest, BufferRequestConfig001, Function | MediumTest | Level2)
247 {
248     buffer = new SurfaceBufferImpl();
249     MessageParcel parcel;
250     buffer->SetBufferRequestConfig(requestConfig);
251     ASSERT_EQ(buffer->GetBufferRequestConfig(), requestConfig);
252     ASSERT_EQ(buffer->WriteBufferRequestConfig(parcel), GSERROR_OK);
253     ASSERT_EQ(buffer->ReadBufferRequestConfig(parcel), GSERROR_OK);
254 }
255 
256 /*
257 * Function: SetSurfaceBufferScalingMode&GetSurfaceBufferScalingMode
258 * Type: Function
259 * Rank: Important(2)
260 * EnvConditions: N/A
261 * CaseDescription: 1. new SurfaceBufferImpl
262 *                  2. call GetSurfaceBufferScalingMode and check default is SCALING_MODE_SCALE_TO_WINDOW
263 *                  3. call SetSurfaceBufferScalingMode and GetSurfaceBufferScalingMode and check ret
264 *                  4. repeatly call SetSurfaceBufferScalingMode and GetSurfaceBufferScalingMode and check ret
265  */
266 HWTEST_F(SurfaceBufferImplTest, SurfaceBufferScalingMode001, Function | MediumTest | Level2)
267 {
268     buffer = new SurfaceBufferImpl();
269     ASSERT_EQ(buffer->GetSurfaceBufferScalingMode(), ScalingMode::SCALING_MODE_SCALE_TO_WINDOW);
270     buffer->SetSurfaceBufferScalingMode(ScalingMode::SCALING_MODE_SCALE_CROP);
271     ASSERT_EQ(buffer->GetSurfaceBufferScalingMode(), ScalingMode::SCALING_MODE_SCALE_CROP);
272     buffer->SetSurfaceBufferScalingMode(ScalingMode::SCALING_MODE_NO_SCALE_CROP);
273     ASSERT_EQ(buffer->GetSurfaceBufferScalingMode(), ScalingMode::SCALING_MODE_NO_SCALE_CROP);
274 }
275 }
276