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 #define LOG_TAG "SystemDefinedPixelMapTest"
16
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <string>
20 #include <sstream>
21
22 #include "logger.h"
23 #include "pixelmap_loader.h"
24 #include "system_defined_pixelmap.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::UDMF;
28 using namespace OHOS;
29 namespace OHOS::Test {
30 using namespace std;
31 static constexpr const char* PIXEL_MAP_WIDTH = "width";
32 static constexpr const char* PIXEL_MAP_HEIGHT = "height";
33 static constexpr const char* PIXEL_MAP_FORMAT = "pixel-format";
34 static constexpr const char* PIXEL_MAP_ALPHA_TYPE = "alpha-type";
35 class SystemDefinedPixelMapTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp() override;
40 void TearDown() override;
41 };
42
SetUpTestCase()43 void SystemDefinedPixelMapTest::SetUpTestCase()
44 {
45 }
46
TearDownTestCase()47 void SystemDefinedPixelMapTest::TearDownTestCase()
48 {
49 }
50
SetUp()51 void SystemDefinedPixelMapTest::SetUp()
52 {
53 }
54
TearDown()55 void SystemDefinedPixelMapTest::TearDown()
56 {
57 }
58
59 /**
60 * @tc.name: SystemDefinedPixelMap001
61 * @tc.desc: Normal testcase of SystemDefinedPixelMap
62 * @tc.type: FUNC
63 */
64 HWTEST_F(SystemDefinedPixelMapTest, SystemDefinedPixelMap001, TestSize.Level1)
65 {
66 LOG_INFO(UDMF_TEST, "SystemDefinedPixelMap001 begin.");
67 std::vector<uint8_t> data = std::vector<uint8_t>();
68 SystemDefinedPixelMap systemDefinedPixelMap(data);
69 EXPECT_EQ(systemDefinedPixelMap.dataType_, SYSTEM_DEFINED_PIXEL_MAP);
70 EXPECT_EQ(systemDefinedPixelMap.rawData_, data);
71 LOG_INFO(UDMF_TEST, "SystemDefinedPixelMap001 end.");
72 }
73
74 /**
75 * @tc.name: SystemDefinedPixelMap002
76 * @tc.desc: Normal testcase of SystemDefinedPixelMap
77 * @tc.type: FUNC
78 */
79 HWTEST_F(SystemDefinedPixelMapTest, SystemDefinedPixelMap002, TestSize.Level1)
80 {
81 LOG_INFO(UDMF_TEST, "SystemDefinedPixelMap002 begin.");
82 UDType type = UDType::ENTITY;
83 ValueType value = std::vector<uint8_t>();
84 SystemDefinedPixelMap systemDefinedPixelMap(type, value);
85 EXPECT_EQ(systemDefinedPixelMap.dataType_, SYSTEM_DEFINED_PIXEL_MAP);
86 EXPECT_EQ(systemDefinedPixelMap.rawData_, std::get<std::vector<uint8_t>>(value));
87 LOG_INFO(UDMF_TEST, "SystemDefinedPixelMap002 end.");
88 }
89
90 /**
91 * @tc.name: SystemDefinedPixelMap003
92 * @tc.desc: Abnormal testcase of SystemDefinedPixelMap, because value is not of type std::vector<uint8_t>
93 * @tc.type: FUNC
94 */
95 HWTEST_F(SystemDefinedPixelMapTest, SystemDefinedPixelMap003, TestSize.Level1)
96 {
97 LOG_INFO(UDMF_TEST, "SystemDefinedPixelMap003 begin.");
98 UDType type = UDType::ENTITY;
99 ValueType value = 0;
100 SystemDefinedPixelMap systemDefinedPixelMap(type, value);
101 EXPECT_EQ(systemDefinedPixelMap.dataType_, SYSTEM_DEFINED_PIXEL_MAP);
102 EXPECT_EQ(systemDefinedPixelMap.rawData_.size(), 0);
103 LOG_INFO(UDMF_TEST, "SystemDefinedPixelMap003 end.");
104 }
105
106 /**
107 * @tc.name: GetPixelMapFromRawData001
108 * @tc.desc: Abnormal testcase of GetPixelMapFromRawData, rawData_ is null
109 * @tc.type: FUNC
110 */
111 HWTEST_F(SystemDefinedPixelMapTest, GetPixelMapFromRawData001, TestSize.Level1)
112 {
113 LOG_INFO(UDMF_TEST, "GetPixelMapFromRawData001 begin.");
114 SystemDefinedPixelMap systemDefinedPixelMap;
115 const std::vector<uint8_t> rawData;
116 systemDefinedPixelMap.SetRawData(rawData);
117 auto ret = systemDefinedPixelMap.GetPixelMapFromRawData();
118 EXPECT_EQ(ret, nullptr);
119 LOG_INFO(UDMF_TEST, "GetPixelMapFromRawData001 end.");
120 }
121
122 /**
123 * @tc.name: GetPixelMapFromRawData002
124 * @tc.desc: Abnormal testcase of GetPixelMapFromRawData, rawData_ is wrong
125 * @tc.type: FUNC
126 */
127 HWTEST_F(SystemDefinedPixelMapTest, GetPixelMapFromRawData002, TestSize.Level1)
128 {
129 LOG_INFO(UDMF_TEST, "GetPixelMapFromRawData002 begin.");
130
131 std::vector<uint8_t> rawData;
132 for (int i = 0; i < 4 * 1024 * 1024; i++) {
133 rawData.emplace_back(1);
134 }
135 SystemDefinedPixelMap systemDefinedPixelMap;
136 UDDetails details = {
137 {PIXEL_MAP_WIDTH, (int32_t)5},
138 {PIXEL_MAP_HEIGHT, (int32_t)7},
139 {PIXEL_MAP_FORMAT, (int32_t)Media::PixelFormat::ARGB_8888},
140 {PIXEL_MAP_ALPHA_TYPE, (int32_t)Media::PixelFormat::ARGB_8888}
141 };
142 systemDefinedPixelMap.SetRawData(rawData);
143 systemDefinedPixelMap.SetDetails(details);
144 auto ret = systemDefinedPixelMap.GetPixelMapFromRawData();
145 EXPECT_EQ(ret, nullptr);
146 LOG_INFO(UDMF_TEST, "GetPixelMapFromRawData002 end.");
147 }
148
149 /**
150 * @tc.name: GetPixelMapFromRawData003
151 * @tc.desc: Abnormal testcase of GetPixelMapFromRawData, rawData_ is null
152 * @tc.type: FUNC
153 */
154 HWTEST_F(SystemDefinedPixelMapTest, GetPixelMapFromRawData003, TestSize.Level1)
155 {
156 LOG_INFO(UDMF_TEST, "GetPixelMapFromRawData002 begin.");
157
158 uint32_t color[35] = { 3, 7, 9, 9, 7, 6 };
159 OHOS::Media::InitializationOptions opts = { { 5, 7 },
160 Media::PixelFormat::ARGB_8888, Media::PixelFormat::ARGB_8888 };
161 std::unique_ptr<OHOS::Media::PixelMap> pixelMap =
162 OHOS::Media::PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
163 auto length = pixelMap->GetByteCount();
164 std::vector<uint8_t> rawData;
165 rawData.resize(length);
166 pixelMap->ReadPixels(length, rawData.data());
167 SystemDefinedPixelMap systemDefinedPixelMap;
168 UDDetails details = {
169 {PIXEL_MAP_WIDTH, (int32_t)5},
170 {PIXEL_MAP_HEIGHT, (int32_t)7},
171 {PIXEL_MAP_FORMAT, (int32_t)Media::PixelFormat::ARGB_8888},
172 {PIXEL_MAP_ALPHA_TYPE, (int32_t)Media::PixelFormat::ARGB_8888}
173 };
174 systemDefinedPixelMap.SetRawData(rawData);
175 systemDefinedPixelMap.SetDetails(details);
176 auto getPixelMap = systemDefinedPixelMap.GetPixelMapFromRawData();
177 auto getLength = getPixelMap->GetByteCount();
178 EXPECT_EQ(length, getLength);
179 std::vector<uint8_t> getRawData;
180 getPixelMap->ReadPixels(length, getRawData.data());
181 for (int32_t i = 0; i < getLength; ++i) {
182 EXPECT_EQ(getRawData[i], rawData[i]);
183 }
184 LOG_INFO(UDMF_TEST, "GetPixelMapFromRawData003 end.");
185 }
186
187 /**
188 * @tc.name: ParseInfoFromPixelMap001
189 * @tc.desc: Abnormal testcase of ParseInfoFromPixelMap001, pixelMap is null
190 * @tc.type: FUNC
191 */
192 HWTEST_F(SystemDefinedPixelMapTest, ParseInfoFromPixelMap001, TestSize.Level1)
193 {
194 LOG_INFO(UDMF_TEST, "ParseInfoFromPixelMap001 begin.");
195 SystemDefinedPixelMap systemDefinedPixelMap;
196 systemDefinedPixelMap.ParseInfoFromPixelMap(nullptr);
197 EXPECT_TRUE(systemDefinedPixelMap.details_.empty());
198 LOG_INFO(UDMF_TEST, "ParseInfoFromPixelMap001 end.");
199 }
200
201 /**
202 * @tc.name: PixelMapLoader001
203 * @tc.desc: Abnormal testcase of PixelMapLoader001
204 * @tc.type: FUNC
205 */
206 HWTEST_F(SystemDefinedPixelMapTest, PixelMapLoader001, TestSize.Level1)
207 {
208 LOG_INFO(UDMF_TEST, "PixelMapLoader001 begin.");
209 PixelMapLoader loader;
210 loader.handler_ = nullptr;
211 std::vector<uint8_t> buff(10);
212 auto ret = loader.DecodeTlv(buff);
213 EXPECT_EQ(ret, nullptr);
214 LOG_INFO(UDMF_TEST, "PixelMapLoader001 end.");
215 }
216
217 /**
218 * @tc.name: PixelMapLoader002
219 * @tc.desc: Abnormal testcase of PixelMapLoader002
220 * @tc.type: FUNC
221 */
222 HWTEST_F(SystemDefinedPixelMapTest, PixelMapLoader002, TestSize.Level1)
223 {
224 LOG_INFO(UDMF_TEST, "PixelMapLoader002 begin.");
225 PixelMapLoader loader;
226 loader.handler_ = nullptr;
227 std::vector<uint8_t> buff;
228
229 uint32_t color[35] = { 3, 7, 9, 9, 7, 6 };
230 OHOS::Media::InitializationOptions opts = { { 5, 7 },
231 Media::PixelFormat::ARGB_8888, Media::PixelFormat::ARGB_8888 };
232 std::unique_ptr<OHOS::Media::PixelMap> pixelMap =
233 OHOS::Media::PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
234 std::shared_ptr<OHOS::Media::PixelMap> pixelMapIn = move(pixelMap);
235
236 auto ret = loader.EncodeTlv(pixelMapIn, buff);
237 EXPECT_EQ(ret, false);
238 LOG_INFO(UDMF_TEST, "PixelMapLoader002 end.");
239 }
240
241 /**
242 * @tc.name: PixelMapLoader003
243 * @tc.desc: Abnormal testcase of PixelMapLoader003
244 * @tc.type: FUNC
245 */
246 HWTEST_F(SystemDefinedPixelMapTest, PixelMapLoader003, TestSize.Level1)
247 {
248 LOG_INFO(UDMF_TEST, "PixelMapLoader003 begin.");
249 PixelMapLoader loader;
250 loader.handler_ = nullptr;
251
252 PixelMapDetails details;
253 auto buff = std::vector<uint8_t>(10);
254 details.rawData = std::ref(buff);
255 auto ret = loader.GetPixelMapFromRawData(details);
256 EXPECT_EQ(ret, nullptr);
257 LOG_INFO(UDMF_TEST, "PixelMapLoader003 end.");
258 }
259
260 /**
261 * @tc.name: PixelMapLoader004
262 * @tc.desc: Abnormal testcase of PixelMapLoader004
263 * @tc.type: FUNC
264 */
265 HWTEST_F(SystemDefinedPixelMapTest, PixelMapLoader004, TestSize.Level1)
266 {
267 LOG_INFO(UDMF_TEST, "PixelMapLoader004 begin.");
268 PixelMapLoader loader;
269 loader.handler_ = nullptr;
270
271 uint32_t color[35] = { 3, 7, 9, 9, 7, 6 };
272 OHOS::Media::InitializationOptions opts = { { 5, 7 },
273 Media::PixelFormat::ARGB_8888, Media::PixelFormat::ARGB_8888 };
274 std::unique_ptr<OHOS::Media::PixelMap> pixelMap =
275 OHOS::Media::PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
276 std::shared_ptr<OHOS::Media::PixelMap> pixelMapIn = move(pixelMap);
277 auto ret = loader.ParseInfoFromPixelMap(pixelMapIn);
278 EXPECT_EQ(ret, nullptr);
279 LOG_INFO(UDMF_TEST, "PixelMapLoader004 end.");
280 }
281
282 /**
283 * @tc.name: PixelMapLoader005
284 * @tc.desc: Abnormal testcase of PixelMapLoader005
285 * @tc.type: FUNC
286 */
287 HWTEST_F(SystemDefinedPixelMapTest, PixelMapLoader005, TestSize.Level1)
288 {
289 LOG_INFO(UDMF_TEST, "PixelMapLoader005 begin.");
290 PixelMapLoader loader;
291 ASSERT_NE(loader.handler_, nullptr);
292
293 std::vector<uint8_t> buff;
294 auto ret = loader.DecodeTlv(buff);
295 EXPECT_EQ(ret, nullptr);
296 LOG_INFO(UDMF_TEST, "PixelMapLoader005 end.");
297 }
298
299 /**
300 * @tc.name: PixelMapLoader006
301 * @tc.desc: Abnormal testcase of PixelMapLoader006
302 * @tc.type: FUNC
303 */
304 HWTEST_F(SystemDefinedPixelMapTest, PixelMapLoader006, TestSize.Level1)
305 {
306 LOG_INFO(UDMF_TEST, "PixelMapLoader006 begin.");
307 PixelMapLoader loader;
308 ASSERT_NE(loader.handler_, nullptr);
309 std::vector<uint8_t> buff;
310 std::shared_ptr<OHOS::Media::PixelMap> pixelMapIn = nullptr;
311
312 auto ret = loader.EncodeTlv(pixelMapIn, buff);
313 EXPECT_EQ(ret, false);
314 LOG_INFO(UDMF_TEST, "PixelMapLoader006 end.");
315 }
316
317 /**
318 * @tc.name: PixelMapLoader007
319 * @tc.desc: Abnormal testcase of PixelMapLoader007
320 * @tc.type: FUNC
321 */
322 HWTEST_F(SystemDefinedPixelMapTest, PixelMapLoader007, TestSize.Level1)
323 {
324 LOG_INFO(UDMF_TEST, "PixelMapLoader007 begin.");
325 PixelMapLoader loader;
326 ASSERT_NE(loader.handler_, nullptr);
327
328 PixelMapDetails details;
329 details.rawData = std::nullopt;
330 auto ret = loader.GetPixelMapFromRawData(details);
331 EXPECT_EQ(ret, nullptr);
332 LOG_INFO(UDMF_TEST, "PixelMapLoader007 end.");
333 }
334
335 /**
336 * @tc.name: PixelMapLoader008
337 * @tc.desc: Abnormal testcase of PixelMapLoader008
338 * @tc.type: FUNC
339 */
340 HWTEST_F(SystemDefinedPixelMapTest, PixelMapLoader008, TestSize.Level1)
341 {
342 LOG_INFO(UDMF_TEST, "PixelMapLoader008 begin.");
343 PixelMapLoader loader;
344 ASSERT_NE(loader.handler_, nullptr);
345
346 auto ret = loader.ParseInfoFromPixelMap(nullptr);
347 EXPECT_EQ(ret, nullptr);
348 LOG_INFO(UDMF_TEST, "PixelMapLoader008 end.");
349 }
350 } // OHOS::Test