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 <cstddef>
17 #include "gtest/gtest.h"
18 #include "skia_adapter/skia_font.h"
19 #include "text/font.h"
20 #ifdef USE_M133_SKIA
21 #include "include/core/SkFontTypes.h"
22 #endif
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
30 class SkiaFontTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36 };
37
SetUpTestCase()38 void SkiaFontTest::SetUpTestCase() {}
TearDownTestCase()39 void SkiaFontTest::TearDownTestCase() {}
SetUp()40 void SkiaFontTest::SetUp() {}
TearDown()41 void SkiaFontTest::TearDown() {}
42
43 /**
44 * @tc.name: SkiaFont001
45 * @tc.desc: Test SkiaFont's constructor
46 * @tc.type: FUNC
47 * @tc.require: I9120P
48 */
49 HWTEST_F(SkiaFontTest, SkiaFont001, TestSize.Level1)
50 {
51 std::shared_ptr<SkiaFont> skiaFont = std::make_shared<SkiaFont>(nullptr, 1, 1, 1);
52 ASSERT_TRUE(skiaFont != nullptr);
53 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
54 skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
55 ASSERT_TRUE(skiaFont != nullptr);
56 }
57
58 /**
59 * @tc.name: SetEdging001
60 * @tc.desc: Test SetEdging
61 * @tc.type: FUNC
62 * @tc.require: I9120P
63 */
64 HWTEST_F(SkiaFontTest, SetEdging001, TestSize.Level1)
65 {
66 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
67 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
68 skiaFont->SetEdging(FontEdging::ALIAS);
69 ASSERT_TRUE(skiaFont->GetFont().getEdging() == SkFont::Edging::kAlias);
70 }
71
72 /**
73 * @tc.name: SetSubpixel001
74 * @tc.desc: Test SetSubpixel
75 * @tc.type: FUNC
76 * @tc.require: I9120P
77 */
78 HWTEST_F(SkiaFontTest, SetSubpixel001, TestSize.Level1)
79 {
80 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
81 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
82 skiaFont->SetSubpixel(true);
83 ASSERT_TRUE(skiaFont->GetFont().isSubpixel());
84 }
85
86 /**
87 * @tc.name: SetHinting001
88 * @tc.desc: Test SetHinting
89 * @tc.type: FUNC
90 * @tc.require: I9120P
91 */
92 HWTEST_F(SkiaFontTest, SetHinting001, TestSize.Level1)
93 {
94 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
95 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
96 skiaFont->SetHinting(FontHinting::NONE);
97 ASSERT_TRUE(skiaFont->GetFont().getHinting() == SkFontHinting::kNone);
98 }
99
100 /**
101 * @tc.name: SetTypeface001
102 * @tc.desc: Test SetTypeface
103 * @tc.type: FUNC
104 * @tc.require: I9120P
105 */
106 HWTEST_F(SkiaFontTest, SetTypeface001, TestSize.Level1)
107 {
108 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
109 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
110 skiaFont->SetTypeface(nullptr);
111 ASSERT_TRUE(skiaFont->GetTypeface() != nullptr);
112 }
113
114 /**
115 * @tc.name: SetScaleX001
116 * @tc.desc: Test SetScaleX
117 * @tc.type: FUNC
118 * @tc.require: I9120P
119 */
120 HWTEST_F(SkiaFontTest, SetScaleX001, TestSize.Level1)
121 {
122 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
123 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
124 skiaFont->SetScaleX(1);
125 ASSERT_TRUE(skiaFont->GetFont().getScaleX() == 1);
126 }
127
128 /**
129 * @tc.name: SetLinearMetrics001
130 * @tc.desc: Test SetLinearMetrics
131 * @tc.type: FUNC
132 * @tc.require: I9120P
133 */
134 HWTEST_F(SkiaFontTest, SetLinearMetrics001, TestSize.Level1)
135 {
136 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
137 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
138 skiaFont->SetLinearMetrics(true);
139 ASSERT_TRUE(skiaFont->GetFont().isLinearMetrics());
140 }
141
142 /**
143 * @tc.name: GetMetrics001
144 * @tc.desc: Test GetMetrics
145 * @tc.type: FUNC
146 * @tc.require: I9120P
147 */
148 HWTEST_F(SkiaFontTest, GetMetrics001, TestSize.Level1)
149 {
150 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
151 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
152 auto metrics = skiaFont->GetMetrics(nullptr);
153 ASSERT_TRUE(metrics > 0);
154 }
155
156 /**
157 * @tc.name: GetWidths001
158 * @tc.desc: Test GetWidths
159 * @tc.type: FUNC
160 * @tc.require: I9120P
161 */
162 HWTEST_F(SkiaFontTest, GetWidths001, TestSize.Level1)
163 {
164 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
165 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
166 uint16_t glyphs[] = { 0, 0 };
167 scalar widths[] = { 0, 0 };
168 skiaFont->GetWidths(glyphs, 2, widths); // 2:count
169 skiaFont->GetWidths(glyphs, 2, widths, nullptr); // 2:count
170 Rect rect1;
171 Rect rect2;
172 Rect bounds[] = { rect1, rect2 };
173 skiaFont->GetWidths(glyphs, 2, widths, bounds);
174 ASSERT_TRUE(rect1.GetWidth() >= 0);
175 }
176
177 /**
178 * @tc.name: GetSize001
179 * @tc.desc: Test GetSize
180 * @tc.type: FUNC
181 * @tc.require: I9120P
182 */
183 HWTEST_F(SkiaFontTest, GetSize001, TestSize.Level1)
184 {
185 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
186 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
187 auto size = skiaFont->GetSize();
188 ASSERT_TRUE(size > 0);
189 }
190
191 /**
192 * @tc.name: GetTypeface001
193 * @tc.desc: Test GetTypeface
194 * @tc.type: FUNC
195 * @tc.require: I9120P
196 */
197 HWTEST_F(SkiaFontTest, GetTypeface001, TestSize.Level1)
198 {
199 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
200 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
201 auto typeface2 = skiaFont->GetTypeface();
202 ASSERT_TRUE(typeface2 != nullptr);
203 }
204
205 /**
206 * @tc.name: MeasureText001
207 * @tc.desc: Test MeasureText
208 * @tc.type: FUNC
209 * @tc.require: I9120P
210 */
211 HWTEST_F(SkiaFontTest, MeasureText001, TestSize.Level1)
212 {
213 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
214 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
215 auto size = skiaFont->MeasureText("11", 2, TextEncoding::UTF8, nullptr); // 2:byteLength
216 ASSERT_TRUE(size > 0);
217 }
218
219 /**
220 * @tc.name: MeasureText002
221 * @tc.desc: Test MeasureText
222 * @tc.type: FUNC
223 * @tc.require: AR20250515745872
224 */
225 HWTEST_F(SkiaFontTest, MeasureText002, TestSize.Level1)
226 {
227 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
228 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
229 auto size = skiaFont->MeasureText(nullptr, 2, TextEncoding::UTF8, nullptr, nullptr, nullptr); // 2:byteLength
230 EXPECT_EQ((int)size, 0);
231
232 size = skiaFont->MeasureText("11", 2, TextEncoding::UTF8, nullptr, nullptr, nullptr); // 2:byteLength
233 EXPECT_EQ((size - 1.1399231) < 1e-6, true);
234
235 Brush brush;
236 size = skiaFont->MeasureText("11", 2, TextEncoding::UTF8, nullptr, &brush, nullptr); // 2:byteLength
237 EXPECT_EQ((size - 1.1399231) < 1e-6, true);
238
239 Pen pen;
240 size = skiaFont->MeasureText("11", 2, TextEncoding::UTF8, nullptr, nullptr, &pen); // 2:byteLength
241 EXPECT_EQ((size - 1.13999796) < 1e-6, true);
242 }
243
244 /**
245 * @tc.name: GetWidthsBoundsTest001
246 * @tc.desc: Test GetWidthsBounds
247 * @tc.type: FUNC
248 * @tc.require: AR20250515745872
249 */
250 HWTEST_F(SkiaFontTest, GetWidthsBoundsTest001, TestSize.Level1)
251 {
252 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
253 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
254 uint16_t glyphs[] = { 0, 0 };
255 scalar widths[] = { 0, 0 };
256 Rect rect1;
257 Rect rect2;
258 Rect bounds[] = { rect1, rect2 };
259 skiaFont->GetWidthsBounds(glyphs, 0, widths, nullptr, nullptr, nullptr);
260 EXPECT_EQ((int)widths[0], 0);
261
262 skiaFont->GetWidthsBounds(glyphs, 2, widths, nullptr, nullptr, nullptr); // 2:count
263 EXPECT_EQ((int)widths[0], 0);
264
265 skiaFont->GetWidthsBounds(glyphs, 2, widths, bounds, nullptr, nullptr); // 2:count
266 EXPECT_EQ(rect1.GetWidth(), 0);
267
268 Brush brush;
269 skiaFont->GetWidthsBounds(glyphs, 2, widths, bounds, &brush, nullptr); // 2:count
270 EXPECT_EQ(rect1.GetWidth(), 0);
271
272 Pen pen;
273 skiaFont->GetWidthsBounds(glyphs, 2, widths, bounds, nullptr, &pen); // 2:count
274 EXPECT_EQ(rect1.GetWidth(), 0);
275 }
276
277 /**
278 * @tc.name: GetPosTest001
279 * @tc.desc: Test GetPos
280 * @tc.type: FUNC
281 * @tc.require: AR20250515745872
282 */
283 HWTEST_F(SkiaFontTest, GetPosTest001, TestSize.Level1)
284 {
285 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
286 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
287 uint16_t glyphs[] = { 0, 0 };
288 Point points[] = { {0, 0}, {0, 0} };
289 skiaFont->GetPos(glyphs, 0, points);
290 EXPECT_EQ(points[0].GetX(), 0);
291 EXPECT_EQ(points[0].GetY(), 0);
292 Point origin(10, 10);
293 skiaFont->GetPos(glyphs, 2, points, origin); // 2:count
294 EXPECT_EQ(points[0].GetX(), 10);
295 EXPECT_EQ(points[0].GetY(), 10);
296 }
297
298 /**
299 * @tc.name: GetSpacingTest001
300 * @tc.desc: Test GetSpacing
301 * @tc.type: FUNC
302 * @tc.require: AR20250515745872
303 */
304 HWTEST_F(SkiaFontTest, GetSpacingTest001, TestSize.Level1)
305 {
306 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
307 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
308 auto space = skiaFont->GetSpacing();
309 EXPECT_EQ((int)space, 1);
310 }
311 } // namespace Drawing
312 } // namespace Rosen
313 } // namespace OHOS