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 "gtest/gtest.h"
17
18 #include "native_common_utils.h"
19 #include "effect_json_helper.h"
20 #include "common_utils.h"
21 #include "string_helper.h"
22 #include "format_helper.h"
23 #include "native_effect_base.h"
24 #include "memcpy_helper.h"
25 #include "event_report.h"
26 #include "mock_producer_surface.h"
27 #include "cJSON.h"
28
29 using namespace testing::ext;
30 namespace {
31 const float YUV_BYTES_PER_PIXEL = 1.5f;
32 const int32_t P10_BYTES_PER_LUMA = 2;
33 const u_int32_t RGBA_BYTES_PER_PIXEL = 4;
34 constexpr uint32_t WIDTH = 1920;
35 constexpr uint32_t HEIGHT = 1080;
36 constexpr uint32_t MAX_ALLOC_SIZE = 600 * 1024 * 1024;
37 }
38
39 namespace OHOS {
40 namespace Media {
41 namespace Effect {
42 namespace Test {
43
44 static const std::string TAG = "ParsePictureTest";
45 constexpr char TEST_INCLUDE_AUX_PATH[] = "/data/test/resource/camera_efilter_test.jpg";
46 constexpr char TEST_IMAGE_PATH[] = "/data/test/resource/image_effect_1k_test1.jpg";
47
CreatePictureByPath(std::string imagePath)48 static std::unique_ptr<Picture> CreatePictureByPath(std::string imagePath)
49 {
50 SourceOptions opts;
51 uint32_t errorCode = 0;
52 EFFECT_LOGW("%{public}s create pixelmap by path %{public}s", TAG.c_str(), imagePath.c_str());
53 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(imagePath, opts, errorCode);
54 CHECK_AND_RETURN_RET_LOG(imageSource != nullptr, nullptr,
55 "CreateImageSource fail! path=%{public}s, errorCode=%{public}d", imagePath.c_str(), errorCode);
56
57 DecodingOptionsForPicture decodingOptions;
58 decodingOptions.desireAuxiliaryPictures.insert(AuxiliaryPictureType::GAINMAP);
59 decodingOptions.desireAuxiliaryPictures.insert(AuxiliaryPictureType::DEPTH_MAP);
60 decodingOptions.desireAuxiliaryPictures.insert(AuxiliaryPictureType::UNREFOCUS_MAP);
61 decodingOptions.desireAuxiliaryPictures.insert(AuxiliaryPictureType::LINEAR_MAP);
62 std::unique_ptr<Picture> picture = imageSource->CreatePicture(decodingOptions, errorCode);
63 CHECK_AND_RETURN_RET_LOG(picture != nullptr, nullptr,
64 "CreatePicture fail! path=%{public}s, errorCode=%{public}d", imagePath.c_str(), errorCode);
65
66 return picture;
67 }
68
CreateEffectBufferByPicture(Picture * picture)69 static std::shared_ptr<EffectBuffer> CreateEffectBufferByPicture(Picture* picture)
70 {
71 if (picture == nullptr) {
72 return nullptr;
73 }
74
75 std::shared_ptr<PixelMap> pixelMap = picture->GetMainPixel();
76 if (pixelMap == nullptr) {
77 return nullptr;
78 }
79
80 std::shared_ptr<BufferInfo> info = std::make_shared<BufferInfo>();
81 info->width_ = pixelMap->GetWidth();
82 info->height_ = pixelMap->GetHeight();
83 info->rowStride_ = pixelMap->GetRowStride();
84 info->len_ = info->rowStride_ * info->height_;
85 info->formatType_ = IEffectFormat::RGBA8888;
86 info->surfaceBuffer_ = nullptr;
87 uint8_t *pixels = const_cast<uint8_t *>(pixelMap->GetPixels());
88 void *addr = static_cast<void *>(pixels);
89 info->pixelMap_ = pixelMap.get();
90
91 std::shared_ptr<ExtraInfo> extraInfo = std::make_shared<ExtraInfo>();
92 extraInfo->dataType = DataType::PIXEL_MAP;
93 extraInfo->bufferType = BufferType::HEAP_MEMORY;
94 extraInfo->picture = picture;
95
96 std::shared_ptr<EffectBuffer> effectBuf = std::make_shared<EffectBuffer>(info, addr, extraInfo);
97 return effectBuf != nullptr && effectBuf->buffer_ != nullptr ? effectBuf : nullptr;
98 }
99
100 class TestUtils : public testing::Test {
101 public:
102 TestUtils() = default;
103
104 ~TestUtils() override = default;
SetUpTestCase()105 static void SetUpTestCase() {}
106
TearDownTestCase()107 static void TearDownTestCase() {}
108
SetUp()109 void SetUp() override{}
110
TearDown()111 void TearDown() override{}
112 };
113
114 HWTEST_F(TestUtils, NativeCommonUtilsParseOHAny001, TestSize.Level1) {
115 ImageEffect_Any value;
116 value.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_INT32;
117 value.dataValue.int32Value = 123;
118 Any any;
119 ErrorCode result = NativeCommonUtils::ParseOHAny(&value, any);
120 ASSERT_EQ(result, ErrorCode::SUCCESS);
121 int actualValue = AnyCast<int>(any);
122 ASSERT_EQ(actualValue, 123);
123 }
124
125 HWTEST_F(TestUtils, NativeCommonUtilsParseOHAny002, TestSize.Level1) {
126 ImageEffect_Any value;
127 value.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_FLOAT;
128 value.dataValue.floatValue = 123.45f;
129 Any any;
130 ErrorCode result = NativeCommonUtils::ParseOHAny(&value, any);
131 ASSERT_EQ(result, ErrorCode::SUCCESS);
132 float actualValue = AnyCast<float>(any);
133 ASSERT_EQ(actualValue, 123.45f);
134 }
135
136 HWTEST_F(TestUtils, NativeCommonUtilsParseOHAny003, TestSize.Level1) {
137 ImageEffect_Any value;
138 value.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_DOUBLE;
139 value.dataValue.doubleValue = 123.45;
140 Any any;
141 ErrorCode result = NativeCommonUtils::ParseOHAny(&value, any);
142 ASSERT_EQ(result, ErrorCode::SUCCESS);
143 double actualValue = AnyCast<double>(any);
144 ASSERT_EQ(actualValue, 123.45);
145 }
146
147 HWTEST_F(TestUtils, NativeCommonUtilsParseOHAny004, TestSize.Level1) {
148 ImageEffect_Any value;
149 value.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_CHAR;
150 value.dataValue.charValue = 'A';
151 Any any;
152 ErrorCode result = NativeCommonUtils::ParseOHAny(&value, any);
153 ASSERT_EQ(result, ErrorCode::SUCCESS);
154 ASSERT_EQ(AnyCast<char>(any), 'A');
155 }
156
157 HWTEST_F(TestUtils, NativeCommonUtilsParseOHAny005, TestSize.Level1) {
158 ImageEffect_Any value;
159 value.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_LONG;
160 value.dataValue.longValue = 123456789L;
161 Any any;
162 ErrorCode result = NativeCommonUtils::ParseOHAny(&value, any);
163 ASSERT_EQ(result, ErrorCode::SUCCESS);
164 ASSERT_EQ(AnyCast<long>(any), 123456789L);
165 }
166
167 HWTEST_F(TestUtils, NativeCommonUtilsParseOHAny006, TestSize.Level1) {
168 ImageEffect_Any value;
169 value.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_PTR;
170 value.dataValue.ptrValue = (void*)0x12345678;
171 Any any;
172 ErrorCode result = NativeCommonUtils::ParseOHAny(&value, any);
173 ASSERT_EQ(result, ErrorCode::SUCCESS);
174 ASSERT_EQ(AnyCast<void*>(any), reinterpret_cast<void*>(0x12345678));
175 }
176
177 HWTEST_F(TestUtils, NativeCommonUtilsParseOHAny007, TestSize.Level1) {
178 ImageEffect_Any value;
179 Any any;
180 ErrorCode result = NativeCommonUtils::ParseOHAny(&value, any);
181 ASSERT_NE(result, ErrorCode::SUCCESS);
182 }
183
184 HWTEST_F(TestUtils, NativeCommonUtilsParseOHAny008, TestSize.Level1) {
185 ImageEffect_Any value;
186 value.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_BOOL;
187 value.dataValue.boolValue = true;
188 Any any;
189 EXPECT_EQ(NativeCommonUtils::ParseOHAny(&value, any), ErrorCode::SUCCESS);
190 }
191
192 HWTEST_F(TestUtils, NativeCommonUtilsSwitchToOHAny001, TestSize.Level1) {
193 Any any = 10.0;
194 ImageEffect_Any value;
195 value.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_DOUBLE;
196 ErrorCode result = NativeCommonUtils::SwitchToOHAny(any, &value);
197 EXPECT_EQ(result, ErrorCode::SUCCESS);
198 EXPECT_DOUBLE_EQ(value.dataValue.doubleValue, 10.0);
199
200 Any anyChar = 'a';
201 ImageEffect_Any valueChar;
202 valueChar.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_CHAR;
203 result = NativeCommonUtils::SwitchToOHAny(anyChar, &valueChar);
204 EXPECT_EQ(result, ErrorCode::SUCCESS);
205 EXPECT_EQ(valueChar.dataValue.charValue, 'a');
206
207 Any anyLong = 10L;
208 ImageEffect_Any valueLong;
209 valueLong.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_LONG;
210 result = NativeCommonUtils::SwitchToOHAny(anyLong, &valueLong);
211 EXPECT_EQ(result, ErrorCode::SUCCESS);
212 EXPECT_EQ(valueLong.dataValue.longValue, 10L);
213
214 Any anyPtr = (void*)10;
215 ImageEffect_Any valuePtr;
216 valuePtr.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_PTR;
217 result = NativeCommonUtils::SwitchToOHAny(anyPtr, &valuePtr);
218 EXPECT_EQ(result, ErrorCode::SUCCESS);
219 EXPECT_EQ(valuePtr.dataValue.ptrValue, (void*)10);
220
221 Any anyBool = true;
222 ImageEffect_Any valueBool;
223 valueBool.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_BOOL;
224 result = NativeCommonUtils::SwitchToOHAny(anyBool, &valueBool);
225 EXPECT_EQ(result, ErrorCode::SUCCESS);
226 EXPECT_EQ(valueBool.dataValue.boolValue, true);
227
228 Any anyUnknown = std::string("Unsupported");
229 ImageEffect_Any valueUnknown;
230 valueUnknown.dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_UNKNOWN;
231 result = NativeCommonUtils::SwitchToOHAny(anyUnknown, &valueUnknown);
232 EXPECT_EQ(result, ErrorCode::ERR_NOT_SUPPORT_SWITCH_TO_OHANY);
233 }
234
235 HWTEST_F(TestUtils, JsonHelper001, TestSize.Level1) {
236 EffectJsonPtr root = EffectJsonHelper::CreateObject();
237 ASSERT_NE(root, nullptr);
238 ASSERT_TRUE(root->IsObject());
239 ASSERT_TRUE(root->Put("stringKey", "testString"));
240 ASSERT_TRUE(root->Put("floatKey", 1.23f));
241 ASSERT_TRUE(root->Put("intKey", 123));
242 EffectJsonPtr intRoot = EffectJsonHelper::CreateArray();
243 ASSERT_TRUE(intRoot->Add(1));
244 ASSERT_TRUE(intRoot->Add(2));
245 ASSERT_TRUE(intRoot->Add(3));
246 ASSERT_TRUE(root->Put("arrayKey", intRoot));
247
248 ASSERT_TRUE(root->HasElement("stringKey"));
249 EffectJsonPtr stringKeyJsonPtr = root->GetElement("stringKey");
250 ASSERT_NE(stringKeyJsonPtr, nullptr);
251 ASSERT_TRUE(stringKeyJsonPtr->IsString());
252 ASSERT_FALSE(stringKeyJsonPtr->IsNumber());
253 ASSERT_EQ(stringKeyJsonPtr->GetInt(), 0);
254 ASSERT_EQ(stringKeyJsonPtr->GetUInt(), 0);
255 ASSERT_EQ(stringKeyJsonPtr->GetFloat(), 0);
256 ASSERT_EQ(stringKeyJsonPtr->GetDouble(), 0);
257 ASSERT_FALSE(stringKeyJsonPtr->GetBool());
258 std::string stringValue = stringKeyJsonPtr->GetString();
259 ASSERT_STREQ(stringValue.c_str(), "testString");
260
261 ASSERT_TRUE(root->HasElement("floatKey"));
262 EffectJsonPtr floatKeyJsonPtr = root->GetElement("floatKey");
263 ASSERT_NE(floatKeyJsonPtr, nullptr);
264 ASSERT_TRUE(floatKeyJsonPtr->IsNumber());
265 ASSERT_TRUE(floatKeyJsonPtr->GetString().empty());
266 ASSERT_FALSE(floatKeyJsonPtr->IsString());
267 float floatValue = floatKeyJsonPtr->GetFloat();
268 ASSERT_EQ(floatValue, 1.23f);
269
270 ASSERT_FALSE(root->HasElement("nonExistKey"));
271
272 ASSERT_TRUE(root->HasElement("arrayKey"));
273 EffectJsonPtr arrayKeyJsonPtr = root->GetElement("arrayKey");
274 ASSERT_NE(arrayKeyJsonPtr, nullptr);
275 ASSERT_TRUE(arrayKeyJsonPtr->IsArray());
276 std::vector<EffectJsonPtr> arrayJsonPtr = arrayKeyJsonPtr->GetArray();
277 ASSERT_EQ(arrayJsonPtr.size(), 3);
278 ASSERT_EQ(arrayJsonPtr[0]->GetInt(), 1);
279 ASSERT_EQ(arrayJsonPtr[1]->GetInt(), 2);
280 ASSERT_EQ(arrayJsonPtr[2]->GetInt(), 3);
281 }
282
283 HWTEST_F(TestUtils, NativeCommonUtilsParseJson001, TestSize.Level1) {
284 std::string key = "test_key";
285 Any any = nullptr;
286 Json *json = nullptr;
287 EffectJsonPtr result = std::make_shared<EffectJson>(json);
288 ErrorCode ret = CommonUtils::ParseAnyAndAddToJson(key, any, result);
289 ASSERT_EQ(ret, ErrorCode::ERR_ANY_CAST_TYPE_NOT_MATCH);
290 }
291 HWTEST_F(TestUtils, NativeCommonUtilsParseNativeWindowData001, TestSize.Level1) {
292 std::shared_ptr<BufferInfo> bufferinfo = std::make_unique<BufferInfo>();
293 void *addr = nullptr;
294 std::shared_ptr<ExtraInfo> extrainfo = std::make_unique<ExtraInfo>();
295 std::shared_ptr<EffectBuffer> effectBuffer = std::make_unique<EffectBuffer>(bufferinfo, addr, extrainfo);
296 DataType datatype = DataType::UNKNOWN;
297 ErrorCode result = CommonUtils::ParseNativeWindowData(effectBuffer, datatype);
298 ASSERT_EQ(result, ErrorCode::SUCCESS);
299 }
300 HWTEST_F(TestUtils, NativeCommonUtilsModifyPixelMapProperty001, TestSize.Level1) {
301 PixelMap pixelMap;
302 std::shared_ptr<BufferInfo> bufferinfo = std::make_unique<BufferInfo>();
303 void *addr = nullptr;
304 std::shared_ptr<ExtraInfo> extrainfo = std::make_unique<ExtraInfo>();
305 std::shared_ptr<EffectBuffer> buffer = std::make_unique<EffectBuffer>(bufferinfo, addr, extrainfo);
306 std::shared_ptr<EffectContext> context = std::make_unique<EffectContext>();
307 context->memoryManager_ = std::make_unique<EffectMemoryManager>();
308 ErrorCode result = CommonUtils::ModifyPixelMapProperty(&pixelMap, buffer, context);
309 EXPECT_EQ(result, ErrorCode::ERR_ALLOC_MEMORY_FAIL);
310 }
311 HWTEST_F(TestUtils, StringHelper001, TestSize.Level1) {
312 std::string input = "abc";
313 std::string suffix = "abcd";
314 std::string srcStr = "abcdef";
315 std::string endStr = "def";
316 std::shared_ptr<StringHelp> stringHelp = std::make_shared<StringHelp>();
317 EXPECT_FALSE(stringHelp->EndsWith(input, suffix));
318 EXPECT_FALSE(stringHelp->EndsWithIgnoreCase(input, suffix));
319 bool result = stringHelp->EndsWith(srcStr, endStr);
320 EXPECT_TRUE(result);
321 }
322
323 HWTEST_F(TestUtils, FormatHelper001, TestSize.Level1) {
324 uint32_t height = 1280;
325 uint32_t width = 960;
326 std::shared_ptr<FormatHelper> formatHelper = std::make_shared<FormatHelper>();
327 uint32_t result = formatHelper->CalculateDataRowCount(height, IEffectFormat::YUVNV12);
328 ASSERT_EQ(result, height * YUV_BYTES_PER_PIXEL);
329
330 result = formatHelper->CalculateDataRowCount(height, IEffectFormat::DEFAULT);
331 ASSERT_EQ(result, height);
332
333 result = formatHelper->CalculateRowStride(width, IEffectFormat::YCRCB_P010);
334 ASSERT_EQ(result, width * P10_BYTES_PER_LUMA);
335
336 result = formatHelper->CalculateRowStride(width, IEffectFormat::DEFAULT);
337 ASSERT_EQ(result, width);
338 }
339
AllocBuffer(size_t size)340 std::shared_ptr<void> AllocBuffer(size_t size)
341 {
342 if (size <= 0 || size > MAX_ALLOC_SIZE) {
343 return nullptr;
344 }
345
346 void *buffer = malloc(size);
347 if (buffer == nullptr) {
348 return nullptr;
349 }
350
351 std::shared_ptr<void> bufferPtr(buffer, [](void *buffer) {
352 if (buffer != nullptr) {
353 free(buffer);
354 }
355 });
356 return bufferPtr;
357 }
358
CreateConverterInfo(IEffectFormat format,void * addr,uint32_t rowStride)359 FormatConverterInfo CreateConverterInfo(IEffectFormat format, void *addr, uint32_t rowStride)
360 {
361 return {
362 .bufferInfo = {
363 .width_ = WIDTH,
364 .height_ = HEIGHT,
365 .len_ = FormatHelper::CalculateSize(WIDTH, HEIGHT, format),
366 .formatType_ = format,
367 .rowStride_ = rowStride,
368 },
369 .buffer = addr,
370 };
371 }
372
373 HWTEST_F(TestUtils, FormatHelper002, TestSize.Level1)
374 {
375 std::unordered_set<IEffectFormat> formats = FormatHelper::GetAllSupportedFormats();
376 ASSERT_NE(formats.size(), 0);
377
378 ASSERT_TRUE(FormatHelper::IsSupportConvert(IEffectFormat::RGBA8888, IEffectFormat::YUVNV21));
379
380 std::shared_ptr<void> rgbaBuffer = AllocBuffer(FormatHelper::CalculateSize(WIDTH, HEIGHT, IEffectFormat::RGBA8888));
381 FormatConverterInfo rgbaConverterInfo = CreateConverterInfo(IEffectFormat::RGBA8888, rgbaBuffer.get(),
382 RGBA_BYTES_PER_PIXEL * WIDTH);
383 std::shared_ptr<void> nv12Buffer = AllocBuffer(FormatHelper::CalculateSize(WIDTH, HEIGHT, IEffectFormat::YUVNV12));
384 FormatConverterInfo nv12ConverterInfo = CreateConverterInfo(IEffectFormat::YUVNV12, nv12Buffer.get(), WIDTH);
385 std::shared_ptr<void> nv21Buffer = AllocBuffer(FormatHelper::CalculateSize(WIDTH, HEIGHT, IEffectFormat::YUVNV21));
386 FormatConverterInfo nv21ConverterInfo = CreateConverterInfo(IEffectFormat::YUVNV21, nv21Buffer.get(), WIDTH);
387
388 ErrorCode res = FormatHelper::ConvertFormat(rgbaConverterInfo, nv12ConverterInfo);
389 ASSERT_EQ(res, ErrorCode::SUCCESS);
390
391 res = FormatHelper::ConvertFormat(rgbaConverterInfo, nv21ConverterInfo);
392 ASSERT_EQ(res, ErrorCode::SUCCESS);
393
394 res = FormatHelper::ConvertFormat(nv12ConverterInfo, rgbaConverterInfo);
395 ASSERT_EQ(res, ErrorCode::SUCCESS);
396
397 res = FormatHelper::ConvertFormat(nv21ConverterInfo, rgbaConverterInfo);
398 ASSERT_EQ(res, ErrorCode::SUCCESS);
399
400 res = FormatHelper::ConvertFormat(nv12ConverterInfo, nv21ConverterInfo);
401 ASSERT_NE(res, ErrorCode::SUCCESS);
402 }
403
404 HWTEST_F(TestUtils, NativeCommonUtils001, TestSize.Level1) {
405 ImageEffect_Format ohFormatType = ImageEffect_Format::EFFECT_PIXEL_FORMAT_RGBA8888;
406 IEffectFormat formatType;
407 std::shared_ptr<NativeCommonUtils> nativeCommonUtils = std::make_shared<NativeCommonUtils>();
408 nativeCommonUtils->SwitchToFormatType(ohFormatType, formatType);
409 ASSERT_EQ(formatType, IEffectFormat::RGBA8888);
410
411 ohFormatType = ImageEffect_Format::EFFECT_PIXEL_FORMAT_UNKNOWN;
412 nativeCommonUtils->SwitchToFormatType(ohFormatType, formatType);
413 ASSERT_EQ(formatType, IEffectFormat::DEFAULT);
414 }
415
416 HWTEST_F(TestUtils, ErrorCode001, TestSize.Level1) {
417 ErrorCode code = ErrorCode::ERR_PERMISSION_DENIED;
418 std::string expected = "ERROR_PERMISSION_DENIED";
419 std::string actual = GetErrorName(code);
420 ASSERT_EQ(expected, actual);
421
422 code = ErrorCode::ERR_INPUT_NULL;
423 expected = "Unknow error type";
424 actual = GetErrorName(code);
425 ASSERT_EQ(expected, actual);
426 }
427
428 HWTEST_F(TestUtils, MemcpyHelperCopyData001, TestSize.Level1)
429 {
430 MemoryData *src = new MemoryData();
431 MemoryData *dst = src;
432 MemcpyHelper::CopyData(src, dst);
433 EXPECT_EQ(src, dst);
434
435 dst = new MemoryData();
436 MemcpyHelper::CopyData(src, dst);
437 EXPECT_NE(src, dst);
438
439 MemcpyHelper::CopyData(src, src);
440 EXPECT_EQ(src, src);
441
442 std::shared_ptr<BufferInfo> bufferInfo = std::make_unique<BufferInfo>();
443 void *add = nullptr;
444 std::shared_ptr<ExtraInfo> extraInfo = std::make_unique<ExtraInfo>();
445 std::shared_ptr<EffectBuffer> dst2 = std::make_unique<EffectBuffer>(bufferInfo, add, extraInfo);
446 CopyInfo info = {
447 .bufferInfo = *dst2->bufferInfo_,
448 .data = static_cast<uint8_t *>(dst2->buffer_),
449 };
450 MemcpyHelper::CopyData(info, nullptr);
451 MemcpyHelper::CopyData(info, dst2.get());
452
453 delete src;
454 src = nullptr;
455 delete dst;
456 dst = nullptr;
457 }
458
459 HWTEST_F(TestUtils, MemcpyHelperCopyData002, TestSize.Level1)
460 {
461 std::shared_ptr<BufferInfo> bufferInfo = std::make_unique<BufferInfo>();
462 void *add = nullptr;
463 std::shared_ptr<ExtraInfo> extraInfo = std::make_unique<ExtraInfo>();
464 std::shared_ptr<EffectBuffer> src = std::make_unique<EffectBuffer>(bufferInfo, add, extraInfo);
465 std::shared_ptr<EffectBuffer> dst = src;
466
467 MemcpyHelper::CopyData(src.get(), dst.get());
468 EXPECT_EQ(src.get(), dst.get());
469 }
470
471 HWTEST_F(TestUtils, NativeCommonUtilsSwitchToOHEffectInfo001, TestSize.Level1)
472 {
473 EffectInfo effectInfo;
474 std::shared_ptr<OH_EffectFilterInfo> ohFilterInfo = std::make_shared<OH_EffectFilterInfo>();
475 std::vector<IPType> ipType;
476 ipType.emplace_back(IPType::DEFAULT);
477 effectInfo.formats_.emplace(IEffectFormat::DEFAULT, ipType);
478
479 NativeCommonUtils::SwitchToOHEffectInfo(&effectInfo, ohFilterInfo.get());
480 ASSERT_NE(ohFilterInfo->supportedBufferTypes.size(), 1);
481 ASSERT_EQ(ohFilterInfo->supportedFormats.size(), 1);
482 ASSERT_EQ(*(ohFilterInfo->supportedFormats.begin()), ImageEffect_Format::EFFECT_PIXEL_FORMAT_UNKNOWN);
483 }
484
485 HWTEST_F(TestUtils, NativeCommonUtilsGetSupportedFormats001, TestSize.Level1)
486 {
487 std::shared_ptr<OH_EffectFilterInfo> ohFilterInfo = nullptr;
488 uint32_t result = NativeCommonUtils::GetSupportedFormats(ohFilterInfo.get());
489 ASSERT_EQ(result, 0);
490
491 ohFilterInfo = std::make_shared<OH_EffectFilterInfo>();
492 ohFilterInfo->supportedFormats.emplace(ImageEffect_Format::EFFECT_PIXEL_FORMAT_YCRCB_P010);
493 result = NativeCommonUtils::GetSupportedFormats(ohFilterInfo.get());
494 ASSERT_EQ(result, 0);
495 }
496
497 HWTEST_F(TestUtils, NativeCommonUtilsConverStartResult001, TestSize.Level1)
498 {
499 ErrorCode errorCode = ErrorCode::ERR_ALLOC_MEMORY_FAIL;
500 ImageEffect_ErrorCode result = NativeCommonUtils::ConvertStartResult(errorCode);
501 ASSERT_EQ(result, ImageEffect_ErrorCode::EFFECT_ALLOCATE_MEMORY_FAILED);
502 }
503
504 HWTEST_F(TestUtils, ReportHiSysEvent_001, TestSize.Level1)
505 {
506 const EventInfo eventInfo = {
507 .errorInfo = {
508 .errorCode = 0,
509 .errorMsg = "test",
510 }
511 };
512 EventReport::ReportHiSysEvent("not_find_test", eventInfo);
513 }
514
515 HWTEST_F(TestUtils, CopyAuxiliaryBufferInfos_001, TestSize.Level1)
516 {
517 std::shared_ptr<BufferInfo> bufferinfo = std::make_unique<BufferInfo>();
518 void *addr = nullptr;
519 std::shared_ptr<ExtraInfo> extrainfo = std::make_unique<ExtraInfo>();
520 std::shared_ptr<EffectBuffer> src = std::make_unique<EffectBuffer>(bufferinfo, addr, extrainfo);
521 std::shared_ptr<EffectBuffer> dst = std::make_unique<EffectBuffer>(bufferinfo, addr, extrainfo);
522 CommonUtils::CopyAuxiliaryBufferInfos(src.get(), dst.get());
523 EXPECT_EQ(src->auxiliaryBufferInfos, nullptr);
524
525 src->auxiliaryBufferInfos = std::make_shared<std::unordered_map<EffectPixelmapType, std::shared_ptr<BufferInfo>>>();
526 CommonUtils::CopyAuxiliaryBufferInfos(src.get(), dst.get());
527 EXPECT_NE(src->auxiliaryBufferInfos, nullptr);
528 }
529
530 HWTEST_F(TestUtils, MetaData_001, TestSize.Level1)
531 {
532 sptr<SurfaceBuffer> inBuffer;
533 MockProducerSurface::AllocDmaMemory(inBuffer);
534
535 CommonUtils::GetMetaData(inBuffer);
536
537 MetaDataMap map;
538 CommonUtils::SetMetaData(map, inBuffer);
539
540 MockProducerSurface::ReleaseDmaBuffer(inBuffer);
541 }
542
543 HWTEST_F(TestUtils, ParsePixelMapData_001, TestSize.Level1)
544 {
545 std::shared_ptr<BufferInfo> bufferinfo = std::make_unique<BufferInfo>();
546 void *addr = nullptr;
547 std::shared_ptr<ExtraInfo> extrainfo = std::make_unique<ExtraInfo>();
548 std::shared_ptr<EffectBuffer> src = std::make_unique<EffectBuffer>(bufferinfo, addr, extrainfo);
549
550 PixelMap pixelMap;
551 ErrorCode res = CommonUtils::ParsePixelMapData(&pixelMap, src);
552 EXPECT_NE(res, ErrorCode::SUCCESS);
553 }
554
555 HWTEST_F(TestUtils, IsEnableCopyMetaData_001, TestSize.Level1)
556 {
557 bool res = CommonUtils::IsEnableCopyMetaData(2, nullptr, nullptr);
558 EXPECT_EQ(res, false);
559
560 std::shared_ptr<BufferInfo> bufferinfo = std::make_unique<BufferInfo>();
561 void *addr = nullptr;
562 std::shared_ptr<ExtraInfo> extrainfo = std::make_unique<ExtraInfo>();
563 std::shared_ptr<EffectBuffer> src = std::make_unique<EffectBuffer>(bufferinfo, addr, extrainfo);
564 src->bufferInfo_->bufferType_ = BufferType::DMA_BUFFER;
565 src->bufferInfo_->pixelMap_ = nullptr;
566 res = CommonUtils::IsEnableCopyMetaData(1, src.get());
567 EXPECT_EQ(res, false);
568
569 PixelMap pixelMap;
570 src->bufferInfo_->pixelMap_ = &pixelMap;
571 src->bufferInfo_->surfaceBuffer_ = nullptr;
572 res = CommonUtils::IsEnableCopyMetaData(1, src.get());
573 EXPECT_EQ(res, false);
574 }
575
576 HWTEST_F(TestUtils, JsonHelper002, TestSize.Level1) {
577 EffectJsonPtr root = EffectJsonHelper::CreateObject();
578 int32_t defaultValue1 = 0;
579 int32_t res1 = root->GetInt("test", defaultValue1);
580 EXPECT_EQ(res1, defaultValue1);
581 res1 = root->GetUInt("test", defaultValue1);
582 EXPECT_EQ(res1, defaultValue1);
583
584 const char *jsonString1 = "{\"intKey\": null}";
585 Json *json1 = cJSON_Parse(jsonString1);
586 root->Put("intKey", json1, true);
587 res1 = root->GetInt("intKey", defaultValue1);
588 EXPECT_EQ(res1, defaultValue1);
589 res1 = root->GetUInt("intKey", defaultValue1);
590 EXPECT_EQ(res1, defaultValue1);
591
592 const char *jsonString11 = "{\"intKey\": \"test\"}";
593 Json *json11 = cJSON_Parse(jsonString11);
594 root->Put("intKey", json11, true);
595 res1 = root->GetInt("intKey", defaultValue1);
596 EXPECT_EQ(res1, defaultValue1);
597 res1 = root->GetUInt("intKey", defaultValue1);
598 EXPECT_EQ(res1, defaultValue1);
599
600 float defaultValue2 = 0.0f;
601 float res2 = root->GetFloat("test", defaultValue2);
602 EXPECT_EQ(res2, defaultValue2);
603
604 const char *jsonString2 = "{\"floatKey\": null}";
605 Json *json2 = cJSON_Parse(jsonString2);
606 root->Put("floatKey", json2, true);
607 res2 = root->GetFloat("floatKey", defaultValue2);
608 EXPECT_EQ(res2, defaultValue2);
609
610 const char *jsonString22 = "{\"floatKey\": \"test\"}";
611 Json *json22 = cJSON_Parse(jsonString22);
612 root->Put("floatKey", json22, true);
613 res2 = root->GetFloat("floatKey", defaultValue2);
614 EXPECT_EQ(res2, defaultValue2);
615 }
616
617 HWTEST_F(TestUtils, JsonHelper003, TestSize.Level1) {
618 EffectJsonPtr root = EffectJsonHelper::CreateObject();
619 double defaultValue3 = 0.0;
620 double res3 = root->GetDouble("test", defaultValue3);
621 EXPECT_EQ(res3, defaultValue3);
622
623 const char *jsonString3 = "{\"doubleKey\": null}";
624 Json *json3 = cJSON_Parse(jsonString3);
625 root->Put("doubleKey", json3, true);
626 res3 = root->GetDouble("doubleKey", defaultValue3);
627 EXPECT_EQ(res3, defaultValue3);
628
629 const char *jsonString33 = "{\"doubleKey\": \"test\"}";
630 Json *json33 = cJSON_Parse(jsonString33);
631 root->Put("floatKey", json33, true);
632 res3 = root->GetDouble("doubleKey", defaultValue3);
633 EXPECT_EQ(res3, defaultValue3);
634
635 bool defaultValue4 = true;
636 bool res4 = root->GetBool("test", defaultValue4);
637 EXPECT_EQ(res4, defaultValue4);
638
639 const char *jsonString4 = "{\"boolKey\": null}";
640 Json *json4 = cJSON_Parse(jsonString4);
641 root->Put("boolKey", json4, true);
642 res4 = root->GetBool("boolKey", defaultValue4);
643 EXPECT_EQ(res4, defaultValue4);
644
645 const char *jsonString44 = "{\"boolKey\": \"test\"}";
646 Json *json44 = cJSON_Parse(jsonString44);
647 root->Put("boolKey", json44, true);
648 res4 = root->GetBool("boolKey", defaultValue4);
649 EXPECT_EQ(res4, defaultValue4);
650
651 std::string defaultValue5 = "string";
652 std::string res5 = root->GetString("test", defaultValue5);
653 EXPECT_EQ(res5, defaultValue5);
654
655 const char *jsonString5 = "{\"stringKey\": null}";
656 Json *json5 = cJSON_Parse(jsonString5);
657 root->Put("stringKey", json5, true);
658 res5 = root->GetString("stringKey", defaultValue5);
659 EXPECT_EQ(res5, defaultValue5);
660
661 const char *jsonString55 = "{\"stringKey\": 1}";
662 Json *json55 = cJSON_Parse(jsonString55);
663 root->Put("stringKey", json55, true);
664 res5 = root->GetString("stringKey", defaultValue5);
665 EXPECT_EQ(res5, defaultValue5);
666 }
667
668 HWTEST_F(TestUtils, JsonHelper004, TestSize.Level1) {
669 EffectJsonPtr root = EffectJsonHelper::CreateObject();
670 std::vector<EffectJsonPtr> defaultValue6 = {};
671
672 std::vector<EffectJsonPtr> res6 = root->GetArray("test");
673 EXPECT_EQ(res6, defaultValue6);
674
675 const char *jsonString6 = "{\"arrayKey\": null}";
676 Json *json6 = cJSON_Parse(jsonString6);
677 root->Put("arrayKey", json6, true);
678 res6 = root->GetArray("arrayKey");
679 EXPECT_EQ(res6, defaultValue6);
680
681 const char *jsonString66 = "{\"arrayKey\": 1}";
682 Json *json66 = cJSON_Parse(jsonString66);
683 root->Put("arrayKey", json66, true);
684 res6 = root->GetArray("arrayKey");
685 EXPECT_EQ(res6, defaultValue6);
686
687 EffectJsonPtr root2 = EffectJsonHelper::CreateObject((true));
688 root2->json_ = nullptr;
689 std::string s = root2->ToString();
690 }
691
692 HWTEST_F(TestUtils, CommonUtilsParsePicture001, TestSize.Level1)
693 {
694 static std::unique_ptr<Picture> g_picture = CreatePictureByPath(TEST_INCLUDE_AUX_PATH);
695 EXPECT_NE(g_picture, nullptr);
696 Picture *testPicture = g_picture.get();
697 std::shared_ptr<EffectBuffer> testEffectBuffer = CreateEffectBufferByPicture(testPicture);
698 EXPECT_NE(testEffectBuffer, nullptr);
699 ErrorCode result = CommonUtils::ParsePicture(testPicture, testEffectBuffer);
700
701 EXPECT_EQ(result, ErrorCode::SUCCESS);
702 EXPECT_EQ(testEffectBuffer->auxiliaryBufferInfos->size(), 4);
703 EXPECT_EQ(testEffectBuffer->bufferInfo_->pixelmapType_, EffectPixelmapType::PRIMARY);
704 EXPECT_EQ(testEffectBuffer->bufferInfo_->bufferType_, testEffectBuffer->extraInfo_->bufferType);
705 EXPECT_EQ(testEffectBuffer->bufferInfo_->addr_, testEffectBuffer->buffer_);
706 EXPECT_EQ(testEffectBuffer->extraInfo_->dataType, DataType::PICTURE);
707 EXPECT_EQ(testEffectBuffer->extraInfo_->picture, testPicture);
708 EXPECT_EQ(testEffectBuffer->bufferInfo_->hdrFormat_, HdrFormat::HDR8_GAINMAP);
709
710 std::shared_ptr<PixelMap> gainMap =
711 testPicture->GetAuxiliaryPicture(AuxiliaryPictureType::GAINMAP)->GetContentPixel();
712 BufferType bufferType = CommonUtils::SwitchToEffectBuffType(gainMap.get()->GetAllocatorType());
713 EXPECT_EQ(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::GAINMAP)->second->pixelmapType_,
714 EffectPixelmapType::GAINMAP);
715 EXPECT_EQ(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::GAINMAP)->second->bufferType_,
716 bufferType);
717 EXPECT_NE(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::GAINMAP)->second->addr_, nullptr);
718
719 std::shared_ptr<PixelMap> unRefocusMap =
720 testPicture->GetAuxiliaryPicture(AuxiliaryPictureType::UNREFOCUS_MAP)->GetContentPixel();
721 bufferType = CommonUtils::SwitchToEffectBuffType(unRefocusMap.get()->GetAllocatorType());
722 EXPECT_EQ(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::UNREFOCUS)->second->pixelmapType_,
723 EffectPixelmapType::UNREFOCUS);
724 EXPECT_EQ(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::UNREFOCUS)->second->bufferType_,
725 bufferType);
726 EXPECT_NE(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::UNREFOCUS)->second->addr_, nullptr);
727
728 std::shared_ptr<PixelMap> depthMap =
729 testPicture->GetAuxiliaryPicture(AuxiliaryPictureType::DEPTH_MAP)->GetContentPixel();
730 bufferType = CommonUtils::SwitchToEffectBuffType(depthMap.get()->GetAllocatorType());
731 EXPECT_EQ(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::DEPTHMAP)->second->pixelmapType_,
732 EffectPixelmapType::DEPTHMAP);
733 EXPECT_EQ(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::DEPTHMAP)->second->bufferType_,
734 bufferType);
735 EXPECT_NE(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::DEPTHMAP)->second->addr_, nullptr);
736
737 std::shared_ptr<PixelMap> linearMap =
738 testPicture->GetAuxiliaryPicture(AuxiliaryPictureType::LINEAR_MAP)->GetContentPixel();
739 bufferType = CommonUtils::SwitchToEffectBuffType(linearMap.get()->GetAllocatorType());
740 EXPECT_EQ(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::LINEAR)->second->pixelmapType_,
741 EffectPixelmapType::LINEAR);
742 EXPECT_EQ(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::LINEAR)->second->bufferType_,
743 bufferType);
744 EXPECT_NE(testEffectBuffer->auxiliaryBufferInfos->find(EffectPixelmapType::LINEAR)->second->addr_, nullptr);
745 }
746
747 HWTEST_F(TestUtils, CommonUtilsParsePicture002, TestSize.Level1)
748 {
749 static std::unique_ptr<Picture> g_picture = CreatePictureByPath(TEST_IMAGE_PATH);
750 EXPECT_NE(g_picture, nullptr);
751 std::shared_ptr<EffectBuffer> testEffectBuffer = CreateEffectBufferByPicture(g_picture.get());
752 ErrorCode result = CommonUtils::ParsePicture(g_picture.get(), testEffectBuffer);
753 EXPECT_EQ(result, ErrorCode::SUCCESS);
754 }
755 }
756 }
757 }
758 }
759