1 /*
2 * Copyright (c) 2022 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 "gtest/gtest.h"
17 #include "c/drawing_bitmap.h"
18 #include "c/drawing_canvas.h"
19 #include "c/drawing_color.h"
20 #include "c/drawing_font_collection.h"
21 #include "c/drawing_text_declaration.h"
22 #include "c/drawing_text_typography.h"
23 #include "rosen_text/ui/typography.h"
24 #include "rosen_text/ui/typography_create.h"
25
26 using namespace rosen;
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 class OH_Drawing_TypographyTest : public testing::Test {
32 };
33
ConvertToOriginalText(OH_Drawing_TypographyStyle * style)34 static TypographyStyle* ConvertToOriginalText(OH_Drawing_TypographyStyle* style)
35 {
36 return reinterpret_cast<TypographyStyle*>(style);
37 }
38
ConvertToOriginalText(OH_Drawing_TextStyle * style)39 static TextStyle* ConvertToOriginalText(OH_Drawing_TextStyle* style)
40 {
41 return reinterpret_cast<TextStyle*>(style);
42 }
43
44 /*
45 * @tc.name: OH_Drawing_TypographyTest001
46 * @tc.desc: test for creating TypographyStyle
47 * @tc.type: FUNC
48 */
49 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest001, TestSize.Level1)
50 {
51 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
52 EXPECT_EQ(typoStyle == nullptr, false);
53 OH_Drawing_DestroyTypographyStyle(typoStyle);
54 }
55
56 /*
57 * @tc.name: OH_Drawing_TypographyTest002
58 * @tc.desc: test for text direction
59 * @tc.type: FUNC
60 */
61 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest002, TestSize.Level1)
62 {
63 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
64 OH_Drawing_SetTypographyTextDirection(typoStyle, TEXT_DIRECTION_LTR);
65 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textDirection_, TextDirection::LTR);
66 OH_Drawing_SetTypographyTextDirection(typoStyle, TEXT_DIRECTION_RTL);
67 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textDirection_, TextDirection::RTL);
68 OH_Drawing_SetTypographyTextDirection(typoStyle, -1);
69 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textDirection_, TextDirection::LTR);
70 }
71
72 /*
73 * @tc.name: OH_Drawing_TypographyTest003
74 * @tc.desc: test for text alignment
75 * @tc.type: FUNC
76 */
77 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest003, TestSize.Level1)
78 {
79 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
80 OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_LEFT);
81 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textAlign_, TextAlign::LEFT);
82 OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_RIGHT);
83 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textAlign_, TextAlign::RIGHT);
84 OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
85 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textAlign_, TextAlign::CENTER);
86 OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_JUSTIFY);
87 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textAlign_, TextAlign::JUSTIFY);
88 OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_START);
89 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textAlign_, TextAlign::START);
90 OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_END);
91 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textAlign_, TextAlign::END);
92 OH_Drawing_SetTypographyTextAlign(typoStyle, -1);
93 EXPECT_EQ(ConvertToOriginalText(typoStyle)->textAlign_, TextAlign::LEFT);
94 }
95
96 /*
97 * @tc.name: OH_Drawing_TypographyTest004
98 * @tc.desc: test for max lines
99 * @tc.type: FUNC
100 */
101 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest004, TestSize.Level1)
102 {
103 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
104 OH_Drawing_SetTypographyTextMaxLines(typoStyle, 100);
105 EXPECT_EQ(ConvertToOriginalText(typoStyle)->maxLines_, 100);
106 OH_Drawing_SetTypographyTextMaxLines(typoStyle, 200);
107 EXPECT_EQ(ConvertToOriginalText(typoStyle)->maxLines_, 200);
108 }
109
110 /*
111 * @tc.name: OH_Drawing_TypographyTest005
112 * @tc.desc: test for creating text style
113 * @tc.type: FUNC
114 */
115 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest005, TestSize.Level1)
116 {
117 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
118 EXPECT_EQ(txtStyle == nullptr, false);
119 OH_Drawing_DestroyTextStyle(txtStyle);
120 }
121
122 /*
123 * @tc.name: OH_Drawing_TypographyTest006
124 * @tc.desc: test for text color
125 * @tc.type: FUNC
126 */
127 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest006, TestSize.Level1)
128 {
129 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
130 // black
131 OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
132 EXPECT_EQ(ConvertToOriginalText(txtStyle)->color_, 0xFF000000);
133 // red
134 OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
135 EXPECT_EQ(ConvertToOriginalText(txtStyle)->color_, 0xFFFF0000);
136 // blue
137 OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0xFF));
138 EXPECT_EQ(ConvertToOriginalText(txtStyle)->color_, 0xFF0000FF);
139 }
140
141 /*
142 * @tc.name: OH_Drawing_TypographyTest007
143 * @tc.desc: test for font size
144 * @tc.type: FUNC
145 */
146 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest007, TestSize.Level1)
147 {
148 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
149 OH_Drawing_SetTextStyleFontSize(txtStyle, 80);
150 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontSize_, 80);
151 OH_Drawing_SetTextStyleFontSize(txtStyle, 40);
152 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontSize_, 40);
153 }
154
155 /*
156 * @tc.name: OH_Drawing_TypographyTest008
157 * @tc.desc: test for font weight
158 * @tc.type: FUNC
159 */
160 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest008, TestSize.Level1)
161 {
162 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
163 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_100);
164 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W100);
165 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_200);
166 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W200);
167 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_300);
168 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W300);
169 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
170 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W400);
171 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_500);
172 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W500);
173 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_600);
174 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W600);
175 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_700);
176 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W700);
177 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_800);
178 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W800);
179 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_900);
180 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W900);
181 OH_Drawing_SetTextStyleFontWeight(txtStyle, -1);
182 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontWeight_, FontWeight::W400);
183 }
184
185 /*
186 * @tc.name: OH_Drawing_TypographyTest009
187 * @tc.desc: test for baseline location
188 * @tc.type: FUNC
189 */
190 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest009, TestSize.Level1)
191 {
192 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
193 OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_ALPHABETIC);
194 EXPECT_EQ(ConvertToOriginalText(txtStyle)->textBaseline_, TextBaseline::ALPHABETIC);
195 OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_IDEOGRAPHIC);
196 EXPECT_EQ(ConvertToOriginalText(txtStyle)->textBaseline_, TextBaseline::IDEOGRAPHIC);
197 OH_Drawing_SetTextStyleBaseLine(txtStyle, -1);
198 EXPECT_EQ(ConvertToOriginalText(txtStyle)->textBaseline_, TextBaseline::ALPHABETIC);
199 }
200
201 /*
202 * @tc.name: OH_Drawing_TypographyTest010
203 * @tc.desc: test for text decoration
204 * @tc.type: FUNC
205 */
206 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest010, TestSize.Level1)
207 {
208 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
209 OH_Drawing_SetTextStyleDecoration(txtStyle, TEXT_DECORATION_NONE);
210 EXPECT_EQ(ConvertToOriginalText(txtStyle)->decoration_, TextDecoration::NONE);
211 OH_Drawing_SetTextStyleDecoration(txtStyle, TEXT_DECORATION_UNDERLINE);
212 EXPECT_EQ(ConvertToOriginalText(txtStyle)->decoration_, TextDecoration::UNDERLINE);
213 OH_Drawing_SetTextStyleDecoration(txtStyle, TEXT_DECORATION_OVERLINE);
214 EXPECT_EQ(ConvertToOriginalText(txtStyle)->decoration_, TextDecoration::OVERLINE);
215 OH_Drawing_SetTextStyleDecoration(txtStyle, TEXT_DECORATION_LINE_THROUGH);
216 EXPECT_EQ(ConvertToOriginalText(txtStyle)->decoration_, TextDecoration::LINETHROUGH);
217 OH_Drawing_SetTextStyleDecoration(txtStyle, -1);
218 EXPECT_EQ(ConvertToOriginalText(txtStyle)->decoration_, TextDecoration::NONE);
219 }
220
221 /*
222 * @tc.name: OH_Drawing_TypographyTest011
223 * @tc.desc: test for text decoration color
224 * @tc.type: FUNC
225 */
226 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest011, TestSize.Level1)
227 {
228 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
229 OH_Drawing_SetTextStyleDecorationColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
230 EXPECT_EQ(ConvertToOriginalText(txtStyle)->decorationColor_, 0xFF000000);
231 OH_Drawing_SetTextStyleDecorationColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
232 EXPECT_EQ(ConvertToOriginalText(txtStyle)->decorationColor_, 0xFFFF0000);
233 }
234
235 /*
236 * @tc.name: OH_Drawing_TypographyTest012
237 * @tc.desc: test for font height
238 * @tc.type: FUNC
239 */
240 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest012, TestSize.Level1)
241 {
242 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
243 OH_Drawing_SetTextStyleFontHeight(txtStyle, 0.0);
244 EXPECT_EQ(ConvertToOriginalText(txtStyle)->height_, 0.0);
245 }
246
247 /*
248 * @tc.name: OH_Drawing_TypographyTest013
249 * @tc.desc: test for font families
250 * @tc.type: FUNC
251 */
252 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest013, TestSize.Level1)
253 {
254 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
255 const char* fontFamilies[] = {"Roboto"};
256 OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies);
257 std::vector<std::string> fontFamiliesResult = {"Roboto"};
258 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontFamilies_, fontFamiliesResult);
259 }
260
261 /*
262 * @tc.name: OH_Drawing_TypographyTest014
263 * @tc.desc: test for font italic
264 * @tc.type: FUNC
265 */
266 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest014, TestSize.Level1)
267 {
268 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
269 OH_Drawing_SetTextStyleFontStyle(txtStyle, FONT_STYLE_NORMAL);
270 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontStyle_, FontStyle::NORMAL);
271 OH_Drawing_SetTextStyleFontStyle(txtStyle, FONT_STYLE_ITALIC);
272 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontStyle_, FontStyle::ITALIC);
273 OH_Drawing_SetTextStyleFontStyle(txtStyle, -1);
274 EXPECT_EQ(ConvertToOriginalText(txtStyle)->fontStyle_, FontStyle::NORMAL);
275 }
276
277 /*
278 * @tc.name: OH_Drawing_TypographyTest015
279 * @tc.desc: test for font locale
280 * @tc.type: FUNC
281 */
282 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest015, TestSize.Level1)
283 {
284 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
285 OH_Drawing_SetTextStyleLocale(txtStyle, "en");
286 EXPECT_EQ(ConvertToOriginalText(txtStyle)->locale_, "en");
287 }
288
289 /*
290 * @tc.name: OH_Drawing_TypographyTest016
291 * @tc.desc: test for typography
292 * @tc.type: FUNC
293 */
294 HWTEST_F(OH_Drawing_TypographyTest, OH_Drawing_TypographyTest016, TestSize.Level1)
295 {
296 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
297 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
298 OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle,
299 OH_Drawing_CreateFontCollection());
300 EXPECT_TRUE(handler != nullptr);
301
302 OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
303 double fontSize = 30;
304 OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize);
305 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
306 OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_ALPHABETIC);
307 const char* fontFamilies[] = {"Roboto"};
308 OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies);
309 OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
310
311 const char* text = "OpenHarmony\n";
312 OH_Drawing_TypographyHandlerAddText(handler, text);
313 OH_Drawing_TypographyHandlerPopTextStyle(handler);
314
315 OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler);
316 double maxWidth = 800.0;
317 OH_Drawing_TypographyLayout(typography, maxWidth);
318 EXPECT_EQ(maxWidth, OH_Drawing_TypographyGetMaxWidth(typography));
319 double position[2] = {10.0, 15.0};
320 OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate();
321 OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
322 uint32_t width = 20;
323 uint32_t height = 40;
324 OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
325 EXPECT_EQ(width, OH_Drawing_BitmapGetWidth(cBitmap));
326 EXPECT_EQ(height, OH_Drawing_BitmapGetHeight(cBitmap));
327
328 OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate();
329 OH_Drawing_CanvasBind(cCanvas, cBitmap);
330 OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
331
332 EXPECT_EQ(OH_Drawing_TypographyGetHeight(typography) != 0.0, true);
333 EXPECT_EQ(OH_Drawing_TypographyGetLongestLine(typography) != 0.0, true);
334 EXPECT_EQ(OH_Drawing_TypographyGetMinIntrinsicWidth(typography) <=
335 OH_Drawing_TypographyGetMaxIntrinsicWidth(typography), true);
336 EXPECT_EQ(OH_Drawing_TypographyGetAlphabeticBaseline(typography) != 0.0, true);
337 EXPECT_EQ(OH_Drawing_TypographyGetIdeographicBaseline(typography) != 0.0, true);
338 OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]);
339 OH_Drawing_DestroyTypography(typography);
340 OH_Drawing_DestroyTypographyHandler(handler);
341 }
342 }
343