1 /*
2 * Copyright (c) 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, 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 <fstream>
17 #include <string>
18
19 #include "drawing_color.h"
20 #include "drawing_font.h"
21 #include "drawing_font_collection.h"
22 #include "drawing_text_declaration.h"
23 #include "drawing_text_line.h"
24 #include "drawing_text_lineTypography.h"
25 #include "drawing_text_typography.h"
26 #include "gtest/gtest.h"
27 #include "rosen_text/typography.h"
28 #include "rosen_text/typography_create.h"
29
30 using namespace OHOS::Rosen;
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS {
35
36 class LineTypographyTest : public testing::Test {
37 public:
38 void SetUp() override;
39 void TearDown() override;
40 void PrepareTypographyCreate(const char* text);
41
42 protected:
43 OH_Drawing_TypographyCreate* handler_ = nullptr;
44 OH_Drawing_FontCollection* fontCollection_ = nullptr;
45 OH_Drawing_TextStyle* txtStyle_ = nullptr;
46 OH_Drawing_TypographyStyle* typoStyle_ = nullptr;
47 };
48
SetUp()49 void LineTypographyTest::SetUp()
50 {
51 handler_ = nullptr;
52 fontCollection_ = nullptr;
53 txtStyle_ = nullptr;
54 typoStyle_ = nullptr;
55 }
56
TearDown()57 void LineTypographyTest::TearDown()
58 {
59 if (handler_ != nullptr) {
60 OH_Drawing_DestroyTypographyHandler(handler_);
61 handler_ = nullptr;
62 }
63 if (txtStyle_ != nullptr) {
64 OH_Drawing_DestroyTextStyle(txtStyle_);
65 txtStyle_ = nullptr;
66 }
67 if (fontCollection_ != nullptr) {
68 OH_Drawing_DestroyFontCollection(fontCollection_);
69 fontCollection_ = nullptr;
70 }
71 if (typoStyle_ != nullptr) {
72 OH_Drawing_DestroyTypographyStyle(typoStyle_);
73 typoStyle_ = nullptr;
74 }
75 }
76
PrepareTypographyCreate(const char * text)77 void LineTypographyTest::PrepareTypographyCreate(const char* text)
78 {
79 fontCollection_ = OH_Drawing_CreateFontCollection();
80 EXPECT_TRUE(fontCollection_ != nullptr);
81 typoStyle_ = OH_Drawing_CreateTypographyStyle();
82 EXPECT_TRUE(typoStyle_ != nullptr);
83 handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
84 EXPECT_TRUE(handler_ != nullptr);
85 txtStyle_ = OH_Drawing_CreateTextStyle();
86 EXPECT_TRUE(txtStyle_ != nullptr);
87 OH_Drawing_SetTextStyleColor(txtStyle_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
88 double fontSize = 30;
89 OH_Drawing_SetTextStyleFontSize(txtStyle_, fontSize);
90 OH_Drawing_SetTextStyleFontWeight(txtStyle_, FONT_WEIGHT_400);
91 OH_Drawing_SetTextStyleBaseLine(txtStyle_, TEXT_BASELINE_ALPHABETIC);
92 const char* fontFamilies[] = {"Roboto"};
93 OH_Drawing_SetTextStyleFontFamilies(txtStyle_, 1, fontFamilies);
94 OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
95 if (text != nullptr) {
96 OH_Drawing_TypographyHandlerAddText(handler_, text);
97 }
98 }
99
100 /*
101 * @tc.name: OH_Drawing_CreateLineTypographyTest001
102 * @tc.desc: testing for the OH_Drawing_CreateLineTypography and OH_Drawing_DestroyLineTypography
103 * @tc.type: FUNC
104 */
105 HWTEST_F(LineTypographyTest, OH_Drawing_CreateLineTypographyTest001, TestSize.Level1)
106 {
107 PrepareTypographyCreate("OpenHarmony\n");
108 OH_Drawing_LineTypography *lineTypography1 = OH_Drawing_CreateLineTypography(handler_);
109 EXPECT_NE(lineTypography1, nullptr);
110 LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypography1);
111 EXPECT_EQ(innerlineTypography->GetUnicodeSize(), 12);
112
113 OH_Drawing_LineTypography *lineTypography2 = OH_Drawing_CreateLineTypography(handler_);
114 EXPECT_EQ(lineTypography2, nullptr);
115 OH_Drawing_DestroyLineTypography(lineTypography1);
116
117 OH_Drawing_LineTypography *nullLineTypograph = OH_Drawing_CreateLineTypography(nullptr);
118 EXPECT_EQ(nullLineTypograph, nullptr);
119 }
120
121 /*
122 * @tc.name: OH_Drawing_CreateLineTypographyTest002
123 * @tc.desc: testing for the OH_Drawing_TypographyCreate does not contain text
124 * @tc.type: FUNC
125 */
126 HWTEST_F(LineTypographyTest, OH_Drawing_CreateLineTypographyTest002, TestSize.Level1)
127 {
128 PrepareTypographyCreate(nullptr);
129 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
130 EXPECT_EQ(lineTypography, nullptr);
131 }
132
133 /*
134 * @tc.name: OH_Drawing_GetLineBreakTest003
135 * @tc.desc: normal testing for the OH_Drawing_LineTypographyGetLineBreak
136 * @tc.type: FUNC
137 */
138 HWTEST_F(LineTypographyTest, OH_Drawing_GetLineBreakTest003, TestSize.Level1)
139 {
140 const char* text = "OpenHarmony\n";
141 PrepareTypographyCreate(text);
142 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
143 EXPECT_NE(lineTypography, nullptr);
144 LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypography);
145 EXPECT_EQ(innerlineTypography->GetUnicodeSize(), 12);
146
147 double maxWidth = 800.0;
148 size_t startIndex = 0;
149 auto count = OH_Drawing_LineTypographyGetLineBreak(lineTypography, startIndex, maxWidth);
150 EXPECT_EQ(count, strlen(text));
151 OH_Drawing_DestroyLineTypography(lineTypography);
152 }
153
154 /*
155 * @tc.name: OH_Drawing_GetLineBreakTest004
156 * @tc.desc: testing for the OH_Drawing_LineTypographyGetLineBreak.
157 * @tc.type: FUNC
158 */
159 HWTEST_F(LineTypographyTest, OH_Drawing_GetLineBreakTest004, TestSize.Level1)
160 {
161 const char* text1 = "hello\n world";
162 const char* text2 = "hello\n";
163 PrepareTypographyCreate(text1);
164 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
165 EXPECT_NE(lineTypography, nullptr);
166 LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypography);
167 EXPECT_EQ(innerlineTypography->GetUnicodeSize(), 12);
168
169 double maxWidth = 800.0;
170 size_t startIndex = 0;
171 auto count = OH_Drawing_LineTypographyGetLineBreak(lineTypography, startIndex, maxWidth);
172 EXPECT_EQ(count, strlen(text2));
173 OH_Drawing_DestroyLineTypography(lineTypography);
174 }
175
176 /*
177 * @tc.name: OH_Drawing_GetLineBreakTest005
178 * @tc.desc: boundary testing for the OH_Drawing_LineTypographyGetLineBreak.
179 * @tc.type: FUNC
180 */
181 HWTEST_F(LineTypographyTest, OH_Drawing_GetLineBreakTest005, TestSize.Level1)
182 {
183 const char* text = "OpenHarmoney\n";
184 PrepareTypographyCreate(text);
185 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
186 EXPECT_NE(lineTypography, nullptr);
187 LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypography);
188 EXPECT_EQ(innerlineTypography->GetUnicodeSize(), 13);
189
190 double maxWidth = 800.0;
191 size_t startIndex = strlen(text) - 1;
192 auto count = OH_Drawing_LineTypographyGetLineBreak(lineTypography, startIndex, maxWidth);
193 EXPECT_EQ(count, 1);
194 maxWidth = 0;
195 startIndex = 0;
196 count = OH_Drawing_LineTypographyGetLineBreak(lineTypography, startIndex, maxWidth);
197 EXPECT_EQ(count, 0);
198 OH_Drawing_DestroyLineTypography(lineTypography);
199 }
200
201 /*
202 * @tc.name: OH_Drawing_GetLineBreakTest006
203 * @tc.desc: abnormal testing for the OH_Drawing_LineTypographyGetLineBreak.
204 * @tc.type: FUNC
205 */
206 HWTEST_F(LineTypographyTest, OH_Drawing_GetLineBreakTest006, TestSize.Level1)
207 {
208 const char* text = "OpenHarmoney\n";
209 PrepareTypographyCreate(text);
210 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
211 EXPECT_NE(lineTypography, nullptr);
212 LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypography);
213 EXPECT_EQ(innerlineTypography->GetUnicodeSize(), 13);
214
215 double maxWidth = 800.0;
216 size_t startIndex = strlen(text);
217 auto count = OH_Drawing_LineTypographyGetLineBreak(lineTypography, startIndex, maxWidth);
218 EXPECT_EQ(count, 0);
219 maxWidth = 0.01;
220 startIndex = 0;
221 count = OH_Drawing_LineTypographyGetLineBreak(lineTypography, startIndex, maxWidth);
222 EXPECT_EQ(count, 1);
223 OH_Drawing_DestroyLineTypography(lineTypography);
224 }
225
226 /*
227 * @tc.name: OH_Drawing_CreateLineTest007
228 * @tc.desc: testing for the OH_Drawing_LineTypographyGetLineBreak.
229 * @tc.type: FUNC
230 */
231 HWTEST_F(LineTypographyTest, OH_Drawing_CreateLineTest007, TestSize.Level1)
232 {
233 const char* text = "OpenHarmoney\n";
234 PrepareTypographyCreate(text);
235 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
236 EXPECT_NE(lineTypography, nullptr);
237 LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypography);
238 EXPECT_EQ(innerlineTypography->GetUnicodeSize(), 13);
239
240 size_t startIndex = 0;
241 size_t count = strlen(text);
242 auto line = OH_Drawing_LineTypographyCreateLine(lineTypography, startIndex, count);
243 EXPECT_NE(line, nullptr);
244 EXPECT_EQ(OH_Drawing_TextLineGetGlyphCount(line), 12L);
245 OH_Drawing_DestroyTextLine(line);
246 OH_Drawing_DestroyLineTypography(lineTypography);
247 }
248
249 /*
250 * @tc.name: OH_Drawing_CreateLineTest008
251 * @tc.desc: testing for the OH_Drawing_LineTypographyGetLineBreak.
252 * @tc.type: FUNC
253 */
254 HWTEST_F(LineTypographyTest, OH_Drawing_CreateLineTest008, TestSize.Level1)
255 {
256 const char* text = "Hello\n world!";
257 PrepareTypographyCreate(text);
258 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
259 EXPECT_NE(lineTypography, nullptr);
260 LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypography);
261 EXPECT_EQ(innerlineTypography->GetUnicodeSize(), 13);
262
263 size_t startIndex = 0;
264 size_t count = strlen(text);
265 auto line = OH_Drawing_LineTypographyCreateLine(lineTypography, startIndex, count);
266 EXPECT_NE(line, nullptr);
267 EXPECT_EQ(OH_Drawing_TextLineGetGlyphCount(line), 5L);
268 OH_Drawing_DestroyTextLine(line);
269 OH_Drawing_DestroyLineTypography(lineTypography);
270 }
271
272 /*
273 * @tc.name: OH_Drawing_CreateLineTest009
274 * @tc.desc: boundary testing for the OH_Drawing_LineTypographyGetLineBreak.
275 * @tc.type: FUNC
276 */
277 HWTEST_F(LineTypographyTest, OH_Drawing_CreateLineTest009, TestSize.Level1)
278 {
279 const char* text = "OpenHarmoney\n";
280 PrepareTypographyCreate(text);
281 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
282 EXPECT_NE(lineTypography, nullptr);
283 LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypography);
284 EXPECT_EQ(innerlineTypography->GetUnicodeSize(), 13);
285
286 size_t startIndex = strlen(text) - 1;
287 size_t count = 1;
288 auto line1 = OH_Drawing_LineTypographyCreateLine(lineTypography, startIndex, count);
289 EXPECT_NE(line1, nullptr);
290 EXPECT_EQ(OH_Drawing_TextLineGetGlyphCount(line1), 0L);
291 OH_Drawing_DestroyTextLine(line1);
292
293 startIndex = 0;
294 count = 0;
295 auto line2 = OH_Drawing_LineTypographyCreateLine(lineTypography, startIndex, count);
296 EXPECT_NE(line2, nullptr);
297 EXPECT_EQ(OH_Drawing_TextLineGetGlyphCount(line2), 12L);
298 OH_Drawing_DestroyTextLine(line2);
299 OH_Drawing_DestroyLineTypography(lineTypography);
300 }
301
302 /*
303 * @tc.name: OH_Drawing_CreateLineTest010
304 * @tc.desc: abnormal testing for the OH_Drawing_LineTypographyGetLineBreak.
305 * @tc.type: FUNC
306 */
307 HWTEST_F(LineTypographyTest, OH_Drawing_CreateLineTest010, TestSize.Level1)
308 {
309 const char* text = "OpenHarmoney\n";
310 PrepareTypographyCreate(text);
311 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
312 EXPECT_NE(lineTypography, nullptr);
313 LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypography);
314 EXPECT_EQ(innerlineTypography->GetUnicodeSize(), 13);
315
316 size_t startIndex = strlen(text);
317 size_t count = 1;
318 auto line1 = OH_Drawing_LineTypographyCreateLine(lineTypography, startIndex, count);
319 EXPECT_EQ(OH_Drawing_TextLineGetGlyphCount(line1), 0L);
320 EXPECT_EQ(line1, nullptr);
321 OH_Drawing_DestroyTextLine(line1);
322
323 startIndex = 0;
324 count = strlen(text) + 1;
325 auto line2 = OH_Drawing_LineTypographyCreateLine(lineTypography, startIndex, count);
326 EXPECT_EQ(line2, nullptr);
327 EXPECT_EQ(OH_Drawing_TextLineGetGlyphCount(line2), 0L);
328 OH_Drawing_DestroyTextLine(line2);
329 OH_Drawing_DestroyLineTypography(lineTypography);
330 }
331
332 /*
333 * @tc.name: OH_Drawing_LineTypographyTest011
334 * @tc.desc: complex scenes test for the LineTypography
335 * @tc.type: FUNC
336 */
337 HWTEST_F(LineTypographyTest, OH_Drawing_LineTypographyTest011, TestSize.Level1)
338 {
339 std::string text = "Hello \t 中国 测 World \n !@#$%^&*~(){}[] 123 4567890 - = ,. < >、/Drawing testlp 试 ";
340 text += "Drawing \n\n \u231A \u513B \u00A9\uFE0F aaa clp11⌚مرحبا中国 测 World测试文本";
341 PrepareTypographyCreate(text.c_str());
342 OH_Drawing_LineTypography *lineTypography = OH_Drawing_CreateLineTypography(handler_);
343 EXPECT_NE(lineTypography, nullptr);
344 double maxWidth = 800.0;
345 size_t startIndex = 0;
346 int yPosition = 0;
347 do {
348 auto count = OH_Drawing_LineTypographyGetLineBreak(lineTypography, startIndex, maxWidth);
349 if (count == 0) {
350 break;
351 }
352 OH_Drawing_TextLine *line = OH_Drawing_LineTypographyCreateLine(lineTypography, startIndex, count);
353 EXPECT_NE(line, nullptr);
354 yPosition += 30;
355 OH_Drawing_DestroyTextLine(line);
356 startIndex += count;
357 } while (true);
358 OH_Drawing_DestroyLineTypography(lineTypography);
359 }
360
361 /*
362 * @tc.name: OH_Drawing_LineTypographyTest012
363 * @tc.desc: complex scenes test for the LineTypography runsize
364 * @tc.type: FUNC
365 */
366 HWTEST_F(LineTypographyTest, OH_Drawing_LineTypographyTest012, TestSize.Level1)
367 {
368 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
369 OH_Drawing_SetTypographyTextDirection(typoStyle, TEXT_DIRECTION_LTR);
370 OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_LEFT);
371 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
372
373 OH_Drawing_TypographyCreate* handler =
374 OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
375 OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
376 const char* text = "头好痒 test 我 test";
377 OH_Drawing_TypographyHandlerAddText(handler, text);
378 OH_Drawing_LineTypography* lineTypography = OH_Drawing_CreateLineTypography(handler);
379 OH_Drawing_TextLine* textLine = OH_Drawing_LineTypographyCreateLine(lineTypography, 0, 15);
380 OH_Drawing_Array* runs = OH_Drawing_TextLineGetGlyphRuns(textLine);
381 size_t runsSize = OH_Drawing_GetDrawingArraySize(runs);
382 EXPECT_EQ(runsSize, 6);
383 OH_Drawing_DestroyTextLine(textLine);
384 OH_Drawing_DestroyLineTypography(lineTypography);
385 }
386 } // namespace OHOS
387