1 /*
2 * Copyright (C) 2025 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 "media_errors.h"
17 #include "avmetadata_unittest.h"
18
19
20 namespace OHOS {
21 namespace Media {
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 using namespace testing;
26 using namespace testing::ext;
SetUpTestCase(void)27 void AVMetadataUnittest::SetUpTestCase(void)
28 {
29 }
30
TearDownTestCase(void)31 void AVMetadataUnittest::TearDownTestCase(void)
32 {
33 }
34
SetUp(void)35 void AVMetadataUnittest::SetUp(void)
36 {
37 avmetadataPtr_ = std::make_shared<AVMetadataHelperImpl>();
38 }
39
TearDown(void)40 void AVMetadataUnittest::TearDown(void)
41 {
42 avmetadataPtr_ = nullptr;
43 }
44
45 /**
46 * @tc.number : DumpPixelMap_001
47 * @tc.name : DumpPixelMap
48 * @tc.desc : Test pixelMap->GetAllocatorType() == AllocatorType::DMA_ALLOC
49 */
50 HWTEST_F(AVMetadataUnittest, DumpPixelMap_001, TestSize.Level0)
51 {
52 ASSERT_NE(avmetadataPtr_, nullptr);
53 bool isDump = true;
54 auto mockPixelMap = std::make_shared<MockPixelMap>();
55 EXPECT_CALL(*mockPixelMap, GetWidth()).WillRepeatedly(Return(0));
56 EXPECT_CALL(*mockPixelMap, GetHeight()).WillRepeatedly(Return(0));
57 EXPECT_CALL(*mockPixelMap, GetPixelFormat()).WillRepeatedly(Return(PixelFormat::YCBCR_P010));
58 EXPECT_CALL(*mockPixelMap, GetAllocatorType()).WillRepeatedly(Return(AllocatorType::DMA_ALLOC));
59 EXPECT_CALL(*mockPixelMap, GetFd()).WillRepeatedly(Return(nullptr));
60 std::shared_ptr<PixelMap> pixelMap = mockPixelMap;
61 std::string fileNameSuffix;
62 avmetadataPtr_->DumpPixelMap(isDump, pixelMap, fileNameSuffix);
63 EXPECT_NE(avmetadataPtr_, nullptr);
64 }
65
66 /**
67 * @tc.number : DumpAVBuffer_001
68 * @tc.name : DumpAVBuffer
69 * @tc.desc : Test surfaceBuffer == nullptr
70 */
71 HWTEST_F(AVMetadataUnittest, DumpAVBuffer_001, TestSize.Level0)
72 {
73 ASSERT_NE(avmetadataPtr_, nullptr);
74 bool isDump = true;
75 auto mockBuffer = std::make_shared<MockAVBuffer>();
76 EXPECT_CALL(*mockBuffer, GetUniqueId()).WillRepeatedly(Return(1));
77 mockBuffer->meta_ = std::make_shared<Meta>();
78 auto mockMemory = std::make_shared<MockAVMemory>();
79 EXPECT_CALL(*mockMemory, GetSize()).WillRepeatedly(Return(1));
80 uint8_t* test = nullptr;
81 EXPECT_CALL(*mockMemory, GetAddr()).WillRepeatedly(Return(test));
82 EXPECT_CALL(*mockMemory, GetSurfaceBuffer()).WillRepeatedly(Return(nullptr));
83 mockBuffer->memory_ = mockMemory;
84 std::shared_ptr<AVBuffer> frameBuffer = mockBuffer;
85 std::string fileNameSuffix;
86 auto ret = avmetadataPtr_->DumpAVBuffer(isDump, frameBuffer, fileNameSuffix);
87 EXPECT_NE(ret, 0);
88 }
89
90 /**
91 * @tc.number : DumpAVBuffer_002
92 * @tc.name : DumpAVBuffer
93 * @tc.desc : Test surfaceBuffer != nullptr
94 */
95 HWTEST_F(AVMetadataUnittest, DumpAVBuffer_002, TestSize.Level0)
96 {
97 ASSERT_NE(avmetadataPtr_, nullptr);
98 auto mockSurfaceBuffer = new MockSurfaceBuffer();
99 EXPECT_CALL(*(mockSurfaceBuffer), GetSize()).WillRepeatedly(Return(0));
100 EXPECT_CALL(*(mockSurfaceBuffer), GetVirAddr()).WillRepeatedly(Return(nullptr));
101 sptr<SurfaceBuffer> surfaceBuffer(mockSurfaceBuffer);
102 bool isDump = true;
103 auto mockBuffer = std::make_shared<MockAVBuffer>();
104 EXPECT_CALL(*mockBuffer, GetUniqueId()).WillRepeatedly(Return(1));
105 mockBuffer->meta_ = std::make_shared<Meta>();
106 auto mockMemory = std::make_shared<MockAVMemory>();
107 EXPECT_CALL(*mockMemory, GetSize()).WillRepeatedly(Return(1));
108 uint8_t* test = nullptr;
109 EXPECT_CALL(*mockMemory, GetAddr()).WillRepeatedly(Return(test));
110 EXPECT_CALL(*mockMemory, GetSurfaceBuffer()).WillRepeatedly(Return(mockSurfaceBuffer));
111 mockBuffer->memory_ = mockMemory;
112 std::shared_ptr<AVBuffer> frameBuffer = mockBuffer;
113 std::string fileNameSuffix;
114 auto ret = avmetadataPtr_->DumpAVBuffer(isDump, frameBuffer, fileNameSuffix);
115 EXPECT_NE(mockMemory->GetSurfaceBuffer(), nullptr);
116 EXPECT_NE(ret, 0);
117 }
118
119 /**
120 * @tc.number : FormatColorSpaceInfo_001
121 * @tc.name : FormatColorSpaceInfo
122 * @tc.desc : Test all
123 */
124 HWTEST_F(AVMetadataUnittest, FormatColorSpaceInfo_001, TestSize.Level0)
125 {
126 ASSERT_NE(avmetadataPtr_, nullptr);
127 OHOS::HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceInfo colorSpaceInfo;
128 colorSpaceInfo.primaries =
129 OHOS::HDI::Display::Graphic::Common::V1_0::CM_ColorPrimaries::COLORPRIMARIES_P3_D65;
130 colorSpaceInfo.matrix = OHOS::HDI::Display::Graphic::Common::V1_0::CM_Matrix::MATRIX_BT601_P;
131 avmetadataPtr_->FormatColorSpaceInfo(colorSpaceInfo);
132 EXPECT_EQ(colorSpaceInfo.matrix, OHOS::HDI::Display::Graphic::Common::V1_0::CM_Matrix::MATRIX_P3);
133
134 colorSpaceInfo.primaries = OHOS::HDI::Display::Graphic::Common::V1_0::CM_ColorPrimaries::COLORPRIMARIES_BT601_P;
135 colorSpaceInfo.matrix = OHOS::HDI::Display::Graphic::Common::V1_0::CM_Matrix::MATRIX_BT601_N;
136 avmetadataPtr_->FormatColorSpaceInfo(colorSpaceInfo);
137 EXPECT_EQ(colorSpaceInfo.matrix, OHOS::HDI::Display::Graphic::Common::V1_0::CM_Matrix::MATRIX_BT601_P);
138 }
139
140 /**
141 * @tc.number : SetPixelMapYuvInfo_001
142 * @tc.name : SetPixelMapYuvInfo
143 * @tc.desc : Test retVal != OHOS::GSERROR_OK || planes == nullptr
144 * Test surfaceBuffer == nullptr || needModifyStride == false
145 */
146 HWTEST_F(AVMetadataUnittest, SetPixelMapYuvInfo_001, TestSize.Level0)
147 {
148 ASSERT_NE(avmetadataPtr_, nullptr);
149 auto mockSurfaceBuffer = new MockSurfaceBuffer();
150 EXPECT_CALL(*(mockSurfaceBuffer), GetPlanesInfo(_)).WillRepeatedly(Return(GSError::GSERROR_INVALID_ARGUMENTS));
151 sptr<SurfaceBuffer> surfaceBuffer(mockSurfaceBuffer);
152 auto mockPixelMap = std::make_shared<MockPixelMap>();
153 EXPECT_CALL(*mockPixelMap, GetWidth()).WillRepeatedly(Return(0));
154 EXPECT_CALL(*mockPixelMap, GetHeight()).WillRepeatedly(Return(0));
155 EXPECT_CALL(*mockPixelMap, SetImageYUVInfo(_)).WillRepeatedly(Return());
156 std::shared_ptr<PixelMap> pixelMap = mockPixelMap;
157 AVMetadataHelperImpl::PixelMapInfo pixelMapInfo;
158 pixelMapInfo.isHdr = true;
159 bool needModifyStride = true;
160 avmetadataPtr_->SetPixelMapYuvInfo(surfaceBuffer, pixelMap, pixelMapInfo, needModifyStride);
161 surfaceBuffer = nullptr;
162 avmetadataPtr_->SetPixelMapYuvInfo(surfaceBuffer, pixelMap, pixelMapInfo, needModifyStride);
163 EXPECT_EQ(mockPixelMap->GetWidth(), 0);
164 }
165
166 /**
167 * @tc.number : SetHelperCallback_001
168 * @tc.name : SetHelperCallback
169 * @tc.desc : Test all
170 */
171 HWTEST_F(AVMetadataUnittest, SetHelperCallback_001, TestSize.Level0)
172 {
173 ASSERT_NE(avmetadataPtr_, nullptr);
174 std::shared_ptr<HelperCallback> callback = nullptr;
175 avmetadataPtr_->avMetadataHelperService_ = nullptr;
176 auto ret = avmetadataPtr_->SetHelperCallback(callback);
177 EXPECT_NE(ret, 0);
178 }
179
180 /**
181 * @tc.number : ScalePixelMapByMode_001
182 * @tc.name : ScalePixelMapByMode
183 * @tc.desc : Test all
184 */
185 HWTEST_F(AVMetadataUnittest, ScalePixelMapByMode_001, TestSize.Level0)
186 {
187 ASSERT_NE(avmetadataPtr_, nullptr);
188 auto mockPixelMap = std::make_shared<MockPixelMap>();
189 EXPECT_CALL(*mockPixelMap, GetWidth()).WillRepeatedly(Return(0));
190 EXPECT_CALL(*mockPixelMap, GetHeight()).WillRepeatedly(Return(1));
191 EXPECT_CALL(*mockPixelMap, scale(_, _)).WillRepeatedly(Return());
192 std::shared_ptr<PixelMap> pixelMap = mockPixelMap;
193 AVMetadataHelperImpl::PixelMapInfo info;
194 PixelMapParams param;
195 int32_t scaleMode = 0;
196 avmetadataPtr_->ScalePixelMapByMode(pixelMap, info, param, scaleMode);
197 scaleMode = 1;
198 avmetadataPtr_->ScalePixelMapByMode(pixelMap, info, param, scaleMode);
199 scaleMode = 2;
200 avmetadataPtr_->ScalePixelMapByMode(pixelMap, info, param, scaleMode);
201 EXPECT_EQ(mockPixelMap->GetHeight(), 1);
202 }
203
204 /**
205 * @tc.number : CreatePixelMapYuv_001
206 * @tc.name : CreatePixelMapYuv
207 * @tc.desc : Test pixelMapInfo.pixelFormat == PixelFormat::UNKNOWN
208 * Test frameBuffer->memory_->GetSize() != 0
209 * && frameBuffer->memory_->GetSurfaceBuffer() == nullptr
210 */
211 HWTEST_F(AVMetadataUnittest, CreatePixelMapYuv_001, TestSize.Level0)
212 {
213 ASSERT_NE(avmetadataPtr_, nullptr);
214 auto mockBuffer = std::make_shared<MockAVBuffer>();
215 EXPECT_CALL(*mockBuffer, GetUniqueId()).WillRepeatedly(Return(1));
216 mockBuffer->meta_ = std::make_shared<Meta>();
217 auto mockMemory = std::make_shared<MockAVMemory>();
218 EXPECT_CALL(*mockMemory, GetSize()).WillRepeatedly(Return(1));
219 EXPECT_CALL(*mockMemory, GetSurfaceBuffer()).WillRepeatedly(Return(nullptr));
220 mockBuffer->memory_ = mockMemory;
221 std::shared_ptr<AVBuffer> frameBuffer = mockBuffer;
222 AVMetadataHelperImpl::PixelMapInfo pixelMapInfo;
223 pixelMapInfo.pixelFormat = PixelFormat::UNKNOWN;
224 auto ret = avmetadataPtr_->CreatePixelMapYuv(frameBuffer, pixelMapInfo);
225 EXPECT_EQ(ret, nullptr);
226 }
227 } // namespace Media
228 } // namespace OHOS
229