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 <cstddef>
17 #include "gtest/gtest.h"
18 #include "draw/surface.h"
19 #include "effect/color_space.h"
20 #include "image/bitmap.h"
21 #include "image/image_info.h"
22 #include "include/core/SkBitmap.h"
23 #include "include/core/SkImageInfo.h"
24 #include "skia_adapter/skia_bitmap.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace Drawing {
32 class SkiaBitmapTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38 };
39
SetUpTestCase()40 void SkiaBitmapTest::SetUpTestCase() {}
TearDownTestCase()41 void SkiaBitmapTest::TearDownTestCase() {}
SetUp()42 void SkiaBitmapTest::SetUp() {}
TearDown()43 void SkiaBitmapTest::TearDown() {}
44
45 /**
46 * @tc.name: Build001
47 * @tc.desc: Test Build
48 * @tc.type: FUNC
49 * @tc.require: I91F9L
50 */
51 HWTEST_F(SkiaBitmapTest, Build001, TestSize.Level1)
52 {
53 int width = 100;
54 int height = 50;
55 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888,
56 AlphaType::ALPHATYPE_PREMUL};
57 SkiaBitmap skiaBitmap;
58 skiaBitmap.Build(width, height, bitmapFormat, 0);
59 }
60
61 /**
62 * @tc.name: Build002
63 * @tc.desc: Test Build
64 * @tc.type: FUNC
65 * @tc.require: I91F9L
66 */
67 HWTEST_F(SkiaBitmapTest, Build002, TestSize.Level1)
68 {
69 int width = 100;
70 int height = 50;
71 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888,
72 ALPHATYPE_PREMUL, nullptr};
73 SkiaBitmap skiaBitmap;
74 skiaBitmap.Build(offscreenInfo, 0);
75 }
76
77 /**
78 * @tc.name: GetWidth001
79 * @tc.desc: Test GetWidth
80 * @tc.type: FUNC
81 * @tc.require: I91F9L
82 */
83 HWTEST_F(SkiaBitmapTest, GetWidth001, TestSize.Level1)
84 {
85 SkiaBitmap skiaBitmap;
86 ASSERT_TRUE(skiaBitmap.GetWidth() >= 0);
87 }
88
89 /**
90 * @tc.name: GetHeight001
91 * @tc.desc: Test GetHeight
92 * @tc.type: FUNC
93 * @tc.require: I91F9L
94 */
95 HWTEST_F(SkiaBitmapTest, GetHeight001, TestSize.Level1)
96 {
97 SkiaBitmap skiaBitmap;
98 ASSERT_TRUE(skiaBitmap.GetHeight() >= 0);
99 }
100
101 /**
102 * @tc.name: GetRowBytes001
103 * @tc.desc: Test GetRowBytes
104 * @tc.type: FUNC
105 * @tc.require: I91F9L
106 */
107 HWTEST_F(SkiaBitmapTest, GetRowBytes001, TestSize.Level1)
108 {
109 SkiaBitmap skiaBitmap;
110 ASSERT_TRUE(skiaBitmap.GetRowBytes() >= 0);
111 }
112
113 /**
114 * @tc.name: GetColorType001
115 * @tc.desc: Test GetColorType
116 * @tc.type: FUNC
117 * @tc.require: I91F9L
118 */
119 HWTEST_F(SkiaBitmapTest, GetColorType001, TestSize.Level1)
120 {
121 SkiaBitmap skiaBitmap;
122 skiaBitmap.GetColorType();
123 }
124
125 /**
126 * @tc.name: GetAlphaType001
127 * @tc.desc: Test GetAlphaType
128 * @tc.type: FUNC
129 * @tc.require: I91F9L
130 */
131 HWTEST_F(SkiaBitmapTest, GetAlphaType001, TestSize.Level1)
132 {
133 SkiaBitmap skiaBitmap;
134 skiaBitmap.GetAlphaType();
135 }
136
137 /**
138 * @tc.name: ExtractSubset001
139 * @tc.desc: Test ExtractSubset
140 * @tc.type: FUNC
141 * @tc.require: I91F9L
142 */
143 HWTEST_F(SkiaBitmapTest, ExtractSubset001, TestSize.Level1)
144 {
145 Bitmap left;
146 Rect subset = Rect(0, 0, 50, 50); // 50: right, bottom
147 SkiaBitmap skiaBitmap;
148 ASSERT_TRUE(!skiaBitmap.ExtractSubset(left, subset));
149 }
150
151 /**
152 * @tc.name: GetPixels001
153 * @tc.desc: Test GetPixels
154 * @tc.type: FUNC
155 * @tc.require: I91F9L
156 */
157 HWTEST_F(SkiaBitmapTest, GetPixels001, TestSize.Level1)
158 {
159 SkiaBitmap skiaBitmap;
160 ASSERT_TRUE(skiaBitmap.GetPixels() == nullptr);
161 }
162
163 /**
164 * @tc.name: PeekPixels001
165 * @tc.desc: Test PeekPixels
166 * @tc.type: FUNC
167 * @tc.require: I91F9L
168 */
169 HWTEST_F(SkiaBitmapTest, PeekPixels001, TestSize.Level1)
170 {
171 Pixmap pixmap;
172 SkiaBitmap skiaBitmap;
173 ASSERT_TRUE(!skiaBitmap.PeekPixels(pixmap));
174 }
175
176 /**
177 * @tc.name: ExportSkiaBitmap001
178 * @tc.desc: Test ExportSkiaBitmap
179 * @tc.type: FUNC
180 * @tc.require: I91F9L
181 */
182 HWTEST_F(SkiaBitmapTest, ExportSkiaBitmap001, TestSize.Level1)
183 {
184 SkiaBitmap skiaBitmap;
185 skiaBitmap.ExportSkiaBitmap();
186 }
187
188 /**
189 * @tc.name: IsImmutable001
190 * @tc.desc: Test IsImmutable
191 * @tc.type: FUNC
192 * @tc.require: I91F9L
193 */
194 HWTEST_F(SkiaBitmapTest, IsImmutable001, TestSize.Level1)
195 {
196 SkiaBitmap skiaBitmap;
197 ASSERT_TRUE(!skiaBitmap.IsImmutable());
198 }
199
200 /**
201 * @tc.name: SetImmutable001
202 * @tc.desc: Test SetImmutable
203 * @tc.type: FUNC
204 * @tc.require: I91F9L
205 */
206 HWTEST_F(SkiaBitmapTest, SetImmutable001, TestSize.Level1)
207 {
208 SkiaBitmap skiaBitmap;
209 skiaBitmap.SetImmutable();
210 }
211
212 /**
213 * @tc.name: ClearWithColor001
214 * @tc.desc: Test ClearWithColor
215 * @tc.type: FUNC
216 * @tc.require: I91F9L
217 */
218 HWTEST_F(SkiaBitmapTest, ClearWithColor001, TestSize.Level1)
219 {
220 SkiaBitmap skiaBitmap;
221 skiaBitmap.ClearWithColor(0xFF000000);
222 }
223
224 /**
225 * @tc.name: GetColor001
226 * @tc.desc: Test GetColor
227 * @tc.type: FUNC
228 * @tc.require: I91F9L
229 */
230 HWTEST_F(SkiaBitmapTest, GetColor001, TestSize.Level1)
231 {
232 SkiaBitmap skiaBitmap;
233 skiaBitmap.GetColor(0, 0);
234 }
235
236 /**
237 * @tc.name: Free001
238 * @tc.desc: Test Free
239 * @tc.type: FUNC
240 * @tc.require: I91F9L
241 */
242 HWTEST_F(SkiaBitmapTest, Free001, TestSize.Level1)
243 {
244 SkiaBitmap skiaBitmap;
245 skiaBitmap.Free();
246 }
247
248 /**
249 * @tc.name: IsValid001
250 * @tc.desc: Test IsValid
251 * @tc.type: FUNC
252 * @tc.require: I91F9L
253 */
254 HWTEST_F(SkiaBitmapTest, IsValid001, TestSize.Level1)
255 {
256 SkiaBitmap skiaBitmap;
257 ASSERT_TRUE(skiaBitmap.IsValid());
258 }
259
260 /**
261 * @tc.name: IsEmpty001
262 * @tc.desc: Test IsEmpty
263 * @tc.type: FUNC
264 * @tc.require: I91F9L
265 */
266 HWTEST_F(SkiaBitmapTest, IsEmpty001, TestSize.Level1)
267 {
268 SkiaBitmap skiaBitmap;
269 ASSERT_TRUE(skiaBitmap.IsEmpty());
270 }
271
272 /**
273 * @tc.name: GetPixmap001
274 * @tc.desc: Test GetPixmap
275 * @tc.type: FUNC
276 * @tc.require: I91F9L
277 */
278 HWTEST_F(SkiaBitmapTest, GetPixmap001, TestSize.Level1)
279 {
280 SkiaBitmap skiaBitmap;
281 skiaBitmap.GetPixmap();
282 }
283
284 /**
285 * @tc.name: TryAllocPixels001
286 * @tc.desc: Test TryAllocPixels
287 * @tc.type: FUNC
288 * @tc.require: I91F9L
289 */
290 HWTEST_F(SkiaBitmapTest, TryAllocPixels001, TestSize.Level1)
291 {
292 int width = 100;
293 int height = 50;
294 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888,
295 ALPHATYPE_PREMUL, nullptr};
296 SkiaBitmap skiaBitmap;
297 skiaBitmap.TryAllocPixels(offscreenInfo);
298 }
299
300 /**
301 * @tc.name: Serialize001
302 * @tc.desc: Test Serialize
303 * @tc.type: FUNC
304 * @tc.require: I91F9L
305 */
306 HWTEST_F(SkiaBitmapTest, Serialize001, TestSize.Level1)
307 {
308 SkiaBitmap skiaBitmap;
309 skiaBitmap.Serialize();
310 skiaBitmap.Deserialize(nullptr);
311 skiaBitmap.Deserialize(std::make_shared<Data>());
312
313 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888,
314 AlphaType::ALPHATYPE_PREMUL};
315 skiaBitmap.Build(100, 50, bitmapFormat, 0); // 100:width, 50:height
316 skiaBitmap.ClearWithColor(0xFF000000);
317 skiaBitmap.Serialize();
318 skiaBitmap.Deserialize(std::make_shared<Data>());
319 }
320
321 /**
322 * @tc.name: Build003
323 * @tc.desc: Test Build
324 * @tc.type: FUNC
325 * @tc.require: I91F9L
326 */
327 HWTEST_F(SkiaBitmapTest, Build003, TestSize.Level1)
328 {
329 const int width = -100;
330 const int height = -50;
331 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL};
332 SkiaBitmap skiaBitmap;
333 bool build = skiaBitmap.Build(width, height, bitmapFormat, 0);
334 ASSERT_FALSE(build);
335 }
336
337 /**
338 * @tc.name: Build004
339 * @tc.desc: Test Build
340 * @tc.type: FUNC
341 * @tc.require: I91F9L
342 */
343 HWTEST_F(SkiaBitmapTest, Build004, TestSize.Level1)
344 {
345 const int width = -100;
346 const int height = -50;
347 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, nullptr};
348 SkiaBitmap skiaBitmap;
349 bool build = skiaBitmap.Build(offscreenInfo, 0);
350 ASSERT_FALSE(build);
351 }
352
353 /**
354 * @tc.name: SetInfo
355 * @tc.desc: Test SetInfo
356 * @tc.type: FUNC
357 * @tc.require: I91F9L
358 */
359 HWTEST_F(SkiaBitmapTest, SetInfo, TestSize.Level1)
360 {
361 const int width = 100;
362 const int height = 50;
363 SkiaBitmap skiaBitmap;
364 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, nullptr};
365 skiaBitmap.SetInfo(offscreenInfo);
366 ASSERT_EQ(ColorType::COLORTYPE_RGBA_8888, skiaBitmap.GetColorType());
367 ASSERT_EQ(AlphaType::ALPHATYPE_PREMUL, skiaBitmap.GetAlphaType());
368 }
369
370 /**
371 * @tc.name: ComputeByteSize
372 * @tc.desc: Test ComputeByteSize
373 * @tc.type: FUNC
374 * @tc.require: I91F9L
375 */
376 HWTEST_F(SkiaBitmapTest, ComputeByteSize, TestSize.Level1)
377 {
378 const int width = 100;
379 const int height = 50;
380 SkiaBitmap skiaBitmap;
381 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL };
382 bool build = skiaBitmap.Build(width, height, bitmapFormat, 0);
383 ASSERT_TRUE(build);
384 size_t byteSize = skiaBitmap.ComputeByteSize();
385 ASSERT_TRUE(byteSize >= 0);
386 }
387
388 /**
389 * @tc.name: CopyPixels
390 * @tc.desc: Test CopyPixels
391 * @tc.type: FUNC
392 * @tc.require: I91F9L
393 */
394 HWTEST_F(SkiaBitmapTest, CopyPixels, TestSize.Level1)
395 {
396 const int srcWidth = 100;
397 const int srcHight = 50;
398 std::unique_ptr<Bitmap> dstBitmap = std::make_unique<Bitmap>();
399 ASSERT_TRUE(dstBitmap != nullptr);
400 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL };
401 SkiaBitmap skiaBitmap;
402 skiaBitmap.Build(srcWidth, srcHight, bitmapFormat, 0);
403 skiaBitmap.CopyPixels(*dstBitmap, 0, 0);
404 void* pixels = dstBitmap->GetPixels();
405 ASSERT_TRUE(pixels != skiaBitmap.GetPixels());
406 }
407
408 /**
409 * @tc.name: MakeImage
410 * @tc.desc: Test MakeImage
411 * @tc.type: FUNC
412 * @tc.require: I91F9L
413 */
414 HWTEST_F(SkiaBitmapTest, MakeImage, TestSize.Level1)
415 {
416 SkBitmap skBitmap;
417 SkiaBitmap skiaBitmap;
418 skiaBitmap.SetSkBitmap(skBitmap);
419 std::shared_ptr<Image> result = skiaBitmap.MakeImage();
420 ASSERT_TRUE(result == nullptr);
421 }
422
423 /**
424 * @tc.name: Serialize002
425 * @tc.desc: Test Serialize
426 * @tc.type: FUNC
427 * @tc.require: I91F9L
428 */
429 HWTEST_F(SkiaBitmapTest, Serialize002, TestSize.Level1)
430 {
431 const int width = 100;
432 const int height = 50;
433 SkiaBitmap skiaBitmap;
434 std::shared_ptr<ColorSpace> colorSpace = ColorSpace::CreateSRGB();
435 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, colorSpace};
436 bool build = skiaBitmap.Build(offscreenInfo, 0);
437 ASSERT_TRUE(build);
438 std::shared_ptr<Data> result = skiaBitmap.Serialize();
439 ASSERT_TRUE(result != nullptr);
440 }
441
442 /**
443 * @tc.name: Serialize003
444 * @tc.desc: Test Serialize
445 * @tc.type: FUNC
446 * @tc.require: I91F9L
447 */
448 HWTEST_F(SkiaBitmapTest, Serialize003, TestSize.Level1)
449 {
450 const int width = 100;
451 const int height = 50;
452 SkiaBitmap skiaBitmap;
453 std::shared_ptr<ColorSpace> colorSpace = ColorSpace::CreateSRGB();
454 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, colorSpace};
455 bool build = skiaBitmap.Build(offscreenInfo, 0);
456 ASSERT_TRUE(build);
457 std::shared_ptr<Data> result = skiaBitmap.Serialize();
458 ASSERT_TRUE(result->GetSize() == 20104);
459 }
460
461 /**
462 * @tc.name: Deserialize
463 * @tc.desc: Test Deserialize
464 * @tc.type: FUNC
465 * @tc.require: I91F9L
466 */
467 HWTEST_F(SkiaBitmapTest, Deserialize, TestSize.Level1)
468 {
469 const int width = 100;
470 const int height = 50;
471 SkiaBitmap skiaBitmap;
472 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL};
473 skiaBitmap.Build(width, height, bitmapFormat, 0);
474 std::shared_ptr<Data> data = skiaBitmap.Serialize();
475 ASSERT_TRUE(data != nullptr);
476 bool ret = skiaBitmap.Deserialize(data);
477 ASSERT_TRUE(ret);
478 }
479 } // namespace Drawing
480 } // namespace Rosen
481 } // namespace OHOS