1 /*
2  * Copyright (c) 2022 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, Hardware
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 "draw/color.h"
19 #include "image/bitmap.h"
20 #include "image/image.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 class BitmapTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp() override;
33     void TearDown() override;
34 };
35 
SetUpTestCase()36 void BitmapTest::SetUpTestCase() {}
TearDownTestCase()37 void BitmapTest::TearDownTestCase() {}
SetUp()38 void BitmapTest::SetUp() {}
TearDown()39 void BitmapTest::TearDown() {}
40 
41 /**
42  * @tc.name: BitmapCreateAndDestroy001
43  * @tc.desc:
44  * @tc.type: FUNC
45  * @tc.require:AR000GGNV3
46  * @tc.author:
47  */
48 HWTEST_F(BitmapTest, BitmapCreateAndDestroy001, TestSize.Level1)
49 {
50     // The best way to create Bitmap.
51     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
52     ASSERT_TRUE(bitmap != nullptr);
53 }
54 
55 /**
56  * @tc.name: BitmapBuildTest001
57  * @tc.desc:
58  * @tc.type: FUNC
59  * @tc.require:AR000GGNV3
60  * @tc.author:
61  */
62 HWTEST_F(BitmapTest, BitmapBuildTest001, TestSize.Level1)
63 {
64     // The best way to Build Bitmap.
65     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
66     ASSERT_TRUE(bitmap != nullptr);
67     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
68     EXPECT_TRUE(bitmap->Build(100, 200, bitmapFormat));
69 
70     bitmapFormat = { COLORTYPE_RGB_565, ALPHATYPE_PREMUL };
71     EXPECT_TRUE(bitmap->Build(100, 200, bitmapFormat));
72 
73     bitmapFormat = { COLORTYPE_ARGB_4444, ALPHATYPE_UNPREMUL };
74     EXPECT_TRUE(bitmap->Build(100, 200, bitmapFormat));
75 
76     bitmapFormat.colorType = COLORTYPE_RGBA_8888;
77     EXPECT_TRUE(bitmap->Build(100, 200, bitmapFormat));
78 
79     bitmapFormat.colorType = COLORTYPE_BGRA_8888;
80     EXPECT_TRUE(bitmap->Build(100, 200, bitmapFormat));
81 
82     bitmapFormat.colorType = COLORTYPE_N32;
83     EXPECT_TRUE(bitmap->Build(100, 200, bitmapFormat));
84 }
85 
86 /**
87  * @tc.name: BitmapBuildTest002
88  * @tc.desc:
89  * @tc.type: FUNC
90  * @tc.require:AR000GGNV3
91  * @tc.author:
92  */
93 HWTEST_F(BitmapTest, BitmapBuildTest002, TestSize.Level1)
94 {
95     // The best way to Build Bitmap.
96     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
97     ASSERT_TRUE(bitmap != nullptr);
98     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
99     EXPECT_TRUE(bitmap->Build(150, 99, bitmapFormat));
100 
101     bitmapFormat = { COLORTYPE_UNKNOWN, ALPHATYPE_OPAQUE };
102     EXPECT_FALSE(bitmap->Build(100, 200, bitmapFormat));
103 
104     bitmapFormat = { COLORTYPE_ALPHA_8, ALPHATYPE_UNKNOWN };
105     EXPECT_FALSE(bitmap->Build(100, 200, bitmapFormat));
106 }
107 
108 /**
109  * @tc.name: BitmapBuildTest003
110  * @tc.desc:
111  * @tc.type: FUNC
112  * @tc.require:AR000GGNV3
113  * @tc.author:
114  */
115 HWTEST_F(BitmapTest, BitmapBuildTest003, TestSize.Level1)
116 {
117     // The best way to Build Bitmap.
118     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
119     ASSERT_TRUE(bitmap != nullptr);
120     ImageInfo imageInfo = {111, 450, ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE};
121     EXPECT_TRUE(bitmap->Build(imageInfo));
122 
123     imageInfo = {111, 450, ColorType::COLORTYPE_UNKNOWN, AlphaType::ALPHATYPE_OPAQUE};
124     EXPECT_FALSE(bitmap->Build(imageInfo));
125 
126     imageInfo = {111, 450, ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_UNKNOWN};
127     EXPECT_FALSE(bitmap->Build(imageInfo));
128 }
129 
130 /**
131  * @tc.name: BitmapGetWidthTest001
132  * @tc.desc:
133  * @tc.type: FUNC
134  * @tc.require:AR000GGNV3
135  * @tc.author:
136  */
137 HWTEST_F(BitmapTest, BitmapGetWidthTest001, TestSize.Level1)
138 {
139     // The best way to get width.
140     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
141     ASSERT_TRUE(bitmap != nullptr);
142     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
143     EXPECT_TRUE(bitmap->Build(111, 450, bitmapFormat));
144     ASSERT_EQ(111, bitmap->GetWidth());
145 }
146 
147 /**
148  * @tc.name: BitmapGetWidthTest002
149  * @tc.desc:
150  * @tc.type: FUNC
151  * @tc.require:AR000GGNV3
152  * @tc.author:
153  */
154 HWTEST_F(BitmapTest, BitmapGetWidthTest002, TestSize.Level1)
155 {
156     // The best way to get width.
157     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
158     ASSERT_TRUE(bitmap != nullptr);
159     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
160     EXPECT_TRUE(bitmap->Build(151, 150, bitmapFormat));
161     ASSERT_EQ(151, bitmap->GetWidth());
162 }
163 
164 /**
165  * @tc.name: BitmapGetHeightTest001
166  * @tc.desc:
167  * @tc.type: FUNC
168  * @tc.require:AR000GGNV3
169  * @tc.author:
170  */
171 HWTEST_F(BitmapTest, BitmapGetHeightTest001, TestSize.Level1)
172 {
173     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
174     ASSERT_TRUE(bitmap != nullptr);
175     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
176     EXPECT_TRUE(bitmap->Build(111, 450, bitmapFormat));
177     ASSERT_EQ(450, bitmap->GetHeight());
178 }
179 
180 /**
181  * @tc.name: BitmapGetHeightTest002
182  * @tc.desc:
183  * @tc.type: FUNC
184  * @tc.require:AR000GGNV3
185  * @tc.author:
186  */
187 HWTEST_F(BitmapTest, BitmapGetHeightTest002, TestSize.Level1)
188 {
189     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
190     ASSERT_TRUE(bitmap != nullptr);
191     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
192     EXPECT_TRUE(bitmap->Build(151, 150, bitmapFormat));
193     ASSERT_EQ(150, bitmap->GetHeight());
194 }
195 
196 /**
197  * @tc.name: BitmapGetRowBytesTest
198  * @tc.desc:
199  * @tc.type: FUNC
200  * @tc.require:AR000GGNV3
201  * @tc.author:
202  */
203 HWTEST_F(BitmapTest, BitmapGetRowBytesTest, TestSize.Level1)
204 {
205     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
206     ASSERT_TRUE(bitmap != nullptr);
207     ASSERT_EQ(0, bitmap->GetRowBytes());
208     EXPECT_TRUE(bitmap->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
209     ASSERT_EQ(151, bitmap->GetRowBytes());
210 }
211 
212 /**
213  * @tc.name: BitmapGetColorTypeTest
214  * @tc.desc:
215  * @tc.type: FUNC
216  * @tc.require:AR000GGNV3
217  * @tc.author:
218  */
219 HWTEST_F(BitmapTest, BitmapGetColorTypeTest, TestSize.Level1)
220 {
221     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
222     ASSERT_TRUE(bitmap != nullptr);
223     ASSERT_EQ(ColorType::COLORTYPE_UNKNOWN, bitmap->GetColorType());
224     EXPECT_TRUE(bitmap->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
225     ASSERT_EQ(ColorType::COLORTYPE_ALPHA_8, bitmap->GetColorType());
226 }
227 
228 /**
229  * @tc.name: BitmapGetAlphaTypeTest
230  * @tc.desc:
231  * @tc.type: FUNC
232  * @tc.require:AR000GGNV3
233  * @tc.author:
234  */
235 HWTEST_F(BitmapTest, BitmapGetAlphaTypeTest, TestSize.Level1)
236 {
237     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
238     ASSERT_TRUE(bitmap != nullptr);
239     ASSERT_EQ(AlphaType::ALPHATYPE_UNKNOWN, bitmap->GetAlphaType());
240     EXPECT_TRUE(bitmap->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
241     ASSERT_EQ(AlphaType::ALPHATYPE_OPAQUE, bitmap->GetAlphaType());
242 }
243 
244 /**
245  * @tc.name: BitmapExtractSubsetTest
246  * @tc.desc:
247  * @tc.type: FUNC
248  * @tc.require:AR000GGNV3
249  * @tc.author:
250  */
251 HWTEST_F(BitmapTest, BitmapExtractSubsetTest, TestSize.Level1)
252 {
253     std::unique_ptr<Bitmap> bitmap1 = std::make_unique<Bitmap>();
254     std::unique_ptr<Bitmap> bitmap2 = std::make_unique<Bitmap>();
255     ASSERT_TRUE(bitmap1 != nullptr);
256     EXPECT_TRUE(bitmap1->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
257     EXPECT_TRUE(bitmap1->ExtractSubset(*bitmap2, Rect(0, 0, 100, 100)));
258     EXPECT_FALSE(bitmap1->ExtractSubset(*bitmap2, Rect(200, 200, 300, 300)));
259     EXPECT_TRUE(bitmap2->Build(151, 150, { ColorType::COLORTYPE_RGBA_8888, AlphaType::ALPHATYPE_PREMUL }));
260     EXPECT_TRUE(bitmap1->ExtractSubset(*bitmap2, Rect(0, 0, 100, 100)));
261 }
262 
263 /**
264  * @tc.name: BitmapReadPixelsTest
265  * @tc.desc:
266  * @tc.type: FUNC
267  * @tc.require:AR000GGNV3
268  * @tc.author:
269  */
270 HWTEST_F(BitmapTest, BitmapReadPixelsTest, TestSize.Level1)
271 {
272     std::unique_ptr<Bitmap> bitmap1 = std::make_unique<Bitmap>();
273     std::unique_ptr<Bitmap> bitmap2 = std::make_unique<Bitmap>();
274     ASSERT_TRUE(bitmap1 != nullptr);
275     int w = 151;
276     int h = 150;
277     ImageInfo info = {w, h, ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE};
278     EXPECT_TRUE(bitmap1->Build(info));
279     EXPECT_FALSE(bitmap1->ReadPixels(info, nullptr, 0, 0, 0));
280     EXPECT_TRUE(bitmap2->Build(info));
281     EXPECT_TRUE(bitmap2->GetPixels() != nullptr);
282     EXPECT_FALSE(bitmap1->ReadPixels(info, bitmap2->GetPixels(), 0, 0, 0));
283     EXPECT_TRUE(bitmap1->ReadPixels(info, bitmap2->GetPixels(), w, 0, 0));
284     EXPECT_FALSE(bitmap1->ReadPixels(info, bitmap2->GetPixels(), w/2, 0, 0));
285     EXPECT_FALSE(bitmap1->ReadPixels(info, bitmap2->GetPixels(), w/4, 0, 0));
286     EXPECT_FALSE(bitmap1->ReadPixels(info, bitmap2->GetPixels(), w, 200, 200));
287 }
288 
289 /**
290  * @tc.name: BitmapPeekPixelsTest
291  * @tc.desc:
292  * @tc.type: FUNC
293  * @tc.require:AR000GGNV3
294  * @tc.author:
295  */
296 HWTEST_F(BitmapTest, BitmapPeekPixelsTest, TestSize.Level1)
297 {
298     std::unique_ptr<Bitmap> bitmap1 = std::make_unique<Bitmap>();
299     Pixmap p;
300     ASSERT_TRUE(bitmap1 != nullptr);
301     EXPECT_FALSE(bitmap1->PeekPixels(p));
302     EXPECT_TRUE(bitmap1->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
303     EXPECT_TRUE(bitmap1->PeekPixels(p));
304 }
305 
306 /**
307  * @tc.name: BitmapInstallPixelsTest
308  * @tc.desc:
309  * @tc.type: FUNC
310  * @tc.require:AR000GGNV3
311  * @tc.author:
312  */
313 HWTEST_F(BitmapTest, BitmapInstallPixelsTest, TestSize.Level1)
314 {
315     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
316     ASSERT_TRUE(bitmap != nullptr);
317     int32_t w = 151;
318     int32_t h = 150;
319     void* data = malloc(w * h * 1);
320     ImageInfo info = { w, h, ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
321     EXPECT_TRUE(bitmap->InstallPixels(info, data, w, nullptr, nullptr));
322     free(data);
323 }
324 
325 /**
326  * @tc.name: BitmapImmutableTest
327  * @tc.desc:
328  * @tc.type: FUNC
329  * @tc.require:AR000GGNV3
330  * @tc.author:
331  */
332 HWTEST_F(BitmapTest, BitmapImmutableTest, TestSize.Level1)
333 {
334     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
335     ASSERT_TRUE(bitmap != nullptr);
336     EXPECT_FALSE(bitmap->IsImmutable());
337     EXPECT_TRUE(bitmap->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
338     EXPECT_FALSE(bitmap->IsImmutable());
339     bitmap->SetImmutable();
340     EXPECT_TRUE(bitmap->IsImmutable());
341 }
342 
343 /**
344  * @tc.name: BitmapGetPixmapTest
345  * @tc.desc:
346  * @tc.type: FUNC
347  * @tc.require:AR000GGNV3
348  * @tc.author:
349  */
350 HWTEST_F(BitmapTest, BitmapGetPixmapTest, TestSize.Level1)
351 {
352     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
353     ASSERT_TRUE(bitmap != nullptr);
354     EXPECT_TRUE(bitmap->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
355     Pixmap p = bitmap->GetPixmap();
356     ASSERT_EQ(p.GetAddr(), bitmap->GetPixels());
357 }
358 
359 /**
360  * @tc.name: BitmapMakeImageTest
361  * @tc.desc:
362  * @tc.type: FUNC
363  * @tc.require:AR000GGNV3
364  * @tc.author:
365  */
366 HWTEST_F(BitmapTest, BitmapMakeImageTest, TestSize.Level1)
367 {
368     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
369     ASSERT_TRUE(bitmap != nullptr);
370     EXPECT_TRUE(bitmap->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
371     std::shared_ptr<Image> image = bitmap->MakeImage();
372     ASSERT_TRUE(image != nullptr);
373     ASSERT_EQ(image->GetColorType(), bitmap->GetColorType());
374     ASSERT_EQ(image->GetAlphaType(), bitmap->GetAlphaType());
375 }
376 
377 /**
378  * @tc.name: BitmapSerializeTest
379  * @tc.desc:
380  * @tc.type: FUNC
381  * @tc.require:AR000GGNV3
382  * @tc.author:
383  */
384 HWTEST_F(BitmapTest, BitmapSerializeTest, TestSize.Level1)
385 {
386     std::unique_ptr<Bitmap> bitmap1 = std::make_unique<Bitmap>();
387     ASSERT_TRUE(bitmap1 != nullptr);
388     EXPECT_TRUE(bitmap1->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
389     std::shared_ptr<Data> data = bitmap1->Serialize();
390     ASSERT_TRUE(data != nullptr);
391     std::unique_ptr<Bitmap> bitmap2 = std::make_unique<Bitmap>();
392     ASSERT_TRUE(bitmap2->Deserialize(data));
393     ASSERT_EQ(bitmap1->GetColorType(), bitmap2->GetColorType());
394     ASSERT_EQ(bitmap1->GetAlphaType(), bitmap2->GetAlphaType());
395 }
396 
397 /**
398  * @tc.name: BitmapSetAndPixelsTest001
399  * @tc.desc:
400  * @tc.type: FUNC
401  * @tc.require:AR000GGNV3
402  * @tc.author:
403  */
404 HWTEST_F(BitmapTest, BitmapSetAndPixelsTest001, TestSize.Level1)
405 {
406     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
407     ASSERT_TRUE(bitmap != nullptr);
408     BitmapFormat* bitmapFormat1 = nullptr;
409     bitmap->SetPixels(bitmapFormat1);
410     EXPECT_EQ(bitmapFormat1, bitmap->GetPixels());
411 }
412 
413 /**
414  * @tc.name: BitmapSetAndPixelsTest002
415  * @tc.desc:
416  * @tc.type: FUNC
417  * @tc.require:AR000GGNV3
418  * @tc.author:
419  */
420 HWTEST_F(BitmapTest, BitmapSetAndPixelsTest002, TestSize.Level1)
421 {
422     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
423     ASSERT_TRUE(bitmap != nullptr);
424     BitmapFormat* bitmapFormat2 = nullptr;
425     bitmap->SetPixels(bitmapFormat2);
426     EXPECT_EQ(bitmapFormat2, bitmap->GetPixels());
427 }
428 
429 /**
430  * @tc.name: BitmapCopyPixelsTest001
431  * @tc.desc:
432  * @tc.type: FUNC
433  * @tc.require:AR000GGNV3
434  * @tc.author:
435  */
436 HWTEST_F(BitmapTest, BitmapCopyPixelsTest001, TestSize.Level1)
437 {
438     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
439     ASSERT_TRUE(bitmap != nullptr);
440     Bitmap bitmap1;
441     bitmap->CopyPixels(bitmap1, 100, 105);
442 }
443 
444 /**
445  * @tc.name: BitmapCopyPixelsTest002
446  * @tc.desc:
447  * @tc.type: FUNC
448  * @tc.require:AR000GGNV3
449  * @tc.author:
450  */
451 HWTEST_F(BitmapTest, BitmapCopyPixelsTest002, TestSize.Level1)
452 {
453     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
454     ASSERT_TRUE(bitmap != nullptr);
455     Bitmap bitmap1;
456     bitmap->CopyPixels(bitmap1, 66, 5);
457 }
458 
459 /**
460  * @tc.name: BitmapClearWithColor001
461  * @tc.desc:
462  * @tc.type: FUNC
463  * @tc.require:AR000GGNV3
464  * @tc.author:
465  */
466 HWTEST_F(BitmapTest, BitmapClearWithColor001, TestSize.Level1)
467 {
468     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
469     ASSERT_TRUE(bitmap != nullptr);
470     bitmap->ClearWithColor(COLORTYPE_UNKNOWN);
471 }
472 
473 /**
474  * @tc.name: BitmapClearWithColor002
475  * @tc.desc:
476  * @tc.type: FUNC
477  * @tc.require:AR000GGNV3
478  * @tc.author:
479  */
480 HWTEST_F(BitmapTest, BitmapClearWithColor002, TestSize.Level1)
481 {
482     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
483     ASSERT_TRUE(bitmap != nullptr);
484     bitmap->ClearWithColor(COLORTYPE_ALPHA_8);
485 }
486 
487 /**
488  * @tc.name: BitmapIsValid
489  * @tc.desc:
490  * @tc.type: FUNC
491  * @tc.require:AR000GGNV3
492  * @tc.author:
493  */
494 HWTEST_F(BitmapTest, BitmapIsValid, TestSize.Level1)
495 {
496     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
497     ASSERT_TRUE(bitmap != nullptr);
498     ASSERT_TRUE(bitmap->IsValid());
499     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
500     EXPECT_TRUE(bitmap->Build(151, 150, bitmapFormat));
501     ASSERT_FALSE(bitmap->IsValid());
502 }
503 
504 /**
505  * @tc.name: BitmapIsEmpty
506  * @tc.desc:
507  * @tc.type: FUNC
508  * @tc.require:AR000GGNV3
509  * @tc.author:
510  */
511 HWTEST_F(BitmapTest, BitmapIsEmpty, TestSize.Level1)
512 {
513     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
514     ASSERT_TRUE(bitmap != nullptr);
515     ASSERT_TRUE(bitmap->IsEmpty());
516     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
517     EXPECT_TRUE(bitmap->Build(151, 150, bitmapFormat));
518     ASSERT_FALSE(bitmap->IsEmpty());
519 }
520 
521 /**
522  * @tc.name: BitmapBitmapGetColorTest001
523  * @tc.desc:
524  * @tc.type: FUNC
525  * @tc.require:AR000GGNV3
526  * @tc.author:
527  */
528 HWTEST_F(BitmapTest, BitmapBitmapGetColorTest001, TestSize.Level1)
529 {
530     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
531     ASSERT_TRUE(bitmap != nullptr);
532     ASSERT_EQ(Color::COLOR_TRANSPARENT, bitmap->GetColor(0, 0));
533 }
534 
535 /**
536  * @tc.name: BitmapGetColorTest002
537  * @tc.desc:
538  * @tc.type: FUNC
539  * @tc.require:AR000GGNV3
540  * @tc.author:
541  */
542 HWTEST_F(BitmapTest, BitmapGetColorTest002, TestSize.Level1)
543 {
544     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
545     ASSERT_TRUE(bitmap != nullptr);
546     ASSERT_EQ(Color::COLOR_TRANSPARENT, bitmap->GetColor(1, 2));
547 }
548 
549 /**
550  * @tc.name: BitmapSetFormatTest
551  * @tc.desc:
552  * @tc.type: FUNC
553  * @tc.require:AR000GGNV3
554  * @tc.author:
555  */
556 HWTEST_F(BitmapTest, BitmapSetFormatTest, TestSize.Level1)
557 {
558     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
559     ASSERT_TRUE(bitmap != nullptr);
560     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
561     EXPECT_TRUE(bitmap->Build(111, 450, bitmapFormat));
562     bitmap->SetFormat({ ColorType::COLORTYPE_RGBA_8888, AlphaType::ALPHATYPE_UNPREMUL });
563     ASSERT_EQ(ColorType::COLORTYPE_RGBA_8888, bitmap->GetFormat().colorType);
564     ASSERT_EQ(AlphaType::ALPHATYPE_UNPREMUL, bitmap->GetFormat().alphaType);
565     bitmap->Free();
566 }
567 
568 /**
569  * @tc.name: BitmapGetFormatTest001
570  * @tc.desc:
571  * @tc.type: FUNC
572  * @tc.require:AR000GGNV3
573  * @tc.author:
574  */
575 HWTEST_F(BitmapTest, BitmapGetFormatTest001, TestSize.Level1)
576 {
577     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
578     ASSERT_TRUE(bitmap != nullptr);
579     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
580     EXPECT_TRUE(bitmap->Build(111, 450, bitmapFormat));
581     ASSERT_EQ(ColorType::COLORTYPE_ALPHA_8, bitmap->GetFormat().colorType);
582     ASSERT_EQ(AlphaType::ALPHATYPE_OPAQUE, bitmap->GetFormat().alphaType);
583     bitmap->Free();
584 }
585 
586 /**
587  * @tc.name: BitmapGetFormatTest002
588  * @tc.desc:
589  * @tc.type: FUNC
590  * @tc.require:AR000GGNV3
591  * @tc.author:
592  */
593 HWTEST_F(BitmapTest, BitmapGetFormatTest002, TestSize.Level1)
594 {
595     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
596     ASSERT_TRUE(bitmap != nullptr);
597     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
598     EXPECT_TRUE(bitmap->Build(151, 150, bitmapFormat));
599     ASSERT_EQ(ColorType::COLORTYPE_ALPHA_8, bitmap->GetFormat().colorType);
600     ASSERT_EQ(AlphaType::ALPHATYPE_OPAQUE, bitmap->GetFormat().alphaType);
601     bitmap->Free();
602 }
603 
604 /**
605  * @tc.name: BitmapSetInfoTest
606  * @tc.desc:
607  * @tc.type: FUNC
608  * @tc.require:AR000GGNV3
609  * @tc.author:
610  */
611 HWTEST_F(BitmapTest, BitmapSetInfoTest, TestSize.Level1)
612 {
613     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
614     ASSERT_TRUE(bitmap != nullptr);
615     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
616     EXPECT_TRUE(bitmap->Build(111, 450, bitmapFormat));
617     bitmap->SetInfo({ 200, 300, ColorType::COLORTYPE_RGBA_8888, AlphaType::ALPHATYPE_UNPREMUL });
618     ASSERT_EQ(ColorType::COLORTYPE_RGBA_8888, bitmap->GetFormat().colorType);
619     ASSERT_EQ(AlphaType::ALPHATYPE_UNPREMUL, bitmap->GetFormat().alphaType);
620     ASSERT_EQ(200, bitmap->GetWidth());
621     ASSERT_EQ(300, bitmap->GetHeight());
622     bitmap->Free();
623 }
624 
625 /*
626  * @tc.name: BitmapGetImageInfoTest001
627  * @tc.desc: test for bitmap GetImageInfo.
628  * @tc.type: FUNC
629  * @tc.require: AR20240104201189
630  */
631 HWTEST_F(BitmapTest, BitmapGetImageInfoTest001, TestSize.Level1)
632 {
633     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
634     ASSERT_TRUE(bitmap != nullptr);
635     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
636     const unsigned int width = 500;
637     const unsigned int height = 500;
638     EXPECT_TRUE(bitmap->Build(width, height, bitmapFormat));
639 
640     auto imageInfo = bitmap->GetImageInfo();
641     EXPECT_EQ(width, imageInfo.GetWidth());
642     EXPECT_EQ(height, imageInfo.GetHeight());
643 
644     ImageInfo info{width, height, ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE};
645     bitmap->SetInfo(info);
646     auto info2 = bitmap->GetImageInfo();
647     EXPECT_EQ(width, info2.GetWidth());
648     EXPECT_EQ(height, info2.GetHeight());
649 
650     bitmap->Free();
651 }
652 
653 /*
654  * @tc.name: BitmapTryAllocPixelsTest001
655  * @tc.desc: test for bitmap TryAllocPixels.
656  * @tc.type: FUNC
657  * @tc.require: AR20240104201189
658  */
659 HWTEST_F(BitmapTest, BitmapTryAllocPixelsTest001, TestSize.Level1)
660 {
661     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
662     ASSERT_TRUE(bitmap != nullptr);
663     ImageInfo imageInfo = {500, 500, ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE};
664     EXPECT_TRUE(bitmap->TryAllocPixels(imageInfo));
665 
666     imageInfo = {500, 500, ColorType::COLORTYPE_UNKNOWN, AlphaType::ALPHATYPE_OPAQUE};
667     EXPECT_TRUE(bitmap->TryAllocPixels(imageInfo));
668 
669     imageInfo = {500, 500, ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_UNKNOWN};
670     EXPECT_FALSE(bitmap->TryAllocPixels(imageInfo));
671 }
672 
673 /**
674  * @tc.name: ExtractSubset001
675  * @tc.desc: Test ExtractSubset
676  * @tc.type: FUNC
677  * @tc.require: AR20240104201189
678  */
679 HWTEST_F(BitmapTest, ExtractSubset001, TestSize.Level1)
680 {
681     Bitmap left;
682     Rect subset = Rect(0, 0, 50, 50); // 50: right, bottom
683     Bitmap bitmap;
684     ASSERT_TRUE(!bitmap.ExtractSubset(left, subset));
685     bitmap.Free();
686 }
687 
688 /**
689  * @tc.name: ReadPixels001
690  * @tc.desc: Test ReadPixels
691  * @tc.type: FUNC
692  * @tc.require:AR000GGNV3
693  * @tc.author:
694  */
695 HWTEST_F(BitmapTest, ReadPixels001, TestSize.Level1)
696 {
697     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
698     ASSERT_TRUE(bitmap != nullptr);
699     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
700     EXPECT_TRUE(bitmap->Build(200, 200, bitmapFormat)); // 200: width height
701     ImageInfo imageInfo{50, 50, ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE}; // 50: width height
702     void* dstPixels = nullptr;
703     ASSERT_TRUE(!bitmap->ReadPixels(imageInfo, dstPixels, 200, 0, 0)); // 200: dstRowBytes
704     bitmap->Free();
705 }
706 
707 /**
708  * @tc.name: ComputeByteSize001
709  * @tc.desc: Test ComputeByteSize
710  * @tc.type: FUNC
711  * @tc.require:AR000GGNV3
712  * @tc.author:
713  */
714 HWTEST_F(BitmapTest, ComputeByteSize001, TestSize.Level1)
715 {
716     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
717     ASSERT_TRUE(bitmap != nullptr);
718     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
719     EXPECT_TRUE(bitmap->Build(200, 200, bitmapFormat)); // 200: width height
720     ASSERT_TRUE(bitmap->ComputeByteSize() >= 0);
721     bitmap->Free();
722 }
723 
724 /**
725  * @tc.name: Immutable001
726  * @tc.desc: Test IsImmutable and SetImmutable
727  * @tc.type: FUNC
728  * @tc.require:AR000GGNV3
729  * @tc.author:
730  */
731 HWTEST_F(BitmapTest, Immutable001, TestSize.Level1)
732 {
733     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
734     ASSERT_TRUE(bitmap != nullptr);
735     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
736     EXPECT_TRUE(bitmap->Build(200, 200, bitmapFormat)); // 200: width height
737     bitmap->SetImmutable();
738     ASSERT_TRUE(bitmap->IsImmutable());
739     bitmap->Free();
740 }
741 
742 /**
743  * @tc.name: IsEmpty001
744  * @tc.desc: Test IsEmpty
745  * @tc.type: FUNC
746  * @tc.require:AR000GGNV3
747  * @tc.author:
748  */
749 HWTEST_F(BitmapTest, IsEmpty001, TestSize.Level1)
750 {
751     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
752     ASSERT_TRUE(bitmap != nullptr);
753     ASSERT_TRUE(bitmap->IsEmpty());
754     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
755     EXPECT_TRUE(bitmap->Build(200, 200, bitmapFormat)); // 200: width height
756     ASSERT_TRUE(!bitmap->IsEmpty());
757     bitmap->Free();
758 }
759 
760 /**
761  * @tc.name: GetPixmap001
762  * @tc.desc: Test GetPixmap
763  * @tc.type: FUNC
764  * @tc.require:AR000GGNV3
765  * @tc.author:
766  */
767 HWTEST_F(BitmapTest, GetPixmap001, TestSize.Level1)
768 {
769     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
770     ASSERT_TRUE(bitmap != nullptr);
771     const unsigned int width = 500;
772     const unsigned int height = 500;
773     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
774     EXPECT_TRUE(bitmap->Build(width, height, bitmapFormat));
775     ASSERT_TRUE(width == bitmap->GetPixmap().GetWidth());
776     bitmap->Free();
777 }
778 
779 /**
780  * @tc.name: Serialize001
781  * @tc.desc: Test Serialize
782  * @tc.type: FUNC
783  * @tc.require:AR000GGNV3
784  * @tc.author:
785  */
786 HWTEST_F(BitmapTest, Serialize001, TestSize.Level1)
787 {
788     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
789     ASSERT_TRUE(bitmap != nullptr);
790     const unsigned int width = 500;
791     const unsigned int height = 500;
792     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
793     EXPECT_TRUE(bitmap->Build(width, height, bitmapFormat));
794     ASSERT_TRUE(bitmap->Serialize() != nullptr);
795     bitmap->Free();
796 }
797 
798 /**
799  * @tc.name: SetFormat001
800  * @tc.desc: Test SetFormat
801  * @tc.type: FUNC
802  * @tc.require:AR000GGNV3
803  * @tc.author:
804  */
805 HWTEST_F(BitmapTest, SetFormat001, TestSize.Level1)
806 {
807     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
808     ASSERT_TRUE(bitmap != nullptr);
809     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
810     bitmap->SetFormat(bitmapFormat);
811     ASSERT_EQ(ColorType::COLORTYPE_ALPHA_8, bitmap->GetFormat().colorType);
812     ASSERT_EQ(AlphaType::ALPHATYPE_OPAQUE, bitmap->GetFormat().alphaType);
813     bitmap->Free();
814 }
815 
816 /**
817  * @tc.name: GetColorType001
818  * @tc.desc: Test GetColorType
819  * @tc.type: FUNC
820  * @tc.require:AR000GGNV3
821  * @tc.author:
822  */
823 HWTEST_F(BitmapTest, GetColorType001, TestSize.Level1)
824 {
825     // The best way to Build Bitmap.
826     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
827     ASSERT_TRUE(bitmap != nullptr);
828     BitmapFormat bitmapFormat = { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE };
829     EXPECT_TRUE(bitmap->Build(200, 200, bitmapFormat)); // 200: width height
830     EXPECT_TRUE(bitmap->GetColorType() == ColorType::COLORTYPE_ALPHA_8);
831 }
832 } // namespace Drawing
833 } // namespace Rosen
834 } // namespace OHOS