1 /*
2 * Copyright (c) 2025 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 "drawing_font_collection.h"
17 #include "drawing_text_typography.h"
18 #include "gtest/gtest.h"
19 #include "typography.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23 using namespace OHOS::Rosen;
24
25 namespace OHOS {
26 namespace {
27 const double DEFAULT_FONT_SIZE = 50;
28 const char* DEFAULT_TEXT = "text";
29 } // namespace
30
31 class NdkTypographyPaintAttributeTest : public testing::Test {
32 public:
33 void SetUp() override;
34 void TearDown() override;
35 protected:
36 OH_Drawing_TypographyCreate* fHandler{nullptr};
37 OH_Drawing_Typography* fTypography{nullptr};
38 OH_Drawing_TypographyStyle* fTypoStyle{nullptr};
39 OH_Drawing_TextStyle* fTxtStyle{nullptr};
40 int fLayoutWidth{50};
41 };
42
ConvertToOriginalText(OH_Drawing_Typography * style)43 static Typography* ConvertToOriginalText(OH_Drawing_Typography* style)
44 {
45 return reinterpret_cast<Typography*>(style);
46 }
47
SetUp()48 void NdkTypographyPaintAttributeTest::SetUp()
49 {
50 fTypoStyle = OH_Drawing_CreateTypographyStyle();
51 ASSERT_NE(fTypoStyle, nullptr);
52 OH_Drawing_SetTypographyTextFontSize(fTypoStyle, DEFAULT_FONT_SIZE);
53 fHandler = OH_Drawing_CreateTypographyHandler(fTypoStyle, OH_Drawing_CreateFontCollection());
54 ASSERT_NE(fHandler, nullptr);
55 fTxtStyle = OH_Drawing_CreateTextStyle();
56 ASSERT_NE(fTxtStyle, nullptr);
57 OH_Drawing_SetTextStyleFontSize(fTxtStyle, DEFAULT_FONT_SIZE);
58 OH_Drawing_TypographyHandlerPushTextStyle(fHandler, fTxtStyle);
59 OH_Drawing_TypographyHandlerAddText(fHandler, DEFAULT_TEXT);
60 fTypography = OH_Drawing_CreateTypography(fHandler);
61 ASSERT_NE(fTypography, nullptr);
62 OH_Drawing_TypographyLayout(fTypography, fLayoutWidth);
63 }
64
TearDown()65 void NdkTypographyPaintAttributeTest::TearDown()
66 {
67 if (fHandler != nullptr) {
68 OH_Drawing_DestroyTypographyHandler(fHandler);
69 fHandler = nullptr;
70 }
71 if (fTypography != nullptr) {
72 OH_Drawing_DestroyTypography(fTypography);
73 fTypography = nullptr;
74 }
75 if (fTypoStyle != nullptr) {
76 OH_Drawing_DestroyTypographyStyle(fTypoStyle);
77 fTypoStyle = nullptr;
78 }
79 if (fTxtStyle != nullptr) {
80 OH_Drawing_DestroyTextStyle(fTxtStyle);
81 fTxtStyle = nullptr;
82 }
83 }
84
85 /*
86 * @tc.name: TypographyUpdateNullPtrTest001
87 * @tc.desc: test the nullptr input value for updating the paint attribute
88 * @tc.type: FUNC
89 */
90 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateNullPtrTest001, TestSize.Level0)
91 {
92 uint32_t color = OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF);
93 EXPECT_EQ(color, 0xFF00FFFF);
94 OH_Drawing_TypographyUpdateFontColor(nullptr, color);
95 OH_Drawing_TypographyUpdateDecoration(nullptr, TEXT_DECORATION_LINE_THROUGH);
96 OH_Drawing_TypographyUpdateDecorationThicknessScale(nullptr, 2.0);
97 OH_Drawing_TypographyUpdateDecorationStyle(nullptr, TEXT_DECORATION_STYLE_DOTTED);
98 }
99
100 /*
101 * @tc.name: TypographyUpdateFontColorTest001
102 * @tc.desc: test for updating the font color
103 * @tc.type: FUNC
104 */
105 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateFontColorTest001, TestSize.Level0)
106 {
107 OH_Drawing_TypographyUpdateFontColor(fTypography, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF));
108
109 Typography* typography = ConvertToOriginalText(fTypography);
110 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
111 for (const auto& item : runMetrics) {
112 EXPECT_EQ(item.second.textStyle->color.CastToColorQuad(), Drawing::Color::ColorQuadSetARGB(255, 0, 255, 255));
113 }
114 }
115
116 /*
117 * @tc.name: TypographyUpdateDecorationTest001
118 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_LINE_THROUGH
119 * @tc.type: FUNC
120 */
121 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationTest001, TestSize.Level0)
122 {
123 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_LINE_THROUGH);
124
125 Typography* typography = ConvertToOriginalText(fTypography);
126 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
127 for (const auto& item : runMetrics) {
128 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::LINE_THROUGH);
129 }
130 }
131
132 /*
133 * @tc.name: TypographyUpdateDecorationTest002
134 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_UNDERLINE
135 * @tc.type: FUNC
136 */
137 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationTest002, TestSize.Level0)
138 {
139 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_UNDERLINE);
140
141 Typography* typography = ConvertToOriginalText(fTypography);
142 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
143 for (const auto& item : runMetrics) {
144 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::UNDERLINE);
145 }
146 }
147
148 /*
149 * @tc.name: TypographyUpdateDecorationTest003
150 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_OVERLINE
151 * @tc.type: FUNC
152 */
153 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationTest003, TestSize.Level0)
154 {
155 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_OVERLINE);
156
157 Typography* typography = ConvertToOriginalText(fTypography);
158 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
159 for (const auto& item : runMetrics) {
160 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::OVERLINE);
161 }
162 }
163
164 /*
165 * @tc.name: TypographyUpdateDecorationTest004
166 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_NONE
167 * @tc.type: FUNC
168 */
169 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationTest004, TestSize.Level0)
170 {
171 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_NONE);
172
173 Typography* typography = ConvertToOriginalText(fTypography);
174 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
175 for (const auto& item : runMetrics) {
176 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::NONE);
177 }
178 }
179
180 /*
181 * @tc.name: TypographyUpdateDecorationThicknessScaleTest001
182 * @tc.desc: test for updating the decoration thickness scale
183 * @tc.type: FUNC
184 */
185 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationThicknessScaleTest001,
186 TestSize.Level0)
187 {
188 OH_Drawing_TypographyUpdateDecorationThicknessScale(fTypography, 2.0);
189
190 Typography* typography = ConvertToOriginalText(fTypography);
191 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
192 for (const auto& item : runMetrics) {
193 EXPECT_EQ(item.second.textStyle->decorationThicknessScale, 2.0);
194 }
195 }
196
197 /*
198 * @tc.name: TypographyUpdateDecorationStyleTest001
199 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_DOTTED
200 * @tc.type: FUNC
201 */
202 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationStyleTest001, TestSize.Level0)
203 {
204 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_DOTTED);
205
206 Typography* typography = ConvertToOriginalText(fTypography);
207 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
208 for (const auto& item : runMetrics) {
209 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::DOTTED);
210 }
211 }
212
213 /*
214 * @tc.name: TypographyUpdateDecorationStyleTest002
215 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_DOTTED
216 * @tc.type: FUNC
217 */
218 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationStyleTest002, TestSize.Level0)
219 {
220 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_SOLID);
221
222 Typography* typography = ConvertToOriginalText(fTypography);
223 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
224 for (const auto& item : runMetrics) {
225 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::SOLID);
226 }
227 }
228
229 /*
230 * @tc.name: TypographyUpdateDecorationStyleTest003
231 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_WAVY
232 * @tc.type: FUNC
233 */
234 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationStyleTest003, TestSize.Level0)
235 {
236 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_WAVY);
237
238 Typography* typography = ConvertToOriginalText(fTypography);
239 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
240 for (const auto& item : runMetrics) {
241 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::WAVY);
242 }
243 }
244
245 /*
246 * @tc.name: TypographyUpdateDecorationStyleTest004
247 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_DASHED
248 * @tc.type: FUNC
249 */
250 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationStyleTest004, TestSize.Level0)
251 {
252 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_DASHED);
253
254 Typography* typography = ConvertToOriginalText(fTypography);
255 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
256 for (const auto& item : runMetrics) {
257 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::DASHED);
258 }
259 }
260
261 /*
262 * @tc.name: TypographyUpdateDecorationStyleTest005
263 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_DOUBLE
264 * @tc.type: FUNC
265 */
266 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationStyleTest005, TestSize.Level0)
267 {
268 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_DOUBLE);
269
270 Typography* typography = ConvertToOriginalText(fTypography);
271 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
272 for (const auto& item : runMetrics) {
273 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::DOUBLE);
274 }
275 }
276
277 /*
278 * @tc.name: DrawingTypographyUpdateColorAndDecoration001
279 * @tc.desc: test for updating the font color and decoration in mutiple text.
280 * @tc.type: FUNC
281 */
282 HWTEST_F(NdkTypographyPaintAttributeTest, DrawingTypographyUpdateColorAndDecoration001, TestSize.Level0)
283 {
284 OH_Drawing_SetTextStyleColor(fTxtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0x00));
285 OH_Drawing_SetTextStyleDecoration(fTxtStyle, TEXT_DECORATION_NONE);
286 OH_Drawing_SetTextStyleDecorationStyle(fTxtStyle, TEXT_DECORATION_STYLE_SOLID);
287 OH_Drawing_SetTextStyleDecorationThicknessScale(fTxtStyle, 1.5);
288 OH_Drawing_TypographyHandlerPushTextStyle(fHandler, fTxtStyle);
289 OH_Drawing_TypographyHandlerAddText(fHandler, DEFAULT_TEXT);
290
291 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_LINE_THROUGH);
292 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_DOTTED);
293 OH_Drawing_TypographyUpdateDecorationThicknessScale(fTypography, 2.0);
294 OH_Drawing_TypographyUpdateFontColor(fTypography, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF));
295
296 Typography* typography = ConvertToOriginalText(fTypography);
297 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
298 for (const auto& item : runMetrics) {
299 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::LINE_THROUGH);
300 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::DOTTED);
301 EXPECT_EQ(item.second.textStyle->decorationThicknessScale, 2.0);
302 EXPECT_EQ(item.second.textStyle->color.CastToColorQuad(), Drawing::Color::ColorQuadSetARGB(255, 0, 255, 255));
303 }
304 }
305
306 /*
307 * @tc.name: TypographyUpdateDecorationColorTest001
308 * @tc.desc: test for updating the decoration color
309 * @tc.type: FUNC
310 */
311 HWTEST_F(NdkTypographyPaintAttributeTest, TypographyUpdateDecorationColorTest001, TestSize.Level0)
312 {
313 OH_Drawing_TypographyUpdateDecorationColor(nullptr, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF));
314 OH_Drawing_TypographyUpdateDecorationColor(fTypography, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF));
315
316 Typography* typography = ConvertToOriginalText(fTypography);
317 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
318 for (const auto& item : runMetrics) {
319 EXPECT_EQ(item.second.textStyle->decorationColor.CastToColorQuad(),
320 Drawing::Color::ColorQuadSetARGB(255, 0, 255, 255));
321 }
322 }
323 } // namespace OHOS