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 NativeTypographyUpdate : public testing::Test {
32 public:
33 void SetUp() override;
34 void TearDown() override;
35 void PrepareWorkForAlignmentTest();
36
37 protected:
38 OH_Drawing_TypographyCreate* fHandler{nullptr};
39 OH_Drawing_Typography* fTypography{nullptr};
40 OH_Drawing_TypographyStyle* fTypoStyle{nullptr};
41 OH_Drawing_TextStyle* fTxtStyle{nullptr};
42 int fLayoutWidth{50};
43 };
44
ConvertToOriginalText(OH_Drawing_Typography * style)45 static Typography* ConvertToOriginalText(OH_Drawing_Typography* style)
46 {
47 return reinterpret_cast<Typography*>(style);
48 }
49
PrepareWorkForAlignmentTest()50 void NativeTypographyUpdate::PrepareWorkForAlignmentTest()
51 {
52 fHandler = OH_Drawing_CreateTypographyHandler(fTypoStyle, OH_Drawing_CreateFontCollection());
53 ASSERT_NE(fHandler, nullptr);
54 fTxtStyle = OH_Drawing_CreateTextStyle();
55 ASSERT_NE(fTxtStyle, nullptr);
56 OH_Drawing_SetTextStyleFontSize(fTxtStyle, DEFAULT_FONT_SIZE);
57 OH_Drawing_TypographyHandlerPushTextStyle(fHandler, fTxtStyle);
58 OH_Drawing_TypographyHandlerAddText(fHandler, DEFAULT_TEXT);
59 fTypography = OH_Drawing_CreateTypography(fHandler);
60 ASSERT_NE(fTypography, nullptr);
61 OH_Drawing_TypographyLayout(fTypography, fLayoutWidth);
62 }
63
SetUp()64 void NativeTypographyUpdate::SetUp()
65 {
66 fTypoStyle = OH_Drawing_CreateTypographyStyle();
67 ASSERT_NE(fTypoStyle, nullptr);
68 OH_Drawing_SetTypographyTextFontSize(fTypoStyle, DEFAULT_FONT_SIZE);
69 fHandler = OH_Drawing_CreateTypographyHandler(fTypoStyle, OH_Drawing_CreateFontCollection());
70 ASSERT_NE(fHandler, nullptr);
71 fTxtStyle = OH_Drawing_CreateTextStyle();
72 ASSERT_NE(fTxtStyle, nullptr);
73 OH_Drawing_SetTextStyleFontSize(fTxtStyle, DEFAULT_FONT_SIZE);
74 OH_Drawing_TypographyHandlerPushTextStyle(fHandler, fTxtStyle);
75 OH_Drawing_TypographyHandlerAddText(fHandler, DEFAULT_TEXT);
76 fTypography = OH_Drawing_CreateTypography(fHandler);
77 ASSERT_NE(fTypography, nullptr);
78 OH_Drawing_TypographyLayout(fTypography, fLayoutWidth);
79 }
80
TearDown()81 void NativeTypographyUpdate::TearDown()
82 {
83 if (fHandler != nullptr) {
84 OH_Drawing_DestroyTypographyHandler(fHandler);
85 fHandler = nullptr;
86 }
87 if (fTypography != nullptr) {
88 OH_Drawing_DestroyTypography(fTypography);
89 fTypography = nullptr;
90 }
91 if (fTypoStyle != nullptr) {
92 OH_Drawing_DestroyTypographyStyle(fTypoStyle);
93 fTypoStyle = nullptr;
94 }
95 if (fTxtStyle != nullptr) {
96 OH_Drawing_DestroyTextStyle(fTxtStyle);
97 fTxtStyle = nullptr;
98 }
99 }
100
101 /*
102 * @tc.name: OHDrawingTypographyUpdate001
103 * @tc.desc: test the nullptr input value for updating the paint attribute
104 * @tc.type: FUNC
105 */
106 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate001, Function | MediumTest | Level0)
107 {
108 OH_Drawing_TypographyUpdateFontColor(nullptr, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF));
109 OH_Drawing_TypographyUpdateDecoration(nullptr, TEXT_DECORATION_LINE_THROUGH);
110 OH_Drawing_TypographyUpdateDecorationThicknessScale(nullptr, 2.0);
111 OH_Drawing_TypographyUpdateDecorationStyle(nullptr, TEXT_DECORATION_STYLE_DOTTED);
112 }
113
114 /*
115 * @tc.name: OHDrawingTypographyUpdate002
116 * @tc.desc: test for updating the font color
117 * @tc.type: FUNC
118 */
119 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate002, Function | MediumTest | Level0)
120 {
121 OH_Drawing_TypographyUpdateFontColor(fTypography, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF));
122
123 Typography* typography = ConvertToOriginalText(fTypography);
124 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
125 for (const auto& item : runMetrics) {
126 EXPECT_EQ(item.second.textStyle->color.CastToColorQuad(), Drawing::Color::ColorQuadSetARGB(255, 0, 255, 255));
127 }
128 }
129
130 /*
131 * @tc.name: OHDrawingTypographyUpdate003
132 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_LINE_THROUGH
133 * @tc.type: FUNC
134 */
135 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate003, Function | MediumTest | Level0)
136 {
137 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_LINE_THROUGH);
138
139 Typography* typography = ConvertToOriginalText(fTypography);
140 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
141 for (const auto& item : runMetrics) {
142 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::LINE_THROUGH);
143 }
144 }
145
146 /*
147 * @tc.name: OHDrawingTypographyUpdate004
148 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_UNDERLINE
149 * @tc.type: FUNC
150 */
151 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate004, Function | MediumTest | Level0)
152 {
153 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_UNDERLINE);
154
155 Typography* typography = ConvertToOriginalText(fTypography);
156 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
157 for (const auto& item : runMetrics) {
158 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::UNDERLINE);
159 }
160 }
161
162 /*
163 * @tc.name: OHDrawingTypographyUpdate005
164 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_OVERLINE
165 * @tc.type: FUNC
166 */
167 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate005, Function | MediumTest | Level0)
168 {
169 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_OVERLINE);
170
171 Typography* typography = ConvertToOriginalText(fTypography);
172 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
173 for (const auto& item : runMetrics) {
174 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::OVERLINE);
175 }
176 }
177
178 /*
179 * @tc.name: OHDrawingTypographyUpdate006
180 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_NONE
181 * @tc.type: FUNC
182 */
183 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate006, Function | MediumTest | Level0)
184 {
185 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_NONE);
186
187 Typography* typography = ConvertToOriginalText(fTypography);
188 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
189 for (const auto& item : runMetrics) {
190 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::NONE);
191 }
192 }
193
194 /*
195 * @tc.name: OHDrawingTypographyUpdate007
196 * @tc.desc: test for updating the decoration thickness scale
197 * @tc.type: FUNC
198 */
199 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate007, Function | MediumTest | Level0)
200 {
201 OH_Drawing_TypographyUpdateDecorationThicknessScale(fTypography, 2.0);
202
203 Typography* typography = ConvertToOriginalText(fTypography);
204 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
205 for (const auto& item : runMetrics) {
206 EXPECT_EQ(item.second.textStyle->decorationThicknessScale, 2.0);
207 }
208 }
209
210 /*
211 * @tc.name: OHDrawingTypographyUpdate008
212 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_DOTTED
213 * @tc.type: FUNC
214 */
215 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate008, Function | MediumTest | Level0)
216 {
217 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_DOTTED);
218
219 Typography* typography = ConvertToOriginalText(fTypography);
220 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
221 for (const auto& item : runMetrics) {
222 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::DOTTED);
223 }
224 }
225
226 /*
227 * @tc.name: OHDrawingTypographyUpdate009
228 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_DOTTED
229 * @tc.type: FUNC
230 */
231 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate009, Function | MediumTest | Level0)
232 {
233 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_SOLID);
234
235 Typography* typography = ConvertToOriginalText(fTypography);
236 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
237 for (const auto& item : runMetrics) {
238 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::SOLID);
239 }
240 }
241
242 /*
243 * @tc.name: OHDrawingTypographyUpdate010
244 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_WAVY
245 * @tc.type: FUNC
246 */
247 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate010, Function | MediumTest | Level0)
248 {
249 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_WAVY);
250
251 Typography* typography = ConvertToOriginalText(fTypography);
252 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
253 for (const auto& item : runMetrics) {
254 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::WAVY);
255 }
256 }
257
258 /*
259 * @tc.name: OHDrawingTypographyUpdate011
260 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_DASHED
261 * @tc.type: FUNC
262 */
263 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate011, Function | MediumTest | Level0)
264 {
265 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_DASHED);
266
267 Typography* typography = ConvertToOriginalText(fTypography);
268 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
269 for (const auto& item : runMetrics) {
270 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::DASHED);
271 }
272 }
273
274 /*
275 * @tc.name: OHDrawingTypographyUpdate012
276 * @tc.desc: test for updating the decoration to the value of TEXT_DECORATION_STYLE_DOUBLE
277 * @tc.type: FUNC
278 */
279 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate012, Function | MediumTest | Level0)
280 {
281 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_DOUBLE);
282
283 Typography* typography = ConvertToOriginalText(fTypography);
284 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
285 for (const auto& item : runMetrics) {
286 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::DOUBLE);
287 }
288 }
289
290 /*
291 * @tc.name: OHDrawingTypographyUpdate013
292 * @tc.desc: test for updating the font color and decoration in mutiple text.
293 * @tc.type: FUNC
294 */
295 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdate013, Function | MediumTest | Level0)
296 {
297 OH_Drawing_SetTextStyleColor(fTxtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0x00));
298 OH_Drawing_SetTextStyleDecoration(fTxtStyle, TEXT_DECORATION_NONE);
299 OH_Drawing_SetTextStyleDecorationStyle(fTxtStyle, TEXT_DECORATION_STYLE_SOLID);
300 OH_Drawing_SetTextStyleDecorationThicknessScale(fTxtStyle, 1.5);
301 OH_Drawing_TypographyHandlerPushTextStyle(fHandler, fTxtStyle);
302 OH_Drawing_TypographyHandlerAddText(fHandler, DEFAULT_TEXT);
303
304 OH_Drawing_TypographyUpdateDecoration(fTypography, TEXT_DECORATION_LINE_THROUGH);
305 OH_Drawing_TypographyUpdateDecorationStyle(fTypography, TEXT_DECORATION_STYLE_DOTTED);
306 OH_Drawing_TypographyUpdateDecorationThicknessScale(fTypography, 2.0);
307 OH_Drawing_TypographyUpdateFontColor(fTypography, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF));
308
309 Typography* typography = ConvertToOriginalText(fTypography);
310 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
311 for (const auto& item : runMetrics) {
312 EXPECT_EQ(item.second.textStyle->decoration, TextDecoration::LINE_THROUGH);
313 EXPECT_EQ(item.second.textStyle->decorationStyle, TextDecorationStyle::DOTTED);
314 EXPECT_EQ(item.second.textStyle->decorationThicknessScale, 2.0);
315 EXPECT_EQ(item.second.textStyle->color.CastToColorQuad(), Drawing::Color::ColorQuadSetARGB(255, 0, 255, 255));
316 }
317 }
318
319 /*
320 * @tc.name: OHDrawingSetTypographyTextTrailingSpaceOptimized001
321 * @tc.desc: test for setting the different value of trailingSpaceOptimized and improve code coverage
322 * @tc.type: FUNC
323 */
324 HWTEST_F(NativeTypographyUpdate, OHDrawingSetTypographyTextTrailingSpaceOptimized001, Function | MediumTest | Level0)
325 {
326 OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_RIGHT);
327 OH_Drawing_SetTypographyTextTrailingSpaceOptimized(nullptr, true);
328 OH_Drawing_SetTypographyTextTrailingSpaceOptimized(nullptr, false);
329 OH_Drawing_SetTypographyTextTrailingSpaceOptimized(fTypoStyle, true);
330 OH_Drawing_SetTypographyTextTrailingSpaceOptimized(fTypoStyle, false);
331 PrepareWorkForAlignmentTest();
332 }
333
334 /*
335 * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TypographyUpdateDecorationColor_001
336 * @tc.name : OHDrawingTypographyUpdateDecorationColor001
337 * @tc.desc : test for updating the decoration color
338 * @tc.size : MediumTest
339 * @tc.type : Function
340 * @tc.level : Level 0
341 */
342 HWTEST_F(NativeTypographyUpdate, OHDrawingTypographyUpdateDecorationColor001, Function | MediumTest | Level0)
343 {
344 OH_Drawing_TypographyUpdateDecorationColor(nullptr, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF));
345 OH_Drawing_TypographyUpdateDecorationColor(fTypography, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0xFF));
346
347 Typography* typography = ConvertToOriginalText(fTypography);
348 auto runMetrics = typography->GetLineMetrics()[0].runMetrics;
349 for (const auto& item : runMetrics) {
350 EXPECT_EQ(item.second.textStyle->decorationColor.CastToColorQuad(),
351 Drawing::Color::ColorQuadSetARGB(255, 0, 255, 255));
352 }
353 }
354
355 /*
356 * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_SetTypographyVerticalAlignment_001
357 * @tc.name : OHDrawingSetTypographyVerticalAlignment001
358 * @tc.desc : Test for vertical align valid params
359 * @tc.size : MediumTest
360 * @tc.type : Function
361 * @tc.level : Level 0
362 */
363 HWTEST_F(NativeTypographyUpdate, OHDrawingSetTypographyVerticalAlignment001, Function | MediumTest | Level0)
364 {
365 EXPECT_NO_FATAL_FAILURE(OH_Drawing_SetTypographyVerticalAlignment(nullptr,
366 OH_Drawing_TextVerticalAlignment::TEXT_VERTICAL_ALIGNMENT_BOTTOM));
367 }
368 } // namespace OHOS