• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "avmetadatahelper_impl_unit_test.h"
17 
18 #include "buffer/avbuffer_common.h"
19 #include "common/media_source.h"
20 #include "ibuffer_consumer_listener.h"
21 #include "graphic_common_c.h"
22 #include "media_errors.h"
23 #include "media_log.h"
24 #include "media_description.h"
25 #include "meta/meta.h"
26 #include "meta/meta_key.h"
27 #include "plugin/plugin_time.h"
28 #include "sync_fence.h"
29 #include "uri_helper.h"
30 #include "media_client.h"
31 
32 #include "v1_0/cm_color_space.h"
33 #include "v1_0/hdr_static_metadata.h"
34 #include "v1_0/buffer_handle_meta_key_type.h"
35 
36 using namespace OHOS;
37 using namespace OHOS::Media;
38 using namespace std;
39 using namespace testing::ext;
40 
41 namespace OHOS {
42 namespace Media {
43 
44 static MediaClient g_mediaClientInstance;
GetInstance()45 IMediaService &MediaServiceFactory::GetInstance()
46 {
47     return g_mediaClientInstance;
48 }
49 
MediaClient()50 MediaClient::MediaClient() noexcept {}
51 
~MediaClient()52 MediaClient::~MediaClient() {}
53 
CreateAVMetadataHelperService()54 std::shared_ptr<IAVMetadataHelperService> MediaClient::CreateAVMetadataHelperService()
55 {
56     return nullptr;
57 }
58 
DestroyAVMetadataHelperService(std::shared_ptr<IAVMetadataHelperService> avMetadataHelper)59 int32_t MediaClient::DestroyAVMetadataHelperService(std::shared_ptr<IAVMetadataHelperService> avMetadataHelper)
60 {
61     return 0;
62 }
63 
GetMonitorProxy()64 sptr<IStandardMonitorService> MediaClient::GetMonitorProxy()
65 {
66     return nullptr;
67 }
68 
69 namespace Test {
SetUpTestCase(void)70 void AVMetadtahelperImplUnitTest::SetUpTestCase(void) {}
71 
TearDownTestCase(void)72 void AVMetadtahelperImplUnitTest::TearDownTestCase(void) {}
73 
SetUp(void)74 void AVMetadtahelperImplUnitTest::SetUp(void)
75 {
76     helper_->ReportSceneCode(AV_META_SCENE_NORMAL);
77     helper_->ReportSceneCode(AV_META_SCENE_CLONE);
78     helper_ = std::make_shared<AVMetadataHelperImpl>();
79 }
80 
TearDown(void)81 void AVMetadtahelperImplUnitTest::TearDown(void)
82 {
83     helper_ = nullptr;
84 }
85 
86 /**
87  * @tc.name: FreePixelMapData
88  * @tc.desc: FreePixelMapData
89  * @tc.type: FUNC
90  */
91 HWTEST_F(AVMetadtahelperImplUnitTest, FreePixelMapData, TestSize.Level1)
92 {
93     PixelMapMemHolder *holder = new PixelMapMemHolder{ .isShmem = true, .shmem = nullptr, .heap = new uint8_t(0) };
94     FreePixelMapData(nullptr, reinterpret_cast<void *>(holder), 0);
95     ASSERT_EQ(holder->heap, nullptr);
96 
97     auto mem = AVSharedMemoryBase::CreateFromLocal(10, AVSharedMemory::Flags::FLAGS_READ_WRITE, "test");
98     ASSERT_NE(mem, nullptr);
99     holder = new PixelMapMemHolder{ .isShmem = true, .shmem = mem, .heap = nullptr };
100     FreePixelMapData(nullptr, reinterpret_cast<void *>(holder), 0);
101     ASSERT_EQ(holder->shmem, nullptr);
102 
103     mem = AVSharedMemoryBase::CreateFromLocal(10, AVSharedMemory::Flags::FLAGS_READ_WRITE, "test");
104     ASSERT_NE(mem, nullptr);
105     int32_t *tmpPtr = new int32_t(0);
106     holder = new PixelMapMemHolder{ .isShmem = false, .shmem = mem, .heap = reinterpret_cast<uint8_t *>(tmpPtr) };
107     FreePixelMapData(reinterpret_cast<void *>(tmpPtr), reinterpret_cast<void *>(holder), 0);
108     ASSERT_NE(holder->shmem, nullptr);
109     tmpPtr = nullptr;
110 
111     mem = AVSharedMemoryBase::CreateFromLocal(10, AVSharedMemory::Flags::FLAGS_READ_WRITE, "test");
112     ASSERT_NE(mem, nullptr);
113     tmpPtr = new int32_t(0);
114     holder = new PixelMapMemHolder{ .isShmem = false, .shmem = mem, .heap = nullptr };
115     FreePixelMapData(reinterpret_cast<void *>(tmpPtr), reinterpret_cast<void *>(holder), 0);
116     ASSERT_NE(holder->shmem, nullptr);
117     tmpPtr = nullptr;
118 
119     mem = AVSharedMemoryBase::CreateFromLocal(10, AVSharedMemory::Flags::FLAGS_READ_WRITE, "test");
120     ASSERT_NE(mem, nullptr);
121     tmpPtr = new int32_t(0);
122     holder = new PixelMapMemHolder{ .isShmem = false, .shmem = mem, .heap = reinterpret_cast<uint8_t *>(tmpPtr) };
123     FreePixelMapData(nullptr, reinterpret_cast<void *>(holder), 0);
124     ASSERT_NE(holder->shmem, nullptr);
125     tmpPtr = nullptr;
126 
127     mem = AVSharedMemoryBase::CreateFromLocal(10, AVSharedMemory::Flags::FLAGS_READ_WRITE, "test");
128     ASSERT_NE(mem, nullptr);
129     tmpPtr = new int32_t(0);
130     holder = new PixelMapMemHolder{ .isShmem = false, .shmem = mem, .heap = nullptr };
131     FreePixelMapData(nullptr, reinterpret_cast<void *>(holder), 0);
132     ASSERT_NE(holder->shmem, nullptr);
133     tmpPtr = nullptr;
134 }
135 
136 /**
137  * @tc.name: SaveDataToFile
138  * @tc.desc: SaveDataToFile
139  * @tc.type: FUNC
140  */
141 HWTEST_F(AVMetadtahelperImplUnitTest, SaveDataToFile, TestSize.Level1)
142 {
143     const char *tmpPtr = new char(0);
144 
145     auto res = helper_->SaveDataToFile("testFile.data", tmpPtr, sizeof(int32_t));
146     delete tmpPtr;
147     ASSERT_NE(res, MSERR_OK);
148 }
149 
150 /**
151  * @tc.name: DumpPixelMap
152  * @tc.desc: DumpPixelMap
153  * @tc.type: FUNC
154  */
155 HWTEST_F(AVMetadtahelperImplUnitTest, DumpPixelMap, TestSize.Level1)
156 {
157     auto res = helper_->DumpPixelMap(true, nullptr, ".data");
158     ASSERT_EQ(res, MSERR_INVALID_VAL);
159 
160     InitializationOptions opts;
161     opts.size.width = 1;
162     opts.size.height = 1;
163     opts.pixelFormat = PixelFormat::NV12;
164     std::shared_ptr<PixelMap> pixelMap = PixelMap::Create(opts);
165     ASSERT_NE(pixelMap, nullptr);
166     res = helper_->DumpPixelMap(true, pixelMap, ".data");
167     ASSERT_NE(res, MSERR_OK);
168 }
169 
170 /**
171  * @tc.name: DumpAVBuffer
172  * @tc.desc: DumpAVBuffer
173  * @tc.type: FUNC
174  */
175 HWTEST_F(AVMetadtahelperImplUnitTest, DumpAVBuffer, TestSize.Level1)
176 {
177     auto res = helper_->DumpAVBuffer(true, nullptr, ".data");
178     ASSERT_EQ(res, MSERR_INVALID_VAL);
179 }
180 
181 /**
182  * @tc.name: pixelFormatToString
183  * @tc.desc: pixelFormatToString
184  * @tc.type: FUNC
185  */
186 HWTEST_F(AVMetadtahelperImplUnitTest, pixelFormatToString, TestSize.Level1)
187 {
188     auto res = helper_->pixelFormatToString(PixelFormat::RGB_565);
189     ASSERT_EQ(res, "RGB_565");
190 
191     res = helper_->pixelFormatToString(PixelFormat::RGBA_8888);
192     ASSERT_EQ(res, "RGBA_8888");
193 
194     res = helper_->pixelFormatToString(PixelFormat::RGB_888);
195     ASSERT_EQ(res, "RGB_888");
196 
197     res = helper_->pixelFormatToString(PixelFormat::NV12);
198     ASSERT_EQ(res, "NV12");
199 
200     res = helper_->pixelFormatToString(PixelFormat::YCBCR_P010);
201     ASSERT_EQ(res, "YCBCR_P010");
202 
203     res = helper_->pixelFormatToString(PixelFormat::UNKNOWN);
204     ASSERT_EQ(res, "UNKNOWN");
205 }
206 
207 /**
208  * @tc.name: SetPixelMapYuvInfo
209  * @tc.desc: SetPixelMapYuvInfo
210  * @tc.type: FUNC
211  */
212 HWTEST_F(AVMetadtahelperImplUnitTest, SetPixelMapYuvInfo, TestSize.Level1)
213 {
214     InitializationOptions opts;
215     opts.size.width = 1;
216     opts.size.height = 1;
217     opts.pixelFormat = PixelFormat::NV12;
218     std::shared_ptr<PixelMap> pixelMap = PixelMap::Create(opts);
219     ASSERT_NE(pixelMap, nullptr);
220 
221     AVMetadataHelperImpl::PixelMapInfo pixelMapInfo = { .pixelFormat = PixelFormat::NV12 };
222     sptr<SurfaceBuffer> buffer = nullptr;
223     helper_->SetPixelMapYuvInfo(buffer, pixelMap, pixelMapInfo, false);
224     YUVDataInfo yuvInfo;
225     pixelMap->GetImageYUVInfo(yuvInfo);
226     ASSERT_EQ(yuvInfo.yWidth, 1);
227 }
228 }  // namespace Test
229 }  // namespace Media
230 }  // namespace OHOS