1/*
2 * Copyright (c) 2022-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, Hardware
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 "drawing_brush.h"
18#include "drawing_font.h"
19#include "drawing_point.h"
20#include "drawing_text_typography.h"
21#include "drawing_font_collection.h"
22
23using namespace testing;
24using namespace testing::ext;
25
26namespace OHOS {
27namespace Rosen {
28namespace Drawing {
29class NativeFontTest : public testing::Test {
30public:
31    static void SetUpTestCase();
32    static void TearDownTestCase();
33    void SetUp() override;
34    void TearDown() override;
35};
36
37void NativeFontTest::SetUpTestCase() {}
38void NativeFontTest::TearDownTestCase() {}
39void NativeFontTest::SetUp() {}
40void NativeFontTest::TearDown() {}
41
42/*
43 * @tc.name: NativeFontTest_GetMetrics001
44 * @tc.desc: test for GetMetrics.
45 * @tc.size  : MediumTest
46 * @tc.type  : Function
47 * @tc.level : Level 1
48 */
49HWTEST_F(NativeFontTest, NativeFontTest_GetMetrics001, TestSize.Level1)
50{
51    OH_Drawing_Font* font = OH_Drawing_FontCreate();
52    EXPECT_NE(font, nullptr);
53    OH_Drawing_Font_Metrics cFontMetrics;
54    EXPECT_TRUE(OH_Drawing_FontGetMetrics(font, &cFontMetrics) >= 0);
55    EXPECT_TRUE(OH_Drawing_FontGetMetrics(font, nullptr) < 0);
56    EXPECT_TRUE(OH_Drawing_FontGetMetrics(nullptr, nullptr) < 0);
57    OH_Drawing_FontDestroy(font);
58}
59
60/*
61 * @tc.name: NativeFontTest_IsAndSetBaselineSnap002
62 * @tc.desc: test for SetBaselineSnap and IsBaselineSnap.
63 * @tc.size  : MediumTest
64 * @tc.type  : Function
65 * @tc.level : Level 1
66 */
67HWTEST_F(NativeFontTest, NativeFontTest_IsAndSetBaselineSnap002, TestSize.Level1)
68{
69    OH_Drawing_Font* font = OH_Drawing_FontCreate();
70    EXPECT_NE(font, nullptr);
71    OH_Drawing_FontSetBaselineSnap(nullptr, true);
72    EXPECT_EQ(OH_Drawing_FontIsBaselineSnap(nullptr), false);
73    OH_Drawing_FontSetBaselineSnap(nullptr, false);
74    EXPECT_EQ(OH_Drawing_FontIsBaselineSnap(nullptr), false);
75    OH_Drawing_FontSetBaselineSnap(font, true);
76    EXPECT_EQ(OH_Drawing_FontIsBaselineSnap(font), true);
77    OH_Drawing_FontSetBaselineSnap(font, false);
78    EXPECT_EQ(OH_Drawing_FontIsBaselineSnap(font), false);
79    OH_Drawing_FontDestroy(font);
80}
81
82/*
83 * @tc.name: NativeFontTest_IsAndSetSubpixel003
84 * @tc.desc: test for SetSubpixel and IsSubpixel.
85 * @tc.size  : MediumTest
86 * @tc.type  : Function
87 * @tc.level : Level 1
88 */
89HWTEST_F(NativeFontTest, NativeFontTest_IsAndSetSubpixel003, TestSize.Level1)
90{
91    OH_Drawing_Font* font = OH_Drawing_FontCreate();
92    EXPECT_NE(font, nullptr);
93    OH_Drawing_FontSetSubpixel(nullptr, false);
94    EXPECT_EQ(OH_Drawing_FontIsSubpixel(nullptr), false);
95    OH_Drawing_FontSetSubpixel(nullptr, true);
96    EXPECT_EQ(OH_Drawing_FontIsSubpixel(nullptr), false);
97    OH_Drawing_FontSetSubpixel(font, true);
98    EXPECT_EQ(OH_Drawing_FontIsSubpixel(font), true);
99    OH_Drawing_FontSetSubpixel(font, false);
100    EXPECT_EQ(OH_Drawing_FontIsSubpixel(font), false);
101    OH_Drawing_FontDestroy(font);
102}
103
104/*
105 * @tc.name: NativeFontTest_TextToGlyphs004
106 * @tc.desc: test for TextToGlyphs.
107 * @tc.size  : MediumTest
108 * @tc.type  : Function
109 * @tc.level : Level 1
110 */
111HWTEST_F(NativeFontTest, NativeFontTest_TextToGlyphs004, TestSize.Level1)
112{
113    OH_Drawing_Font* font = OH_Drawing_FontCreate();
114    OH_Drawing_FontSetTextSize(font, 100); // 100 means font text size
115    EXPECT_NE(font, nullptr);
116    const char *str = "hello world";
117    uint32_t count = 0;
118    count = OH_Drawing_FontCountText(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8);
119    EXPECT_EQ(11, count); // 11 means str length
120
121    uint16_t glyphs[50] = {0}; // 50 means glyphs array number
122    int glyphsCount = 0;
123    glyphsCount = OH_Drawing_FontTextToGlyphs(font, str, 0,
124        OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8, glyphs, 0);
125    EXPECT_EQ(0, glyphsCount);
126
127    glyphsCount = OH_Drawing_FontTextToGlyphs(font, str, strlen(str),
128        OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8, glyphs, count);
129    EXPECT_EQ(11, glyphsCount); // 11 means glyphsCount
130
131    float widths[50] = {0.f}; // 50 means widths array number
132    OH_Drawing_FontGetWidths(font, glyphs, glyphsCount, widths);
133    EXPECT_EQ(58.0, widths[0]); // 58.0 means glyphs[0] width
134    OH_Drawing_FontDestroy(font);
135}
136
137/*
138 * @tc.name: NativeFontTest_SetAndGetScaleX005
139 * @tc.desc: test for SetAndGetScaleX.
140 * @tc.size  : MediumTest
141 * @tc.type  : Function
142 * @tc.level : Level 1
143 */
144HWTEST_F(NativeFontTest, NativeFontTest_SetAndGetScaleX005, TestSize.Level1)
145{
146    OH_Drawing_Font* font = OH_Drawing_FontCreate();
147    EXPECT_NE(font, nullptr);
148    OH_Drawing_FontSetScaleX(nullptr, 2);
149    EXPECT_TRUE(OH_Drawing_FontGetScaleX(nullptr) == -1);
150    EXPECT_TRUE(OH_Drawing_FontGetScaleX(font) == 1);
151    OH_Drawing_FontSetScaleX(font, 2);
152    EXPECT_TRUE(OH_Drawing_FontGetScaleX(font) == 2);
153    OH_Drawing_FontDestroy(font);
154}
155
156/*
157 * @tc.name: NativeFontTest_GetAndSetEdging006
158 * @tc.desc: test for GetAndSetEdging.
159 * @tc.size  : MediumTest
160 * @tc.type  : Function
161 * @tc.level : Level 1
162 */
163HWTEST_F(NativeFontTest, NativeFontTest_GetAndSetEdging006, TestSize.Level1)
164{
165    OH_Drawing_Font* font = OH_Drawing_FontCreate();
166    EXPECT_NE(font, nullptr);
167    EXPECT_EQ(OH_Drawing_FontGetEdging(font), OH_Drawing_FontEdging::FONT_EDGING_ANTI_ALIAS);
168    EXPECT_EQ(OH_Drawing_FontGetEdging(nullptr), OH_Drawing_FontEdging::FONT_EDGING_ALIAS);
169    OH_Drawing_FontSetEdging(nullptr, OH_Drawing_FontEdging::FONT_EDGING_ALIAS);
170    EXPECT_EQ(OH_Drawing_FontGetEdging(font), OH_Drawing_FontEdging::FONT_EDGING_ANTI_ALIAS);
171    OH_Drawing_FontSetEdging(font, OH_Drawing_FontEdging::FONT_EDGING_ALIAS);
172    EXPECT_EQ(OH_Drawing_FontGetEdging(font), OH_Drawing_FontEdging::FONT_EDGING_ALIAS);
173    OH_Drawing_FontSetEdging(font, OH_Drawing_FontEdging::FONT_EDGING_ANTI_ALIAS);
174    EXPECT_EQ(OH_Drawing_FontGetEdging(font), OH_Drawing_FontEdging::FONT_EDGING_ANTI_ALIAS);
175    OH_Drawing_FontSetEdging(font, OH_Drawing_FontEdging::FONT_EDGING_SUBPIXEL_ANTI_ALIAS);
176    EXPECT_EQ(OH_Drawing_FontGetEdging(font), OH_Drawing_FontEdging::FONT_EDGING_SUBPIXEL_ANTI_ALIAS);
177    OH_Drawing_FontDestroy(font);
178}
179
180/*
181 * @tc.name: NativeFontTest_GetAndSetForceAutoHinting007
182 * @tc.desc: test for GetAndSetForceAutoHinting.
183 * @tc.size  : MediumTest
184 * @tc.type  : Function
185 * @tc.level : Level 1
186 */
187HWTEST_F(NativeFontTest, NativeFontTest_GetAndSetForceAutoHinting007, TestSize.Level1)
188{
189    OH_Drawing_Font* font = OH_Drawing_FontCreate();
190    EXPECT_NE(font, nullptr);
191    EXPECT_EQ(OH_Drawing_FontIsForceAutoHinting(nullptr), false);
192    OH_Drawing_FontSetForceAutoHinting(nullptr, true);
193    EXPECT_EQ(OH_Drawing_FontIsForceAutoHinting(font), false);
194    OH_Drawing_FontSetForceAutoHinting(font, true);
195    EXPECT_EQ(OH_Drawing_FontIsForceAutoHinting(font), true);
196    OH_Drawing_FontSetForceAutoHinting(font, false);
197    EXPECT_EQ(OH_Drawing_FontIsForceAutoHinting(font), false);
198    OH_Drawing_FontDestroy(font);
199}
200
201/*
202 * @tc.name: NativeFontTest_GetAndSetHinting008
203 * @tc.desc: test for GetHinting and SetHinting.
204 * @tc.size  : MediumTest
205 * @tc.type  : Function
206 * @tc.level : Level 1
207 */
208HWTEST_F(NativeFontTest, NativeFontTest_GetAndSetHinting008, TestSize.Level1)
209{
210    OH_Drawing_Font* font = OH_Drawing_FontCreate();
211    EXPECT_NE(font, nullptr);
212    EXPECT_TRUE(OH_Drawing_FontGetHinting(nullptr) == OH_Drawing_FontHinting::FONT_HINTING_NONE);
213    OH_Drawing_FontSetHinting(font, OH_Drawing_FontHinting::FONT_HINTING_NONE);
214    EXPECT_TRUE(OH_Drawing_FontGetHinting(font) == OH_Drawing_FontHinting::FONT_HINTING_NONE);
215    OH_Drawing_FontSetHinting(font, OH_Drawing_FontHinting::FONT_HINTING_SLIGHT);
216    EXPECT_TRUE(OH_Drawing_FontGetHinting(font) == OH_Drawing_FontHinting::FONT_HINTING_SLIGHT);
217    OH_Drawing_FontSetHinting(font, OH_Drawing_FontHinting::FONT_HINTING_SLIGHT);
218    EXPECT_TRUE(OH_Drawing_FontGetHinting(font) == OH_Drawing_FontHinting::FONT_HINTING_SLIGHT);
219    OH_Drawing_FontDestroy(font);
220}
221
222/*
223 * @tc.name: NativeFontTest_GetAndSetEmbeddedBitmaps009
224 * @tc.desc: test for GetEmbeddedBitmaps and SetEmbeddedBitmaps.
225 * @tc.size  : MediumTest
226 * @tc.type  : Function
227 * @tc.level : Level 1
228 */
229HWTEST_F(NativeFontTest, NativeFontTest_GetAndSetEmbeddedBitmaps009, TestSize.Level1)
230{
231    OH_Drawing_Font* font = OH_Drawing_FontCreate();
232    EXPECT_NE(font, nullptr);
233    EXPECT_TRUE(OH_Drawing_FontIsEmbeddedBitmaps(nullptr) == false);
234    OH_Drawing_FontSetEmbeddedBitmaps(font, true);
235    EXPECT_TRUE(OH_Drawing_FontIsEmbeddedBitmaps(font) == true);
236    OH_Drawing_FontSetEmbeddedBitmaps(font, false);
237    EXPECT_TRUE(OH_Drawing_FontIsEmbeddedBitmaps(font) == false);
238    OH_Drawing_FontDestroy(font);
239}
240
241/*
242 * @tc.name: NativeFontTest_GetTextSize010
243 * @tc.desc: test for GetTextSize.
244 * @tc.size  : MediumTest
245 * @tc.type  : Function
246 * @tc.level : Level 1
247 */
248HWTEST_F(NativeFontTest, NativeFontTest_GetTextSize010, TestSize.Level1)
249{
250    OH_Drawing_Font* font = OH_Drawing_FontCreate();
251    EXPECT_NE(font, nullptr);
252    OH_Drawing_FontSetTextSize(font, 100);
253    float size = OH_Drawing_FontGetTextSize(font);
254    EXPECT_EQ(size, 100);
255    OH_Drawing_FontDestroy(font);
256}
257
258/*
259 * @tc.name: NativeFontTest_GetTextSkewX011
260 * @tc.desc: test for GetTextSkewX.
261 * @tc.size  : MediumTest
262 * @tc.type  : Function
263 * @tc.level : Level 1
264 */
265HWTEST_F(NativeFontTest, NativeFontTest_GetTextSkewX011, TestSize.Level1)
266{
267    OH_Drawing_Font* font = OH_Drawing_FontCreate();
268    EXPECT_NE(font, nullptr);
269    OH_Drawing_FontSetTextSkewX(font, 10);
270    float size = OH_Drawing_FontGetTextSkewX(font);
271    EXPECT_EQ(size, 10);
272    OH_Drawing_FontDestroy(font);
273}
274
275/*
276 * @tc.name: NativeFontTest_IsLinearText012
277 * @tc.desc: test for IsLinearText.
278 * @tc.size  : MediumTest
279 * @tc.type  : Function
280 * @tc.level : Level 1
281 */
282HWTEST_F(NativeFontTest, NativeFontTest_IsLinearText012, TestSize.Level1)
283{
284    OH_Drawing_Font* font = OH_Drawing_FontCreate();
285    EXPECT_NE(font, nullptr);
286    bool ret = OH_Drawing_FontIsLinearText(font);
287    EXPECT_EQ(ret, false);
288    OH_Drawing_FontSetLinearText(font, true);
289    ret = OH_Drawing_FontIsLinearText(font);
290    EXPECT_EQ(ret, true);
291    OH_Drawing_FontDestroy(font);
292}
293
294/*
295 * @tc.name: NativeFontTest_SetFakeBoldText013
296 * @tc.desc: test for SetFakeBoldText.
297 * @tc.size  : MediumTest
298 * @tc.type  : Function
299 * @tc.level : Level 1
300 */
301HWTEST_F(NativeFontTest, NativeFontTest_SetFakeBoldText013, TestSize.Level1)
302{
303    OH_Drawing_Font* font = OH_Drawing_FontCreate();
304    EXPECT_NE(font, nullptr);
305    bool ret = OH_Drawing_FontIsFakeBoldText(font);
306    EXPECT_EQ(ret, false);
307    OH_Drawing_FontSetFakeBoldText(font, true);
308    ret = OH_Drawing_FontIsFakeBoldText(font);
309    EXPECT_EQ(ret, true);
310    OH_Drawing_FontDestroy(font);
311}
312
313/*
314 * @tc.name: NativeFontTest_FontMeasureText014
315 * @tc.desc: test for FontMeasureText.
316 * @tc.type: FUNC
317 * @tc.require: AR000GTO5R
318 */
319HWTEST_F(NativeFontTest, NativeFontTest_FontMeasureText014, TestSize.Level1)
320{
321    OH_Drawing_Font* font = OH_Drawing_FontCreate();
322    EXPECT_NE(font, nullptr);
323    OH_Drawing_FontSetTextSize(font, 50);
324    const char* str = "hello world";
325    float textWidth = 0.f;
326    OH_Drawing_ErrorCode drawingErrorCode = OH_DRAWING_SUCCESS;
327    drawingErrorCode = OH_Drawing_FontMeasureText(nullptr, str, strlen(str),
328                                                  OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8, nullptr, &textWidth);
329    EXPECT_EQ(drawingErrorCode, OH_DRAWING_ERROR_INVALID_PARAMETER);
330    EXPECT_EQ(textWidth, 0.f);
331    drawingErrorCode = OH_Drawing_FontMeasureText(font, str, 0, OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8,
332                                                  nullptr, &textWidth);
333    EXPECT_EQ(drawingErrorCode, OH_DRAWING_ERROR_INVALID_PARAMETER);
334    EXPECT_EQ(textWidth, 0.f);
335    drawingErrorCode = OH_Drawing_FontMeasureText(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8,
336                                                  nullptr, &textWidth);
337    EXPECT_EQ(drawingErrorCode, OH_DRAWING_SUCCESS);
338    EXPECT_EQ(textWidth, 254.0); // 254.0 is textWidth
339
340    OH_Drawing_FontDestroy(font);
341}
342
343
344/*
345 * @tc.name: NativeFontTest_FontMeasureText015
346 * @tc.desc: test for the textbox.
347 * @tc.type: FUNC
348 */
349HWTEST_F(NativeFontTest, NativeFontTest_FontMeasureText015, TestSize.Level1)
350{
351    OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
352    OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle,
353        OH_Drawing_CreateFontCollection());
354    OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler);
355    OH_Drawing_TextBox* textBox = OH_Drawing_TypographyGetRectsForPlaceholders(typography);
356    EXPECT_EQ(textBox == nullptr, false);
357    OH_Drawing_DestroyTypographyStyle(typoStyle);
358    OH_Drawing_DestroyTypographyHandler(handler);
359    OH_Drawing_DestroyTypography(typography);
360    OH_Drawing_TypographyDestroyTextBox(textBox);
361}
362
363/*
364 * @tc.name: NativeFontTest_FontMeasureText016
365 * @tc.desc: test for the textshadow.
366 * @tc.type: FUNC
367 */
368HWTEST_F(NativeFontTest, NativeFontTest_FontMeasureText016, TestSize.Level1)
369{
370    OH_Drawing_TextShadow* shadow = OH_Drawing_CreateTextShadow();
371    uint32_t color = 0;
372    OH_Drawing_Point* offset = OH_Drawing_PointCreate(0, 0);
373    double blurRadius = 0.0;
374    OH_Drawing_SetTextShadow(shadow, color, offset, blurRadius);
375    OH_Drawing_DestroyTextShadow(shadow);
376    OH_Drawing_PointDestroy(offset);
377    EXPECT_TRUE(shadow != nullptr);
378}
379
380/*
381 * @tc.name: NativeFontTest_FontMeasureText017
382 * @tc.desc: test for the fontVariation.
383 * @tc.type: FUNC
384 */
385HWTEST_F(NativeFontTest, NativeFontTest_FontMeasureText017, TestSize.Level1)
386{
387    OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
388    EXPECT_EQ(txtStyle == nullptr, false);
389    const char* key = "宋体";
390    int value = 1;
391    OH_Drawing_TextStyleAddFontFeature(txtStyle, key, value);
392    OH_Drawing_TextStyleAddFontVariation(txtStyle, key, value);
393    EXPECT_EQ(OH_Drawing_TextStyleGetFontFeatureSize(txtStyle), 1);
394    OH_Drawing_FontCollection* fontCollection = OH_Drawing_CreateFontCollection();
395    OH_Drawing_ClearFontCaches(fontCollection);
396    EXPECT_EQ(fontCollection == nullptr, false);
397    OH_Drawing_DestroyFontCollection(fontCollection);
398    OH_Drawing_DestroyTextStyle(txtStyle);
399}
400
401} // namespace Drawing
402} // namespace Rosen
403} // namespace OHOS