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 ASSERT_TRUE(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 ASSERT_TRUE(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 ASSERT_TRUE(skiaBitmap.GetColorType() == ColorType::COLORTYPE_UNKNOWN);
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 ASSERT_TRUE(skiaBitmap.GetAlphaType() == AlphaType::ALPHATYPE_UNKNOWN);
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 ASSERT_TRUE(skiaBitmap.ExportSkiaBitmap().empty());
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 ASSERT_TRUE(!skiaBitmap.IsImmutable());
211 }
212
213 /**
214 * @tc.name: ClearWithColor001
215 * @tc.desc: Test ClearWithColor
216 * @tc.type: FUNC
217 * @tc.require: I91F9L
218 */
219 HWTEST_F(SkiaBitmapTest, ClearWithColor001, TestSize.Level1)
220 {
221 SkiaBitmap skiaBitmap;
222 ASSERT_TRUE(!skiaBitmap.IsImmutable());
223 skiaBitmap.ClearWithColor(0xFF000000);
224 }
225
226 /**
227 * @tc.name: GetColor001
228 * @tc.desc: Test GetColor
229 * @tc.type: FUNC
230 * @tc.require: I91F9L
231 */
232 HWTEST_F(SkiaBitmapTest, GetColor001, TestSize.Level1)
233 {
234 int width = 100;
235 int height = 50;
236 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888,
237 AlphaType::ALPHATYPE_PREMUL};
238 SkiaBitmap skiaBitmap;
239 skiaBitmap.Build(width, height, bitmapFormat, 0);
240 skiaBitmap.ClearWithColor(Color::COLOR_BLACK);
241 ASSERT_TRUE(skiaBitmap.GetColor(0, 0) == Color::COLOR_BLACK);
242 }
243
244 /**
245 * @tc.name: Free001
246 * @tc.desc: Test Free
247 * @tc.type: FUNC
248 * @tc.require: I91F9L
249 */
250 HWTEST_F(SkiaBitmapTest, Free001, TestSize.Level1)
251 {
252 SkiaBitmap skiaBitmap;
253 skiaBitmap.Free();
254 ASSERT_TRUE(skiaBitmap.IsValid());
255 }
256
257 /**
258 * @tc.name: IsValid001
259 * @tc.desc: Test IsValid
260 * @tc.type: FUNC
261 * @tc.require: I91F9L
262 */
263 HWTEST_F(SkiaBitmapTest, IsValid001, TestSize.Level1)
264 {
265 SkiaBitmap skiaBitmap;
266 ASSERT_TRUE(skiaBitmap.IsValid());
267 }
268
269 /**
270 * @tc.name: IsEmpty001
271 * @tc.desc: Test IsEmpty
272 * @tc.type: FUNC
273 * @tc.require: I91F9L
274 */
275 HWTEST_F(SkiaBitmapTest, IsEmpty001, TestSize.Level1)
276 {
277 SkiaBitmap skiaBitmap;
278 ASSERT_TRUE(skiaBitmap.IsEmpty());
279 }
280
281 /**
282 * @tc.name: GetPixmap001
283 * @tc.desc: Test GetPixmap
284 * @tc.type: FUNC
285 * @tc.require: I91F9L
286 */
287 HWTEST_F(SkiaBitmapTest, GetPixmap001, TestSize.Level1)
288 {
289 SkiaBitmap skiaBitmap;
290 ASSERT_TRUE(skiaBitmap.GetPixmap().GetWidth() == 0);
291 }
292
293 /**
294 * @tc.name: TryAllocPixels001
295 * @tc.desc: Test TryAllocPixels
296 * @tc.type: FUNC
297 * @tc.require: I91F9L
298 */
299 HWTEST_F(SkiaBitmapTest, TryAllocPixels001, TestSize.Level1)
300 {
301 int width = 100;
302 int height = 50;
303 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888,
304 ALPHATYPE_PREMUL, nullptr};
305 SkiaBitmap skiaBitmap;
306 skiaBitmap.TryAllocPixels(offscreenInfo);
307 ASSERT_TRUE(!skiaBitmap.IsValid());
308 }
309
310 /**
311 * @tc.name: Serialize001
312 * @tc.desc: Test Serialize
313 * @tc.type: FUNC
314 * @tc.require: I91F9L
315 */
316 HWTEST_F(SkiaBitmapTest, Serialize001, TestSize.Level1)
317 {
318 SkiaBitmap skiaBitmap;
319 skiaBitmap.Serialize();
320 skiaBitmap.Deserialize(nullptr);
321 skiaBitmap.Deserialize(std::make_shared<Data>());
322
323 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888,
324 AlphaType::ALPHATYPE_PREMUL};
325 skiaBitmap.Build(100, 50, bitmapFormat, 0); // 100:width, 50:height
326 skiaBitmap.ClearWithColor(0xFF000000);
327 ASSERT_TRUE(skiaBitmap.Serialize() != nullptr);
328 skiaBitmap.Deserialize(std::make_shared<Data>());
329 }
330
331 /**
332 * @tc.name: Build003
333 * @tc.desc: Test Build
334 * @tc.type: FUNC
335 * @tc.require: I91F9L
336 */
337 HWTEST_F(SkiaBitmapTest, Build003, TestSize.Level1)
338 {
339 const int width = -100;
340 const int height = -50;
341 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL};
342 SkiaBitmap skiaBitmap;
343 bool build = skiaBitmap.Build(width, height, bitmapFormat, 0);
344 ASSERT_FALSE(build);
345 }
346
347 /**
348 * @tc.name: Build004
349 * @tc.desc: Test Build
350 * @tc.type: FUNC
351 * @tc.require: I91F9L
352 */
353 HWTEST_F(SkiaBitmapTest, Build004, TestSize.Level1)
354 {
355 const int width = -100;
356 const int height = -50;
357 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, nullptr};
358 SkiaBitmap skiaBitmap;
359 bool build = skiaBitmap.Build(offscreenInfo, 0);
360 ASSERT_FALSE(build);
361 }
362
363 /**
364 * @tc.name: SetInfo
365 * @tc.desc: Test SetInfo
366 * @tc.type: FUNC
367 * @tc.require: I91F9L
368 */
369 HWTEST_F(SkiaBitmapTest, SetInfo, TestSize.Level1)
370 {
371 const int width = 100;
372 const int height = 50;
373 SkiaBitmap skiaBitmap;
374 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, nullptr};
375 skiaBitmap.SetInfo(offscreenInfo);
376 ASSERT_EQ(ColorType::COLORTYPE_RGBA_8888, skiaBitmap.GetColorType());
377 ASSERT_EQ(AlphaType::ALPHATYPE_PREMUL, skiaBitmap.GetAlphaType());
378 }
379
380 /**
381 * @tc.name: ComputeByteSize
382 * @tc.desc: Test ComputeByteSize
383 * @tc.type: FUNC
384 * @tc.require: I91F9L
385 */
386 HWTEST_F(SkiaBitmapTest, ComputeByteSize, TestSize.Level1)
387 {
388 const int width = 100;
389 const int height = 50;
390 SkiaBitmap skiaBitmap;
391 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL };
392 bool build = skiaBitmap.Build(width, height, bitmapFormat, 0);
393 ASSERT_TRUE(build);
394 size_t byteSize = skiaBitmap.ComputeByteSize();
395 ASSERT_TRUE(byteSize >= 0);
396 }
397
398 /**
399 * @tc.name: CopyPixels
400 * @tc.desc: Test CopyPixels
401 * @tc.type: FUNC
402 * @tc.require: I91F9L
403 */
404 HWTEST_F(SkiaBitmapTest, CopyPixels, TestSize.Level1)
405 {
406 const int srcWidth = 100;
407 const int srcHight = 50;
408 std::unique_ptr<Bitmap> dstBitmap = std::make_unique<Bitmap>();
409 ASSERT_TRUE(dstBitmap != nullptr);
410 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL };
411 SkiaBitmap skiaBitmap;
412 skiaBitmap.Build(srcWidth, srcHight, bitmapFormat, 0);
413 skiaBitmap.CopyPixels(*dstBitmap, 0, 0);
414 void* pixels = dstBitmap->GetPixels();
415 ASSERT_TRUE(pixels != skiaBitmap.GetPixels());
416 }
417
418 /**
419 * @tc.name: MakeImage
420 * @tc.desc: Test MakeImage
421 * @tc.type: FUNC
422 * @tc.require: I91F9L
423 */
424 HWTEST_F(SkiaBitmapTest, MakeImage, TestSize.Level1)
425 {
426 SkBitmap skBitmap;
427 SkiaBitmap skiaBitmap;
428 skiaBitmap.SetSkBitmap(skBitmap);
429 std::shared_ptr<Image> result = skiaBitmap.MakeImage();
430 ASSERT_TRUE(result == nullptr);
431 }
432
433 /**
434 * @tc.name: Serialize002
435 * @tc.desc: Test Serialize
436 * @tc.type: FUNC
437 * @tc.require: I91F9L
438 */
439 HWTEST_F(SkiaBitmapTest, Serialize002, TestSize.Level1)
440 {
441 const int width = 100;
442 const int height = 50;
443 SkiaBitmap skiaBitmap;
444 std::shared_ptr<ColorSpace> colorSpace = ColorSpace::CreateSRGB();
445 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, colorSpace};
446 bool build = skiaBitmap.Build(offscreenInfo, 0);
447 ASSERT_TRUE(build);
448 std::shared_ptr<Data> result = skiaBitmap.Serialize();
449 ASSERT_TRUE(result != nullptr);
450 }
451
452 /**
453 * @tc.name: Serialize003
454 * @tc.desc: Test Serialize
455 * @tc.type: FUNC
456 * @tc.require: I91F9L
457 */
458 HWTEST_F(SkiaBitmapTest, Serialize003, TestSize.Level1)
459 {
460 const int width = 100;
461 const int height = 50;
462 SkiaBitmap skiaBitmap;
463 std::shared_ptr<ColorSpace> colorSpace = ColorSpace::CreateSRGB();
464 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, colorSpace};
465 bool build = skiaBitmap.Build(offscreenInfo, 0);
466 ASSERT_TRUE(build);
467 std::shared_ptr<Data> result = skiaBitmap.Serialize();
468 ASSERT_TRUE(result->GetSize() == 20104);
469 }
470
471 /**
472 * @tc.name: Deserialize
473 * @tc.desc: Test Deserialize
474 * @tc.type: FUNC
475 * @tc.require: I91F9L
476 */
477 HWTEST_F(SkiaBitmapTest, Deserialize, TestSize.Level1)
478 {
479 const int width = 100;
480 const int height = 50;
481 SkiaBitmap skiaBitmap;
482 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL};
483 skiaBitmap.Build(width, height, bitmapFormat, 0);
484 std::shared_ptr<Data> data = skiaBitmap.Serialize();
485 ASSERT_TRUE(data != nullptr);
486 bool ret = skiaBitmap.Deserialize(data);
487 ASSERT_TRUE(ret);
488 }
489 } // namespace Drawing
490 } // namespace Rosen
491 } // namespace OHOS