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 "avsession_pixel_map_adapter.h"
19 #include "avsession_log.h"
20 #include "securec.h"
21
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace AVSession {
26 class AVSessionPixelMapAdapterTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp() override;
31 void TearDown() override;
32 };
33
SetUpTestCase()34 void AVSessionPixelMapAdapterTest::SetUpTestCase()
35 {}
36
TearDownTestCase()37 void AVSessionPixelMapAdapterTest::TearDownTestCase()
38 {}
39
SetUp()40 void AVSessionPixelMapAdapterTest::SetUp()
41 {}
42
TearDown()43 void AVSessionPixelMapAdapterTest::TearDown()
44 {}
45
46 /**
47 * @tc.name: OnPlaybackStateChange001
48 * @tc.desc: test OnPlaybackStateChange
49 * @tc.type: FUNC
50 * @tc.require:
51 */
52 HWTEST_F(AVSessionPixelMapAdapterTest, OnPlaybackStateChange001, TestSize.Level1)
53 {
54 std::shared_ptr<AVSessionPixelMap> ptr = std::make_shared<AVSessionPixelMap>();
55 std::vector<uint8_t> vec = {255, 255};
56 ptr->SetInnerImgBuffer(vec);
57 AVSessionPixelMapAdapter avsessionPixelMapAdapter;
58 auto p = avsessionPixelMapAdapter.ConvertFromInner(ptr);
59 EXPECT_EQ(p, nullptr);
60 }
61
62 /**
63 * @tc.name: OnPlaybackStateChange002
64 * @tc.desc: test OnPlaybackStateChange
65 * @tc.type: FUNC
66 * @tc.require:
67 */
68 HWTEST_F(AVSessionPixelMapAdapterTest, OnPlaybackStateChange002, TestSize.Level1)
69 {
70 std::shared_ptr<AVSessionPixelMap> ptr = std::make_shared<AVSessionPixelMap>();
71 std::vector<uint8_t> vec = {0, 0};
72 ptr->SetInnerImgBuffer(vec);
73 AVSessionPixelMapAdapter avsessionPixelMapAdapter;
74 auto p = avsessionPixelMapAdapter.ConvertFromInner(ptr);
75 EXPECT_EQ(p, nullptr);
76 }
77
78 /**
79 * @tc.name: CopyPixMapToDst001
80 * @tc.desc: test CopyPixMapToDst
81 * @tc.type: FUNC
82 * @tc.require:
83 */
84 HWTEST_F(AVSessionPixelMapAdapterTest, CopyPixMapToDst001, TestSize.Level1)
85 {
86 Media::PixelMap pixelMap;
87 pixelMap.SetPixelsAddr(nullptr, nullptr, 10, Media::AllocatorType::DMA_ALLOC, nullptr);
88 Media::PixelMap source;
89 AVSessionPixelMapAdapter avsessionPixelMapAdapter;
90 bool result = avsessionPixelMapAdapter.CopyPixMapToDst(source, nullptr, 10);
91 EXPECT_EQ(result, false);
92 }
93
94 /**
95 * @tc.name: AVSessionPixelMapAdapterTest_CleanAVSessionPixelMap_001
96 * @tc.desc: Test CleanAVSessionPixelMap with a non-null innerPixelMap.
97 * @tc.type: FUNC
98 * @tc.require: #I5Y4MZ
99 */
100 static HWTEST_F(AVSessionPixelMapAdapterTest, CleanAVSessionPixelMap_001, TestSize.Level1)
101 {
102 SLOGD("AVSessionPixelMapAdapterTest_CleanAVSessionPixelMap_001 begin!");
103 std::shared_ptr<AVSessionPixelMap> innerPixelMap = std::make_shared<AVSessionPixelMap>();
104 std::vector<uint8_t> imgBuffer = {1, 2, 3, 4, 5};
105 innerPixelMap->SetInnerImgBuffer(imgBuffer);
106 AVSessionPixelMapAdapter adapter;
107 adapter.CleanAVSessionPixelMap(innerPixelMap);
108 EXPECT_TRUE(innerPixelMap->GetInnerImgBuffer().empty());
109 SLOGD("AVSessionPixelMapAdapterTest_CleanAVSessionPixelMap_001 end!");
110 }
111
112
113 /**
114 * @tc.name: CopyPixMapToDst_001
115 * @tc.desc: Test CopyPixMapToDst with DMA allocator but no copy failure.
116 * @tc.type: FUNC
117 * @tc.require: #I5Y4MZ
118 */
119 HWTEST_F(AVSessionPixelMapAdapterTest, CopyPixMapToDst_001, TestSize.Level1)
120 {
121 SLOGD("CopyPixMapToDst_001 begin!");
122 OHOS::Media::PixelMap source;
123 OHOS::Media::ImageInfo imageInfo = {
124 .size = {10, 10},
125 .pixelFormat = OHOS::Media::PixelFormat::ARGB_8888,
126 .colorSpace = OHOS::Media::ColorSpace::SRGB,
127 .alphaType = OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN,
128 .baseDensity = 0,
129 .encodedFormat = ""
130 };
131 source.SetImageInfo(imageInfo);
132 source.SetPixelsAddr(nullptr, nullptr, 10 * 10 * 4, OHOS::Media::AllocatorType::DMA_ALLOC, nullptr);
133 uint32_t bufferSize = source.GetRowBytes() * source.GetHeight();
134 std::vector<uint8_t> dstPixels(bufferSize);
135 bool result = AVSessionPixelMapAdapter::CopyPixMapToDst(source, dstPixels.data(), bufferSize);
136 EXPECT_FALSE(result);
137 SLOGD("CopyPixMapToDst_001 end!");
138 }
139
140 /**
141 * @tc.name: ConvertToInner_001
142 * @tc.desc: Test ConvertToInner with CopyPixMapToDst failure due to insufficient buffer size.
143 * @tc.type: FUNC
144 * @tc.require: #I5Y4MZ
145 */
146 HWTEST_F(AVSessionPixelMapAdapterTest, ConvertToInner_001, TestSize.Level1)
147 {
148 SLOGD("ConvertToInner_001 begin!");
149 OHOS::Media::PixelMap source;
150 OHOS::Media::ImageInfo imageInfo = {
151 .size = {10, 10},
152 .pixelFormat = OHOS::Media::PixelFormat::ARGB_8888,
153 .colorSpace = OHOS::Media::ColorSpace::SRGB,
154 .alphaType = OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN,
155 .baseDensity = 0,
156 .encodedFormat = ""
157 };
158 source.SetImageInfo(imageInfo);
159 source.SetPixelsAddr(nullptr, nullptr, 10 * 10 * 4, OHOS::Media::AllocatorType::DMA_ALLOC, nullptr);
160 std::shared_ptr<OHOS::Media::PixelMap> pixelMap = std::make_shared<OHOS::Media::PixelMap>(source);
161 std::shared_ptr<AVSessionPixelMap> result = AVSessionPixelMapAdapter::ConvertToInner(pixelMap);
162 EXPECT_EQ(result, nullptr);
163 SLOGD("ConvertToInner_001 end!");
164 }
165
166 /**
167 * @tc.name: ConvertToInnerWithMinSize_001
168 * @tc.desc: Test ConvertToInnerWithMinSize with CopyPixMapToDst failure.
169 * @tc.type: FUNC
170 * @tc.require: #I5Y4MZ
171 */
172 HWTEST_F(AVSessionPixelMapAdapterTest, ConvertToInnerWithMinSize_001, TestSize.Level1)
173 {
174 SLOGD("ConvertToInnerWithMinSize_001 begin!");
175 OHOS::Media::PixelMap source;
176 OHOS::Media::ImageInfo imageInfo = {
177 .size = {10, 10},
178 .pixelFormat = OHOS::Media::PixelFormat::ARGB_8888,
179 .colorSpace = OHOS::Media::ColorSpace::SRGB,
180 .alphaType = OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN,
181 .baseDensity = 0,
182 .encodedFormat = ""
183 };
184 source.SetImageInfo(imageInfo);
185 source.SetPixelsAddr(nullptr, nullptr, 10 * 10 * 4, OHOS::Media::AllocatorType::DMA_ALLOC, nullptr);
186 std::shared_ptr<OHOS::Media::PixelMap> pixelMap = std::make_shared<OHOS::Media::PixelMap>(source);
187 std::shared_ptr<AVSessionPixelMap> result = AVSessionPixelMapAdapter::ConvertToInnerWithMinSize(pixelMap);
188 EXPECT_EQ(result, nullptr);
189 SLOGD("ConvertToInnerWithMinSize_001 end!");
190 }
191
192 /**
193 * @tc.name: ConvertToInnerWithLimitedSize_001
194 * @tc.desc: Test ConvertToInnerWithLimitedSize with CopyPixMapToDst failure.
195 * @tc.type: FUNC
196 * @tc.require: #I5Y4MZ
197 */
198 HWTEST_F(AVSessionPixelMapAdapterTest, ConvertToInnerWithLimitedSize_001, TestSize.Level1)
199 {
200 SLOGD("ConvertToInnerWithLimitedSize_001 begin!");
201 OHOS::Media::PixelMap source;
202 OHOS::Media::ImageInfo imageInfo = {
203 .size = {10, 10},
204 .pixelFormat = OHOS::Media::PixelFormat::ARGB_8888,
205 .colorSpace = OHOS::Media::ColorSpace::SRGB,
206 .alphaType = OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN,
207 .baseDensity = 0,
208 .encodedFormat = ""
209 };
210 source.SetImageInfo(imageInfo);
211 source.SetPixelsAddr(nullptr, nullptr, 10 * 10 * 4, OHOS::Media::AllocatorType::DMA_ALLOC, nullptr);
212 std::shared_ptr<OHOS::Media::PixelMap> pixelMap = std::make_shared<OHOS::Media::PixelMap>(source);
213 std::shared_ptr<AVSessionPixelMap> result = AVSessionPixelMapAdapter::ConvertToInnerWithLimitedSize(pixelMap);
214 EXPECT_EQ(result, nullptr);
215 SLOGD("ConvertToInnerWithLimitedSize_001 end!");
216 }
217 } // namespace AVSESSION
218 } // namespace OHOS