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 <securec.h>
17 #include "drawing_font_collection.h"
18 #include "drawing_text_typography.h"
19 #include "gtest/gtest.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace {
26 constexpr static float FLOAT_DATA_EPSILON = 1e-6f;
27 const double DEFAULT_FONT_SIZE = 40;
28 const double DEFAULT_LAYOUT_WIDTH = 1000;
29 } // namespace
30
31 class NativeTypographyTestAutoSpace : public testing::Test {
32 public:
33 void TearDown() override;
34 void PrepareWorkForTypographyStyleTest();
35
36 protected:
37 OH_Drawing_FontCollection* fontCollection_{nullptr};
38 OH_Drawing_TextStyle* txtStyle_{nullptr};
39 OH_Drawing_TypographyCreate* handler_{nullptr};
40 OH_Drawing_Typography* typography_{nullptr};
41 OH_Drawing_TypographyStyle* typoStyle_{nullptr};
42 OH_Drawing_TypographyCreate* handler2_{nullptr};
43 OH_Drawing_Typography* typography2_{nullptr};
44 OH_Drawing_TypographyStyle* typoStyle2_{nullptr};
45 };
46
PrepareWorkForTypographyStyleTest()47 void NativeTypographyTestAutoSpace::PrepareWorkForTypographyStyleTest()
48 {
49 txtStyle_ = OH_Drawing_CreateTextStyle();
50 ASSERT_NE(txtStyle_, nullptr);
51 fontCollection_ = OH_Drawing_CreateSharedFontCollection();
52 ASSERT_NE(fontCollection_, nullptr);
53 OH_Drawing_SetTextStyleFontSize(txtStyle_, DEFAULT_FONT_SIZE);
54 }
55
TearDown()56 void NativeTypographyTestAutoSpace::TearDown()
57 {
58 if (fontCollection_ != nullptr) {
59 OH_Drawing_DestroyFontCollection(fontCollection_);
60 fontCollection_ = nullptr;
61 }
62 if (txtStyle_ != nullptr) {
63 OH_Drawing_DestroyTextStyle(txtStyle_);
64 txtStyle_ = nullptr;
65 }
66 if (handler_ != nullptr) {
67 OH_Drawing_DestroyTypographyHandler(handler_);
68 handler_ = nullptr;
69 }
70 if (typography_ != nullptr) {
71 OH_Drawing_DestroyTypography(typography_);
72 typography_ = nullptr;
73 }
74 if (typoStyle_ != nullptr) {
75 OH_Drawing_DestroyTypographyStyle(typoStyle_);
76 typoStyle_ = nullptr;
77 }
78 if (handler2_ != nullptr) {
79 OH_Drawing_DestroyTypographyHandler(handler2_);
80 handler2_ = nullptr;
81 }
82 if (typography2_ != nullptr) {
83 OH_Drawing_DestroyTypography(typography2_);
84 typography2_ = nullptr;
85 }
86 if (typoStyle2_ != nullptr) {
87 OH_Drawing_DestroyTypographyStyle(typoStyle2_);
88 typoStyle2_ = nullptr;
89 }
90 }
91
92 /*
93 * @tc.name: OH_Drawing_SetTypographyTextAutoSpaceTest001
94 * @tc.desc: test for set auto space when paragraph with single run
95 * @tc.type: FUNC
96 */
97 HWTEST_F(NativeTypographyTestAutoSpace, OHDrawingSetTypographyTextAutoSpaceTest001, Function | MediumTest | Level1)
98 {
99 std::string text = "SingRun©2002-2001";
100 PrepareWorkForTypographyStyleTest();
101
102 // paragraph1
103 typoStyle_ = OH_Drawing_CreateTypographyStyle();
104 ASSERT_NE(typoStyle_, nullptr);
105 OH_Drawing_SetTypographyTextAutoSpace(typoStyle_, true);
106 handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
107 ASSERT_NE(handler_, nullptr);
108 OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
109 OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
110 typography_ = OH_Drawing_CreateTypography(handler_);
111 ASSERT_NE(typography_, nullptr);
112 OH_Drawing_TypographyLayout(typography_, DEFAULT_LAYOUT_WIDTH);
113 double longestLineTrue = OH_Drawing_TypographyGetLongestLine(typography_);
114 double line1True = OH_Drawing_TypographyGetLineWidth(typography_, 0);
115 EXPECT_NEAR(longestLineTrue, line1True, FLOAT_DATA_EPSILON);
116
117 // paragraph2
118 typoStyle2_ = OH_Drawing_CreateTypographyStyle();
119 ASSERT_NE(typoStyle2_, nullptr);
120 OH_Drawing_SetTypographyTextAutoSpace(typoStyle2_, false);
121 handler2_ = OH_Drawing_CreateTypographyHandler(typoStyle2_, fontCollection_);
122 ASSERT_NE(handler2_, nullptr);
123 OH_Drawing_TypographyHandlerPushTextStyle(handler2_, txtStyle_);
124 OH_Drawing_TypographyHandlerAddText(handler2_, text.c_str());
125 typography2_ = OH_Drawing_CreateTypography(handler2_);
126 ASSERT_NE(typography2_, nullptr);
127 OH_Drawing_TypographyLayout(typography2_, DEFAULT_LAYOUT_WIDTH);
128 double longestLineFalse = OH_Drawing_TypographyGetLongestLine(typography2_);
129 double line1False = OH_Drawing_TypographyGetLineWidth(typography2_, 0);
130 EXPECT_NEAR(longestLineFalse, line1False, FLOAT_DATA_EPSILON);
131
132 // compare paragraph1 and paragraph2
133 EXPECT_NEAR(longestLineTrue, longestLineFalse + DEFAULT_FONT_SIZE / 8 * 2, FLOAT_DATA_EPSILON);
134 EXPECT_NEAR(line1True, line1False + DEFAULT_FONT_SIZE / 8 * 2, FLOAT_DATA_EPSILON);
135 }
136
137 /*
138 * @tc.name: OH_Drawing_SetTypographyTextAutoSpaceTest002
139 * @tc.desc: test for set auto space when paragraph with single run and the layout width is at the boundary value.
140 * @tc.type: FUNC
141 */
142 HWTEST_F(NativeTypographyTestAutoSpace, OHDrawingSetTypographyTextAutoSpaceTest002, Function | MediumTest | Level1)
143 {
144 std::string text = "SingRun©2002-2001";
145 PrepareWorkForTypographyStyleTest();
146
147 // test boundary value:Use longestline without autospace as layout width when autospace enabled, line count + 1
148 // paragraph1
149 typoStyle_ = OH_Drawing_CreateTypographyStyle();
150 ASSERT_NE(typoStyle_, nullptr);
151 OH_Drawing_SetTypographyTextAutoSpace(typoStyle_, true);
152 handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
153 ASSERT_NE(handler_, nullptr);
154 OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
155 OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
156 typography_ = OH_Drawing_CreateTypography(handler_);
157 ASSERT_NE(typography_, nullptr);
158 double layoutWidth = 388.319641; // boundary value
159 OH_Drawing_TypographyLayout(typography_, layoutWidth);
160 double longestLineTrue = OH_Drawing_TypographyGetLongestLine(typography_);
161 double line1True = OH_Drawing_TypographyGetLineWidth(typography_, 0);
162 double line2True = OH_Drawing_TypographyGetLineWidth(typography_, 1);
163 size_t lineCount = OH_Drawing_TypographyGetLineCount(typography_);
164 EXPECT_NEAR(longestLineTrue, std::max(line1True, line2True), FLOAT_DATA_EPSILON);
165 EXPECT_GT(layoutWidth, longestLineTrue);
166 EXPECT_GT(layoutWidth, line1True);
167 EXPECT_GT(line2True, 0);
168
169 // paragraph2
170 typoStyle2_ = OH_Drawing_CreateTypographyStyle();
171 ASSERT_NE(typoStyle2_, nullptr);
172 OH_Drawing_SetTypographyTextAutoSpace(typoStyle2_, false);
173 handler2_ = OH_Drawing_CreateTypographyHandler(typoStyle2_, fontCollection_);
174 ASSERT_NE(handler2_, nullptr);
175 OH_Drawing_TypographyHandlerPushTextStyle(handler2_, txtStyle_);
176 OH_Drawing_TypographyHandlerAddText(handler2_, text.c_str());
177 typography2_ = OH_Drawing_CreateTypography(handler2_);
178 ASSERT_NE(typography2_, nullptr);
179 OH_Drawing_TypographyLayout(typography2_, layoutWidth);
180 double longestLineFalse2 = OH_Drawing_TypographyGetLongestLine(typography2_);
181 double line1False2 = OH_Drawing_TypographyGetLineWidth(typography2_, 0);
182 size_t lineCount2 = OH_Drawing_TypographyGetLineCount(typography2_);
183 EXPECT_NEAR(longestLineFalse2, line1False2, FLOAT_DATA_EPSILON);
184 EXPECT_NEAR(longestLineFalse2, layoutWidth, FLOAT_DATA_EPSILON);
185 EXPECT_NEAR(line1False2, layoutWidth, FLOAT_DATA_EPSILON);
186
187 // compare paragraph1 and paragraph2
188 EXPECT_GT(longestLineFalse2, longestLineTrue);
189 EXPECT_GT(line1False2, line1True);
190 EXPECT_EQ(lineCount, lineCount2 + 1);
191 }
192
193 /*
194 * @tc.name: OH_Drawing_SetTypographyTextAutoSpaceTest003
195 * @tc.desc: test for set auto space when paragraph with many lines
196 * @tc.type: FUNC
197 */
198 HWTEST_F(NativeTypographyTestAutoSpace, OHDrawingSetTypographyTextAutoSpaceTest003, Function | MediumTest | Level1)
199 {
200 std::string text = "嫌疑者X的牺牲\n版权所有©2002-2001华为技术有限公司保留一切权利\n卸载USB设备";
201 PrepareWorkForTypographyStyleTest();
202
203 // paragraph1
204 typoStyle_ = OH_Drawing_CreateTypographyStyle();
205 ASSERT_NE(typoStyle_, nullptr);
206 OH_Drawing_SetTypographyTextAutoSpace(typoStyle_, true);
207 handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
208 ASSERT_NE(handler_, nullptr);
209 OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
210 OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
211 typography_ = OH_Drawing_CreateTypography(handler_);
212 ASSERT_NE(typography_, nullptr);
213 OH_Drawing_TypographyLayout(typography_, DEFAULT_LAYOUT_WIDTH);
214 double longestLineTrue = OH_Drawing_TypographyGetLongestLine(typography_);
215 double line1True = OH_Drawing_TypographyGetLineWidth(typography_, 0);
216 double line2True = OH_Drawing_TypographyGetLineWidth(typography_, 1);
217 double line3True = OH_Drawing_TypographyGetLineWidth(typography_, 2);
218 EXPECT_NEAR(longestLineTrue, std::max(line1True, std::max(line2True, line3True)), FLOAT_DATA_EPSILON);
219
220 // paragraph2
221 typoStyle2_ = OH_Drawing_CreateTypographyStyle();
222 OH_Drawing_SetTypographyTextAutoSpace(typoStyle2_, false);
223 handler2_ = OH_Drawing_CreateTypographyHandler(typoStyle2_, fontCollection_);
224 OH_Drawing_TypographyHandlerPushTextStyle(handler2_, txtStyle_);
225 OH_Drawing_TypographyHandlerAddText(handler2_, text.c_str());
226 typography2_ = OH_Drawing_CreateTypography(handler2_);
227 OH_Drawing_TypographyLayout(typography2_, DEFAULT_LAYOUT_WIDTH);
228 double longestLineFalse = OH_Drawing_TypographyGetLongestLine(typography2_);
229 double line1False = OH_Drawing_TypographyGetLineWidth(typography2_, 0);
230 double line2False = OH_Drawing_TypographyGetLineWidth(typography2_, 1);
231 double line3False = OH_Drawing_TypographyGetLineWidth(typography2_, 2);
232 EXPECT_NEAR(longestLineFalse, std::max(line1False, std::max(line2False, line3False)), FLOAT_DATA_EPSILON);
233
234 // compare paragraph1 and paragraph2
235 EXPECT_NEAR(longestLineTrue, longestLineFalse + DEFAULT_FONT_SIZE / 8 * 3, FLOAT_DATA_EPSILON);
236 EXPECT_NEAR(line1True, line1False + DEFAULT_FONT_SIZE / 8 * 2, FLOAT_DATA_EPSILON);
237 EXPECT_NEAR(line2True, line2False + DEFAULT_FONT_SIZE / 8 * 3, FLOAT_DATA_EPSILON);
238 EXPECT_NEAR(line3True, line3False + DEFAULT_FONT_SIZE / 8 * 2, FLOAT_DATA_EPSILON);
239 }
240
241 /*
242 * @tc.name: OH_Drawing_SetTypographyTextAutoSpaceTest004
243 * @tc.desc: test for set auto space when paragraph is many lines and the layout width is at the boundary value.
244 * @tc.type: FUNC
245 */
246 HWTEST_F(NativeTypographyTestAutoSpace, OHDrawingSetTypographyTextAutoSpaceTest004, Function | MediumTest | Level1)
247 {
248 std::string text = "嫌疑者X的牺牲\n版权所有©2002-2001华为技术有限公司保留一切权利\n卸载USB设备";
249 PrepareWorkForTypographyStyleTest();
250
251 // test boundary value:Use longestline without autospace as layout width when autospace enabled, line count + 1
252 // paragraph2
253 typoStyle_ = OH_Drawing_CreateTypographyStyle();
254 ASSERT_NE(typoStyle_, nullptr);
255 OH_Drawing_SetTypographyTextAutoSpace(typoStyle_, true);
256 handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
257 ASSERT_NE(handler_, nullptr);
258 OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
259 OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
260 typography_ = OH_Drawing_CreateTypography(handler_);
261 ASSERT_NE(typography_, nullptr);
262 double layoutWidth = 956.159546; // boundary value
263 OH_Drawing_TypographyLayout(typography_, layoutWidth);
264 double longestLineTrue = OH_Drawing_TypographyGetLongestLine(typography_);
265 double line1True = OH_Drawing_TypographyGetLineWidth(typography_, 0);
266 double line2True = OH_Drawing_TypographyGetLineWidth(typography_, 1);
267 double line3True = OH_Drawing_TypographyGetLineWidth(typography_, 2);
268 double line4True = OH_Drawing_TypographyGetLineWidth(typography_, 3);
269 size_t lineCount = OH_Drawing_TypographyGetLineCount(typography_);
270 EXPECT_GT(layoutWidth, longestLineTrue);
271 EXPECT_GT(layoutWidth, line1True);
272 EXPECT_GT(line3True, 0);
273
274 // paragraph2
275 typoStyle2_ = OH_Drawing_CreateTypographyStyle();
276 ASSERT_NE(typoStyle2_, nullptr);
277 OH_Drawing_SetTypographyTextAutoSpace(typoStyle2_, false);
278 handler2_ = OH_Drawing_CreateTypographyHandler(typoStyle2_, fontCollection_);
279 ASSERT_NE(handler2_, nullptr);
280 OH_Drawing_TypographyHandlerPushTextStyle(handler2_, txtStyle_);
281 OH_Drawing_TypographyHandlerAddText(handler2_, text.c_str());
282 typography2_ = OH_Drawing_CreateTypography(handler2_);
283 ASSERT_NE(typography2_, nullptr);
284 OH_Drawing_TypographyLayout(typography2_, layoutWidth);
285 double longestLineFalse2 = OH_Drawing_TypographyGetLongestLine(typography2_);
286 double line1False2 = OH_Drawing_TypographyGetLineWidth(typography2_, 0);
287 double line2False2 = OH_Drawing_TypographyGetLineWidth(typography2_, 1);
288 double line3False2 = OH_Drawing_TypographyGetLineWidth(typography2_, 2);
289 size_t lineCount2 = OH_Drawing_TypographyGetLineCount(typography2_);
290 EXPECT_NEAR(longestLineFalse2, layoutWidth, FLOAT_DATA_EPSILON);
291 EXPECT_NEAR(line2False2, layoutWidth, FLOAT_DATA_EPSILON);
292
293 // compare paragraph1 and paragraph2
294 EXPECT_GT(longestLineFalse2, longestLineTrue);
295 EXPECT_GT(line1True, line1False2);
296 // The critical width value squeezed the second line down, so the characters are fewer, and the width is shorter.
297 EXPECT_GT(line2False2, line2True);
298 EXPECT_GT(line4True, line3False2);
299 EXPECT_EQ(lineCount, lineCount2 + 1);
300 }
301 } // namespace OHOS