1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, Hardware
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include "gtest/gtest.h"
17f6603c60Sopenharmony_ci
18f6603c60Sopenharmony_ci#include "drawing_bitmap.h"
19f6603c60Sopenharmony_ci#include "drawing_types.h"
20f6603c60Sopenharmony_ci
21f6603c60Sopenharmony_ciusing namespace testing;
22f6603c60Sopenharmony_ciusing namespace testing::ext;
23f6603c60Sopenharmony_ci
24f6603c60Sopenharmony_cinamespace OHOS {
25f6603c60Sopenharmony_cinamespace Rosen {
26f6603c60Sopenharmony_cinamespace Drawing {
27f6603c60Sopenharmony_ciclass NativeDrawingBitmapTest : public testing::Test {
28f6603c60Sopenharmony_cipublic:
29f6603c60Sopenharmony_ci    static void SetUpTestCase();
30f6603c60Sopenharmony_ci    static void TearDownTestCase();
31f6603c60Sopenharmony_ci    void SetUp() override;
32f6603c60Sopenharmony_ci    void TearDown() override;
33f6603c60Sopenharmony_ci    protected:
34f6603c60Sopenharmony_ci        OH_Drawing_Bitmap* bitmap_ = nullptr;
35f6603c60Sopenharmony_ci};
36f6603c60Sopenharmony_ci
37f6603c60Sopenharmony_civoid NativeDrawingBitmapTest::SetUpTestCase() {}
38f6603c60Sopenharmony_civoid NativeDrawingBitmapTest::TearDownTestCase() {}
39f6603c60Sopenharmony_civoid NativeDrawingBitmapTest::SetUp()
40f6603c60Sopenharmony_ci{
41f6603c60Sopenharmony_ci    bitmap_ = OH_Drawing_BitmapCreate();
42f6603c60Sopenharmony_ci    ASSERT_NE(bitmap_, nullptr);
43f6603c60Sopenharmony_ci}
44f6603c60Sopenharmony_ci
45f6603c60Sopenharmony_civoid NativeDrawingBitmapTest::TearDown()
46f6603c60Sopenharmony_ci{
47f6603c60Sopenharmony_ci    if (bitmap_ != nullptr) {
48f6603c60Sopenharmony_ci        OH_Drawing_BitmapDestroy(bitmap_);
49f6603c60Sopenharmony_ci        bitmap_ = nullptr;
50f6603c60Sopenharmony_ci    }
51f6603c60Sopenharmony_ci}
52f6603c60Sopenharmony_ci
53f6603c60Sopenharmony_ci/*
54f6603c60Sopenharmony_ci * @tc.name: NativeDrawingBitmapTest_bitmapBuild001
55f6603c60Sopenharmony_ci * @tc.desc: test for drawing_bitmap build.
56f6603c60Sopenharmony_ci * @tc.size  : MediumTest
57f6603c60Sopenharmony_ci * @tc.type  : Function
58f6603c60Sopenharmony_ci * @tc.level : Level 1
59f6603c60Sopenharmony_ci */
60f6603c60Sopenharmony_ciHWTEST_F(NativeDrawingBitmapTest, NativeDrawingBitmapTest_bitmapBuild001, TestSize.Level1)
61f6603c60Sopenharmony_ci{
62f6603c60Sopenharmony_ci    const unsigned int width = 500;
63f6603c60Sopenharmony_ci    const unsigned int height = 500;
64f6603c60Sopenharmony_ci    OH_Drawing_BitmapFormat bitmapFormat { COLOR_FORMAT_ALPHA_8, ALPHA_FORMAT_PREMUL };
65f6603c60Sopenharmony_ci    OH_Drawing_BitmapBuild(bitmap_, width, height, &bitmapFormat);
66f6603c60Sopenharmony_ci    EXPECT_EQ(width, OH_Drawing_BitmapGetWidth(bitmap_));
67f6603c60Sopenharmony_ci    EXPECT_EQ(height, OH_Drawing_BitmapGetHeight(bitmap_));
68f6603c60Sopenharmony_ci}
69f6603c60Sopenharmony_ci
70f6603c60Sopenharmony_ci/*
71f6603c60Sopenharmony_ci * @tc.name: NativeDrawingBitmapTest_bitmapBuild002
72f6603c60Sopenharmony_ci * @tc.desc: test for drawing_bitmap build.
73f6603c60Sopenharmony_ci * @tc.size  : MediumTest
74f6603c60Sopenharmony_ci * @tc.type  : Function
75f6603c60Sopenharmony_ci * @tc.level : Level 1
76f6603c60Sopenharmony_ci */
77f6603c60Sopenharmony_ciHWTEST_F(NativeDrawingBitmapTest, NativeDrawingBitmapTest_bitmapBuild002, TestSize.Level1)
78f6603c60Sopenharmony_ci{
79f6603c60Sopenharmony_ci    const unsigned int width = 0;
80f6603c60Sopenharmony_ci    const unsigned int height = 0;
81f6603c60Sopenharmony_ci    OH_Drawing_BitmapFormat bitmapFormat { COLOR_FORMAT_RGB_565, ALPHA_FORMAT_OPAQUE };
82f6603c60Sopenharmony_ci    OH_Drawing_BitmapBuild(bitmap_, width, height, &bitmapFormat);
83f6603c60Sopenharmony_ci    EXPECT_EQ(width, OH_Drawing_BitmapGetWidth(bitmap_));
84f6603c60Sopenharmony_ci    EXPECT_EQ(height, OH_Drawing_BitmapGetHeight(bitmap_));
85f6603c60Sopenharmony_ci}
86f6603c60Sopenharmony_ci
87f6603c60Sopenharmony_ci/*
88f6603c60Sopenharmony_ci * @tc.name: NativeDrawingBitmapTest_bitmapBuild003
89f6603c60Sopenharmony_ci * @tc.desc: test for drawing_bitmap build.
90f6603c60Sopenharmony_ci * @tc.size  : MediumTest
91f6603c60Sopenharmony_ci * @tc.type  : Function
92f6603c60Sopenharmony_ci * @tc.level : Level 1
93f6603c60Sopenharmony_ci */
94f6603c60Sopenharmony_ciHWTEST_F(NativeDrawingBitmapTest, NativeDrawingBitmapTest_bitmapBuild003, TestSize.Level1)
95f6603c60Sopenharmony_ci{
96f6603c60Sopenharmony_ci    const unsigned int width = 500;
97f6603c60Sopenharmony_ci    const unsigned int height = 500;
98f6603c60Sopenharmony_ci    OH_Drawing_BitmapFormat bitmapFormat { COLOR_FORMAT_ARGB_4444, ALPHA_FORMAT_UNPREMUL };
99f6603c60Sopenharmony_ci    OH_Drawing_BitmapBuild(bitmap_, width, height, &bitmapFormat);
100f6603c60Sopenharmony_ci    EXPECT_EQ(width, OH_Drawing_BitmapGetWidth(bitmap_));
101f6603c60Sopenharmony_ci    EXPECT_EQ(height, OH_Drawing_BitmapGetHeight(bitmap_));
102f6603c60Sopenharmony_ci    EXPECT_EQ(OH_Drawing_BitmapGetPixels(bitmap_) == nullptr, false);
103f6603c60Sopenharmony_ci}
104f6603c60Sopenharmony_ci
105f6603c60Sopenharmony_ci/*
106f6603c60Sopenharmony_ci * @tc.name: NativeDrawingBitmapTest_bitmapBuild004
107f6603c60Sopenharmony_ci * @tc.desc: test for drawing_bitmap build.
108f6603c60Sopenharmony_ci * @tc.size  : MediumTest
109f6603c60Sopenharmony_ci * @tc.type  : Function
110f6603c60Sopenharmony_ci * @tc.level : Level 1
111f6603c60Sopenharmony_ci */
112f6603c60Sopenharmony_ciHWTEST_F(NativeDrawingBitmapTest, NativeDrawingBitmapTest_bitmapBuild004, TestSize.Level1)
113f6603c60Sopenharmony_ci{
114f6603c60Sopenharmony_ci    const unsigned int width = 500;
115f6603c60Sopenharmony_ci    const unsigned int height = 500;
116f6603c60Sopenharmony_ci    OH_Drawing_BitmapFormat bitmapFormat { COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_UNPREMUL };
117f6603c60Sopenharmony_ci    OH_Drawing_BitmapBuild(bitmap_, width, height, &bitmapFormat);
118f6603c60Sopenharmony_ci    EXPECT_EQ(width, OH_Drawing_BitmapGetWidth(bitmap_));
119f6603c60Sopenharmony_ci    EXPECT_EQ(height, OH_Drawing_BitmapGetHeight(bitmap_));
120f6603c60Sopenharmony_ci}
121f6603c60Sopenharmony_ci
122f6603c60Sopenharmony_ci/*
123f6603c60Sopenharmony_ci * @tc.name: NativeDrawingBitmapTest_bitmapCreateFromPixels005
124f6603c60Sopenharmony_ci * @tc.desc: test for OH_Drawing_BitmapCreateFromPixels.
125f6603c60Sopenharmony_ci * @tc.size  : MediumTest
126f6603c60Sopenharmony_ci * @tc.type  : Function
127f6603c60Sopenharmony_ci * @tc.level : Level 1
128f6603c60Sopenharmony_ci */
129f6603c60Sopenharmony_ciHWTEST_F(NativeDrawingBitmapTest, NativeDrawingBitmapTest_bitmapCreateFromPixels005, TestSize.Level1)
130f6603c60Sopenharmony_ci{
131f6603c60Sopenharmony_ci    OH_Drawing_Image_Info imageInfo;
132f6603c60Sopenharmony_ci    OH_Drawing_Bitmap* bitmap = OH_Drawing_BitmapCreate();
133f6603c60Sopenharmony_ci    EXPECT_NE(bitmap, nullptr);
134f6603c60Sopenharmony_ci    OH_Drawing_BitmapFormat cFormat{COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
135f6603c60Sopenharmony_ci    constexpr uint32_t width = 200;
136f6603c60Sopenharmony_ci    constexpr uint32_t height = 200;
137f6603c60Sopenharmony_ci    OH_Drawing_BitmapBuild(bitmap, width, height, &cFormat);
138f6603c60Sopenharmony_ci    void* pixels = OH_Drawing_BitmapGetPixels(bitmap);
139f6603c60Sopenharmony_ci    EXPECT_NE(pixels, nullptr);
140f6603c60Sopenharmony_ci    uint32_t rowBytes = width * height * 4;
141f6603c60Sopenharmony_ci    bitmap_ = OH_Drawing_BitmapCreateFromPixels(&imageInfo, pixels, rowBytes);
142f6603c60Sopenharmony_ci    EXPECT_NE(bitmap_, nullptr);
143f6603c60Sopenharmony_ci    bitmap_ = OH_Drawing_BitmapCreateFromPixels(&imageInfo, pixels, 0);
144f6603c60Sopenharmony_ci    EXPECT_EQ(bitmap_, nullptr);
145f6603c60Sopenharmony_ci    bitmap_ = OH_Drawing_BitmapCreateFromPixels(&imageInfo, nullptr, 0);
146f6603c60Sopenharmony_ci    EXPECT_EQ(bitmap_, nullptr);
147f6603c60Sopenharmony_ci    bitmap_ = OH_Drawing_BitmapCreateFromPixels(nullptr, nullptr, 0);
148f6603c60Sopenharmony_ci    EXPECT_EQ(bitmap_, nullptr);
149f6603c60Sopenharmony_ci}
150f6603c60Sopenharmony_ci/*
151f6603c60Sopenharmony_ci * @tc.name: NativeDrawingBitmapTest_bitmapGetImageInfo006
152f6603c60Sopenharmony_ci * @tc.desc: test for drawing_bitmapGetImageInfo.
153f6603c60Sopenharmony_ci * @tc.size  : MediumTest
154f6603c60Sopenharmony_ci * @tc.type  : Function
155f6603c60Sopenharmony_ci * @tc.level : Level 1
156f6603c60Sopenharmony_ci */
157f6603c60Sopenharmony_ciHWTEST_F(NativeDrawingBitmapTest, NativeDrawingBitmapTest_bitmapGetImageInfo006, TestSize.Level1)
158f6603c60Sopenharmony_ci{
159f6603c60Sopenharmony_ci    const unsigned int width = 500;
160f6603c60Sopenharmony_ci    const unsigned int height = 500;
161f6603c60Sopenharmony_ci    OH_Drawing_BitmapFormat bitmapFormat { COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_UNPREMUL };
162f6603c60Sopenharmony_ci    OH_Drawing_BitmapBuild(bitmap_, width, height, &bitmapFormat);
163f6603c60Sopenharmony_ci    OH_Drawing_Image_Info* imageInfo = new OH_Drawing_Image_Info();
164f6603c60Sopenharmony_ci    OH_Drawing_BitmapGetImageInfo(bitmap_, imageInfo);
165f6603c60Sopenharmony_ci    EXPECT_EQ(width, imageInfo->width);
166f6603c60Sopenharmony_ci    EXPECT_EQ(height, imageInfo->height);
167f6603c60Sopenharmony_ci}
168f6603c60Sopenharmony_ci
169f6603c60Sopenharmony_ci/*
170f6603c60Sopenharmony_ci * @tc.name: NativeDrawingBitmapTest_BitmapReadPixels007
171f6603c60Sopenharmony_ci * @tc.desc: test for drawing_BitmapReadPixels.
172f6603c60Sopenharmony_ci * @tc.size  : MediumTest
173f6603c60Sopenharmony_ci * @tc.type  : Function
174f6603c60Sopenharmony_ci * @tc.level : Level 1
175f6603c60Sopenharmony_ci */
176f6603c60Sopenharmony_ciHWTEST_F(NativeDrawingBitmapTest, NativeDrawingBitmapTest_BitmapReadPixels007, TestSize.Level1)
177f6603c60Sopenharmony_ci{
178f6603c60Sopenharmony_ci    const unsigned int width = 500;
179f6603c60Sopenharmony_ci    const unsigned int height = 500;
180f6603c60Sopenharmony_ci    OH_Drawing_BitmapFormat bitmapFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_UNPREMUL};
181f6603c60Sopenharmony_ci    OH_Drawing_BitmapBuild(bitmap_, width, height, &bitmapFormat);
182f6603c60Sopenharmony_ci    OH_Drawing_Image_Info imageInfo {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_UNPREMUL};
183f6603c60Sopenharmony_ci    void* pixels = new uint32_t[width * height];
184f6603c60Sopenharmony_ci    bool res = OH_Drawing_BitmapReadPixels(nullptr, nullptr, nullptr, width * 4, 0, 0);
185f6603c60Sopenharmony_ci    EXPECT_EQ(res, false);
186f6603c60Sopenharmony_ci    res = OH_Drawing_BitmapReadPixels(nullptr, &imageInfo, pixels, width * 4, 0, 0);
187f6603c60Sopenharmony_ci    EXPECT_EQ(res, false);
188f6603c60Sopenharmony_ci    res = OH_Drawing_BitmapReadPixels(bitmap_, nullptr, pixels, width * 4, 0, 0);
189f6603c60Sopenharmony_ci    EXPECT_EQ(res, false);
190f6603c60Sopenharmony_ci    res = OH_Drawing_BitmapReadPixels(bitmap_, &imageInfo, nullptr, width * 4, 0, 0);
191f6603c60Sopenharmony_ci    EXPECT_EQ(res, false);
192f6603c60Sopenharmony_ci    res = OH_Drawing_BitmapReadPixels(bitmap_, &imageInfo, pixels, width * 4, 0, 0);
193f6603c60Sopenharmony_ci    EXPECT_EQ(res, true);
194f6603c60Sopenharmony_ci    if (pixels != nullptr) {
195f6603c60Sopenharmony_ci        delete[] reinterpret_cast<uint32_t*>(pixels);
196f6603c60Sopenharmony_ci        pixels = nullptr;
197f6603c60Sopenharmony_ci    }
198f6603c60Sopenharmony_ci}
199f6603c60Sopenharmony_ci
200f6603c60Sopenharmony_ci/*
201f6603c60Sopenharmony_ci * @tc.name: NativeDrawingBitmapTest_GetColorFormat008
202f6603c60Sopenharmony_ci * @tc.desc: test for drawing_BitmapGetColorFormat.
203f6603c60Sopenharmony_ci * @tc.size  : MediumTest
204f6603c60Sopenharmony_ci * @tc.type  : Function
205f6603c60Sopenharmony_ci * @tc.level : Level 1
206f6603c60Sopenharmony_ci */
207f6603c60Sopenharmony_ciHWTEST_F(NativeDrawingBitmapTest, NativeDrawingBitmapTest_GetColorFormat008, TestSize.Level1)
208f6603c60Sopenharmony_ci{
209f6603c60Sopenharmony_ci    const unsigned int width = 500;
210f6603c60Sopenharmony_ci    const unsigned int height = 500;
211f6603c60Sopenharmony_ci    OH_Drawing_ColorFormat formats[] = {
212f6603c60Sopenharmony_ci        COLOR_FORMAT_UNKNOWN,
213f6603c60Sopenharmony_ci        COLOR_FORMAT_ALPHA_8,
214f6603c60Sopenharmony_ci        COLOR_FORMAT_RGB_565,
215f6603c60Sopenharmony_ci        COLOR_FORMAT_ARGB_4444,
216f6603c60Sopenharmony_ci        COLOR_FORMAT_RGBA_8888,
217f6603c60Sopenharmony_ci        COLOR_FORMAT_BGRA_8888
218f6603c60Sopenharmony_ci    };
219f6603c60Sopenharmony_ci
220f6603c60Sopenharmony_ci    OH_Drawing_AlphaFormat alphaFormats[] = {
221f6603c60Sopenharmony_ci        ALPHA_FORMAT_UNKNOWN,
222f6603c60Sopenharmony_ci        ALPHA_FORMAT_OPAQUE,
223f6603c60Sopenharmony_ci        ALPHA_FORMAT_PREMUL,
224f6603c60Sopenharmony_ci        ALPHA_FORMAT_UNPREMUL
225f6603c60Sopenharmony_ci    };
226f6603c60Sopenharmony_ci    OH_Drawing_ColorFormat colorFormat_;
227f6603c60Sopenharmony_ci    for (int i = 1; i < 6; i++) {
228f6603c60Sopenharmony_ci        OH_Drawing_Bitmap *bitmap = OH_Drawing_BitmapCreate();
229f6603c60Sopenharmony_ci        OH_Drawing_BitmapFormat bitmapFormat = {formats[i], alphaFormats[2]};
230f6603c60Sopenharmony_ci        OH_Drawing_BitmapBuild(bitmap, width, height, &bitmapFormat);
231f6603c60Sopenharmony_ci        if (bitmap == nullptr) {
232f6603c60Sopenharmony_ci            colorFormat_ = OH_Drawing_BitmapGetColorFormat(bitmap);
233f6603c60Sopenharmony_ci            EXPECT_EQ(colorFormat_, formats[0]);
234f6603c60Sopenharmony_ci        }
235f6603c60Sopenharmony_ci        colorFormat_ = OH_Drawing_BitmapGetColorFormat(bitmap);
236f6603c60Sopenharmony_ci        EXPECT_EQ(colorFormat_, formats[i]);
237f6603c60Sopenharmony_ci    }
238f6603c60Sopenharmony_ci}
239f6603c60Sopenharmony_ci
240f6603c60Sopenharmony_ci/*
241f6603c60Sopenharmony_ci * @tc.name: NativeDrawingBitmapTest_GetAlphaFormat009
242f6603c60Sopenharmony_ci * @tc.desc: test for drawing_BitmapGetAlphaFormat.
243f6603c60Sopenharmony_ci * @tc.size  : MediumTest
244f6603c60Sopenharmony_ci * @tc.type  : Function
245f6603c60Sopenharmony_ci * @tc.level : Level 1
246f6603c60Sopenharmony_ci */
247f6603c60Sopenharmony_ciHWTEST_F(NativeDrawingBitmapTest, NativeDrawingBitmapTest_GetAlphaFormat009, TestSize.Level1)
248f6603c60Sopenharmony_ci{
249f6603c60Sopenharmony_ci    const unsigned int width = 500;
250f6603c60Sopenharmony_ci    const unsigned int height = 500;
251f6603c60Sopenharmony_ci    OH_Drawing_ColorFormat formats[] = {
252f6603c60Sopenharmony_ci        COLOR_FORMAT_UNKNOWN,
253f6603c60Sopenharmony_ci        COLOR_FORMAT_ALPHA_8,
254f6603c60Sopenharmony_ci        COLOR_FORMAT_RGB_565,
255f6603c60Sopenharmony_ci        COLOR_FORMAT_ARGB_4444,
256f6603c60Sopenharmony_ci        COLOR_FORMAT_RGBA_8888,
257f6603c60Sopenharmony_ci        COLOR_FORMAT_BGRA_8888
258f6603c60Sopenharmony_ci    };
259f6603c60Sopenharmony_ci
260f6603c60Sopenharmony_ci    OH_Drawing_AlphaFormat alphaFormats[] = {
261f6603c60Sopenharmony_ci        ALPHA_FORMAT_UNKNOWN,
262f6603c60Sopenharmony_ci        ALPHA_FORMAT_OPAQUE,
263f6603c60Sopenharmony_ci        ALPHA_FORMAT_PREMUL,
264f6603c60Sopenharmony_ci        ALPHA_FORMAT_UNPREMUL
265f6603c60Sopenharmony_ci    };
266f6603c60Sopenharmony_ci    OH_Drawing_AlphaFormat alphaFormat_;
267f6603c60Sopenharmony_ci    for (int i = 1; i < 4; i++) {
268f6603c60Sopenharmony_ci        OH_Drawing_Bitmap *bitmap = OH_Drawing_BitmapCreate();
269f6603c60Sopenharmony_ci        OH_Drawing_BitmapFormat bitmapFormat = {formats[3], alphaFormats[i]};
270f6603c60Sopenharmony_ci        OH_Drawing_BitmapBuild(bitmap, width, height, &bitmapFormat);
271f6603c60Sopenharmony_ci        if (bitmap == nullptr) {
272f6603c60Sopenharmony_ci            alphaFormat_ = OH_Drawing_BitmapGetAlphaFormat(bitmap);
273f6603c60Sopenharmony_ci            EXPECT_EQ(alphaFormat_, alphaFormats[0]);
274f6603c60Sopenharmony_ci        }
275f6603c60Sopenharmony_ci        alphaFormat_ = OH_Drawing_BitmapGetAlphaFormat(bitmap);
276f6603c60Sopenharmony_ci        EXPECT_EQ(alphaFormat_, alphaFormats[i]);
277f6603c60Sopenharmony_ci    }
278f6603c60Sopenharmony_ci}
279f6603c60Sopenharmony_ci} // namespace Drawing
280f6603c60Sopenharmony_ci} // namespace Rosen
281f6603c60Sopenharmony_ci} // namespace OHOS