1/*
2 * Copyright (c) 2022-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, 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 "drawing_brush.h"
19#include "drawing_color.h"
20#include "drawing_filter.h"
21#include "drawing_mask_filter.h"
22#include "drawing_rect.h"
23#include "drawing_shadow_layer.h"
24#include "drawing_color_filter.h"
25#include "effect/color_filter.h"
26#include "effect/filter.h"
27
28using namespace testing;
29using namespace testing::ext;
30
31namespace OHOS {
32namespace Rosen {
33namespace Drawing {
34class NativeDrawingBrushTest : public testing::Test {
35public:
36    static void SetUpTestCase();
37    static void TearDownTestCase();
38    void SetUp() override;
39    void TearDown() override;
40};
41
42void NativeDrawingBrushTest::SetUpTestCase() {}
43void NativeDrawingBrushTest::TearDownTestCase() {}
44void NativeDrawingBrushTest::SetUp() {}
45void NativeDrawingBrushTest::TearDown() {}
46static Filter* CastToFilter(OH_Drawing_Filter* cFilter)
47{
48    return reinterpret_cast<Filter*>(cFilter);
49}
50
51/*
52 * @tc.name: NativeDrawingBrushTest_brushCreate001
53 * @tc.desc: test for create brush and destroy brush.
54 * @tc.size  : MediumTest
55 * @tc.type  : Function
56 * @tc.level : Level 1
57 */
58HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushCreate001, TestSize.Level1)
59{
60    OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
61    EXPECT_EQ(brush == nullptr, false);
62    OH_Drawing_BrushDestroy(brush);
63}
64
65/*
66 * @tc.name: NativeDrawingBrushTest_brushSetColor002
67 * @tc.desc: test for the set methods of brush.
68 * @tc.size  : MediumTest
69 * @tc.type  : Function
70 * @tc.level : Level 1
71 */
72HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushSetColor002, TestSize.Level1)
73{
74    OH_Drawing_Brush* brush1 = OH_Drawing_BrushCreate();
75    OH_Drawing_BrushSetAntiAlias(brush1, false);
76    EXPECT_EQ(OH_Drawing_BrushIsAntiAlias(brush1), false);
77    OH_Drawing_BrushSetColor(brush1, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
78    EXPECT_EQ(OH_Drawing_BrushGetColor(brush1), 0xFFFF0000);
79    constexpr uint8_t alpha = 128;
80    OH_Drawing_BrushSetAlpha(brush1, alpha);
81    EXPECT_EQ(OH_Drawing_BrushGetAlpha(brush1), alpha);
82}
83
84/*
85 * @tc.name: NativeDrawingBrushTest_brushSetBlendMode003
86 * @tc.desc: test for SetBlendMode.
87 * @tc.size  : MediumTest
88 * @tc.type  : Function
89 * @tc.level : Level 1
90 */
91HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushSetBlendMode003, TestSize.Level1)
92{
93    OH_Drawing_BrushSetBlendMode(nullptr, OH_Drawing_BlendMode::BLEND_MODE_CLEAR);
94    OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
95    EXPECT_NE(brush, nullptr);
96    OH_Drawing_BrushSetBlendMode(brush, OH_Drawing_BlendMode::BLEND_MODE_CLEAR);
97}
98
99/*
100 * @tc.name: NativeDrawingBrushTest_brushReset004
101 * @tc.desc: test for the reset methods of brush.
102 * @tc.size  : MediumTest
103 * @tc.type  : Function
104 * @tc.level : Level 1
105 */
106HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushReset004, TestSize.Level1)
107{
108    OH_Drawing_Brush* brush1 = OH_Drawing_BrushCreate();
109    OH_Drawing_BrushSetAntiAlias(brush1, true);
110    OH_Drawing_BrushSetColor(brush1, OH_Drawing_ColorSetArgb(0x00, 0xFF, 0x00, 0xFF));
111    constexpr uint8_t alpha = 128;
112    OH_Drawing_BrushSetAlpha(brush1, alpha);
113
114    OH_Drawing_BrushReset(brush1);
115    EXPECT_EQ(OH_Drawing_BrushIsAntiAlias(brush1), false);
116    EXPECT_EQ(OH_Drawing_BrushGetColor(brush1), 0xFF000000);
117    EXPECT_EQ(OH_Drawing_BrushGetAlpha(brush1), 0xFF);
118
119    OH_Drawing_BrushDestroy(brush1);
120}
121
122/*
123 * @tc.name: NativeDrawingBrushTest_brushGetFilter005
124 * @tc.desc: gets the filter from a brush.
125 * @tc.size  : MediumTest
126 * @tc.type  : Function
127 * @tc.level : Level 1
128 */
129HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushGetFilter005, TestSize.Level1)
130{
131    OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
132    EXPECT_NE(brush, nullptr);
133    OH_Drawing_Filter* cFilter_ = OH_Drawing_FilterCreate();
134    EXPECT_NE(cFilter_, nullptr);
135    OH_Drawing_Filter* tmpFilter_ = OH_Drawing_FilterCreate();
136    EXPECT_NE(tmpFilter_, nullptr);
137
138    OH_Drawing_ColorFilter* colorFilterTmp = OH_Drawing_ColorFilterCreateLinearToSrgbGamma();
139
140    OH_Drawing_FilterSetColorFilter(cFilter_, nullptr);
141    OH_Drawing_FilterGetColorFilter(cFilter_, colorFilterTmp);
142    EXPECT_EQ((reinterpret_cast<ColorFilter*>(colorFilterTmp))->GetType(),
143        ColorFilter::FilterType::NO_TYPE);
144
145    OH_Drawing_ColorFilter* cColorFilter_ = OH_Drawing_ColorFilterCreateBlendMode(0xFF0000FF, BLEND_MODE_COLOR);
146    OH_Drawing_FilterSetColorFilter(cFilter_, cColorFilter_);
147    OH_Drawing_BrushSetFilter(brush, cFilter_);
148    OH_Drawing_BrushGetFilter(brush, tmpFilter_);
149
150    EXPECT_NE(CastToFilter(tmpFilter_)->GetColorFilter(), nullptr);
151    EXPECT_EQ(CastToFilter(tmpFilter_)->GetColorFilter()->GetType(), ColorFilter::FilterType::BLEND_MODE);
152    OH_Drawing_FilterDestroy(cFilter_);
153    OH_Drawing_FilterDestroy(tmpFilter_);
154    OH_Drawing_ColorFilterDestroy(cColorFilter_);
155    OH_Drawing_ColorFilterDestroy(colorFilterTmp);
156    OH_Drawing_BrushDestroy(brush);
157}
158/*
159 * @tc.name: NativeDrawingBrushTest_brushSetShadowLayer006
160 * @tc.desc: gets the filter from a brush.
161 * @tc.size  : MediumTest
162 * @tc.type  : Function
163 * @tc.level : Level 1
164 */
165HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushSetShadowLayer006, TestSize.Level1)
166{
167    // blurRadius:-3.f, offset:(-3.f, 3.f), shadowColor:green
168    OH_Drawing_ShadowLayer* shadow = OH_Drawing_ShadowLayerCreate(-3.f, -3.f, 3.f, 0xFF00FF00);
169    EXPECT_EQ(shadow, nullptr);
170    OH_Drawing_ShadowLayerDestroy(nullptr);
171    // blurRadius:3.f, offset:(-3.f, 3.f), shadowColor:green
172    OH_Drawing_ShadowLayer* shadowLayer = OH_Drawing_ShadowLayerCreate(3.f, -3.f, 3.f, 0xFF00FF00);
173    EXPECT_NE(shadowLayer, nullptr);
174    OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
175    EXPECT_NE(brush, nullptr);
176    OH_Drawing_BrushSetShadowLayer(nullptr, shadowLayer);
177    OH_Drawing_BrushSetShadowLayer(brush, nullptr);
178    OH_Drawing_BrushSetShadowLayer(brush, shadowLayer);
179    OH_Drawing_ShadowLayerDestroy(shadowLayer);
180    OH_Drawing_BrushDestroy(brush);
181}
182} // namespace Drawing
183} // namespace Rosen
184} // namespace OHOS