1 /*
2 * Copyright (c) 2025-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, 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
18 #include "text/font.h"
19 #include "text/text_blob.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class FontTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp() override;
32 void TearDown() override;
33 };
34
SetUpTestCase()35 void FontTest::SetUpTestCase() {}
TearDownTestCase()36 void FontTest::TearDownTestCase() {}
SetUp()37 void FontTest::SetUp() {}
TearDown()38 void FontTest::TearDown() {}
39
40 /**
41 * @tc.name: FontMeasureTextWithBrushOrPenTest001
42 * @tc.desc:
43 * @tc.type: FUNC
44 * @tc.require:AR20250515745872
45 * @tc.author:
46 */
47 HWTEST_F(FontTest, FontMeasureTextWithBrushOrPenTest001, TestSize.Level1)
48 {
49 Font font;
50 std::shared_ptr<OHOS::Rosen::Drawing::Typeface> zhCnTypeface = Drawing::Typeface::MakeDefault();
51 font.SetTypeface(zhCnTypeface);
52 Brush brush;
53 Pen pen;
54 const char* text = "你好世界";
55 auto textWidth = font.MeasureText(text, strlen(text), TextEncoding::UTF8, nullptr, &brush, &pen);
56 EXPECT_EQ((int)textWidth, 0);
57 }
58
59 /**
60 * @tc.name: FontGetWidthsBoundsTest001
61 * @tc.desc:
62 * @tc.type: FUNC
63 * @tc.require:AR20250515745872
64 * @tc.author:
65 */
66 HWTEST_F(FontTest, FontGetWidthsBoundsTest001, TestSize.Level1)
67 {
68 Font font;
69 std::shared_ptr<OHOS::Rosen::Drawing::Typeface> zhCnTypeface = Drawing::Typeface::MakeDefault();
70 font.SetTypeface(zhCnTypeface);
71 Brush brush;
72 Pen pen;
73 float widths[50] = {0.0f};
74 Rect* bounds = new Rect[50];
75 font.GetWidthsBounds(nullptr, 0, widths, bounds, &brush, &pen);
76 EXPECT_EQ((int)widths[0], 0);
77 ASSERT_NE(bounds, nullptr);
78 if (bounds != nullptr) {
79 delete [] bounds;
80 bounds = nullptr;
81 }
82 }
83
84 /**
85 * @tc.name: FontGetPosTest001
86 * @tc.desc:
87 * @tc.type: FUNC
88 * @tc.require:AR20250515745872
89 * @tc.author:
90 */
91 HWTEST_F(FontTest, FontGetPosTest001, TestSize.Level1)
92 {
93 Font font;
94 std::shared_ptr<OHOS::Rosen::Drawing::Typeface> zhCnTypeface = Drawing::Typeface::MakeDefault();
95 font.SetTypeface(zhCnTypeface);
96 uint16_t glyphs[] = { 0, 0 };
97 Point points[] = { {0, 0}, {0, 0} };
98 font.GetPos(glyphs, 2, points, {10, 10}); // 2:count
99 ASSERT_NE((int)points[0].GetX(), 0);
100 ASSERT_NE((int)points[0].GetY(), 0);
101 }
102
103 /**
104 * @tc.name: FontGetSpacingTest001
105 * @tc.desc:
106 * @tc.type: FUNC
107 * @tc.require:AR20250515745872
108 * @tc.author:
109 */
110 HWTEST_F(FontTest, FontGetSpacingTest001, TestSize.Level1)
111 {
112 Font font;
113 std::shared_ptr<OHOS::Rosen::Drawing::Typeface> zhCnTypeface = Drawing::Typeface::MakeDefault();
114 font.SetTypeface(zhCnTypeface);
115 auto spacing = font.GetSpacing();
116 ASSERT_NE((int)spacing, 0);
117 }
118
119 /**
120 * @tc.name: FontUnicharToGlyphWithFeatures001
121 * @tc.desc:
122 * @tc.type: FUNC
123 * @tc.require:ICG6L3
124 * @tc.author:
125 */
126 HWTEST_F(FontTest, FontUnicharToGlyphWithFeatures001, TestSize.Level1)
127 {
128 Font font;
129 const char* str = "a";
130 const char* strEmpty = "";
131 std::shared_ptr<Drawing::DrawingFontFeatures> features = std::make_shared<Drawing::DrawingFontFeatures>();
132 uint16_t glyph = font.UnicharToGlyphWithFeatures(str, nullptr);
133 ASSERT_EQ(glyph, 0);
134 glyph = font.UnicharToGlyphWithFeatures(strEmpty, features);
135 ASSERT_EQ(glyph, 0);
136 std::shared_ptr<OHOS::Rosen::Drawing::Typeface> zhCnTypeface = Drawing::Typeface::MakeDefault();
137 font.SetTypeface(zhCnTypeface);
138 glyph = font.UnicharToGlyphWithFeatures(strEmpty, features);
139 ASSERT_EQ(glyph, 0);
140 }
141
142 /**
143 * @tc.name: MeasureSingleCharacterWithFeatures001
144 * @tc.desc:
145 * @tc.type: FUNC
146 * @tc.require:ICG6L3
147 * @tc.author:
148 */
149 HWTEST_F(FontTest, MeasureSingleCharacterWithFeatures001, TestSize.Level1)
150 {
151 Font font;
152 const char* str = "a";
153 float width = font.MeasureSingleCharacterWithFeatures(str, 0, nullptr);
154 ASSERT_NE(width, 0);
155 const char* strNoFallback = "\uE000";
156 uint32_t unicodeNoFallback = 0xE000;
157 width = font.MeasureSingleCharacterWithFeatures(strNoFallback, unicodeNoFallback, nullptr);
158 ASSERT_EQ(width, 0);
159 }
160 } // namespace Drawing
161 } // namespace Rosen
162 } // namespace OHOS
163