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