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 <fcntl.h>
17 #include <fstream>
18 #include <iostream>
19 #include "gtest/gtest.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkStream.h"
22 #include "include/core/SkTypeface.h"
23 #include "SkFontMgr.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 class SkFontMgrOhosTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 private:
35 static sk_sp<SkFontMgr> m_fontMgrOhosImpl;
36 };
37
38 sk_sp<SkFontMgr> SkFontMgrOhosTest::m_fontMgrOhosImpl = nullptr;
39
SetUpTestCase()40 void SkFontMgrOhosTest::SetUpTestCase()
41 {
42 m_fontMgrOhosImpl = SkFontMgr::RefDefault();
43 }
TearDownTestCase()44 void SkFontMgrOhosTest::TearDownTestCase()
45 {
46 m_fontMgrOhosImpl = nullptr;
47 }
SetUp()48 void SkFontMgrOhosTest::SetUp() {}
TearDown()49 void SkFontMgrOhosTest::TearDown() {}
50
51 const std::string BASE_NAME = {0x48, 0x61, 0x72, 0x6D, 0x6F, 0x6E, 0x79, 0x4F, 0x53};
52
53 const char TTF_FILE_PATH[] = {0x2F, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2F, 0x66, 0x6F, 0x6E, 0x74, 0x73,
54 0x2F, 0x48, 0x61, 0x72, 0x6D, 0x6F, 0x6E, 0x79, 0x4F, 0x53, 0x5F, 0x53, 0x61, 0x6E, 0x73, 0x5F,
55 0x53, 0x43, 0x2E, 0x74, 0x74, 0x66, 0x00};
56
57 /**
58 * @tc.name: GetFontFullName001
59 * @tc.desc: Test GetFontFullName
60 * @tc.type: FUNC
61 */
62 HWTEST_F(SkFontMgrOhosTest, GetFontFullName001, TestSize.Level1)
63 {
64 int fd = open(TTF_FILE_PATH, O_RDONLY);
65 ASSERT_NE(fd, -1);
66 std::vector<SkByteArray> fullnameVec;
67 int ret = m_fontMgrOhosImpl->GetFontFullName(fd, fullnameVec);
68 close(fd);
69 EXPECT_EQ(ret, SUCCESSED);
70 std::string testRes = BASE_NAME + " Sans SC";
71 ASSERT_EQ(fullnameVec.size(), 1);
72 ASSERT_NE(fullnameVec[0].strData, nullptr);
73 ASSERT_EQ(fullnameVec[0].strLen, testRes.size() * 2);
74 }
75
76 /**
77 * @tc.name: CountFamilies001
78 * @tc.desc: Test CountFamilies
79 * @tc.type: FUNC
80 */
81 HWTEST_F(SkFontMgrOhosTest, CountFamilies001, TestSize.Level1)
82 {
83 int count = m_fontMgrOhosImpl->countFamilies();
84 ASSERT_EQ(count, 10);
85 }
86
87 /**
88 * @tc.name: GetFamilyName001
89 * @tc.desc: Test GetFamilyName
90 * @tc.type: FUNC
91 */
92 HWTEST_F(SkFontMgrOhosTest, GetFamilyName001, TestSize.Level1)
93 {
94 SkString str;
95 m_fontMgrOhosImpl->getFamilyName(0, &str);
96 std::string testRes1 = BASE_NAME + "-Sans";
97 EXPECT_STREQ(str.c_str(), testRes1.c_str());
98 m_fontMgrOhosImpl->getFamilyName(5, &str);
99 std::string testRes2 = BASE_NAME + "-Sans-Condensed";
100 EXPECT_STREQ(str.c_str(), testRes2.c_str());
101 m_fontMgrOhosImpl->getFamilyName(6, &str);
102 std::string testRes3 = BASE_NAME + "-Sans-Digit";
103 EXPECT_STREQ(str.c_str(), testRes3.c_str());
104 m_fontMgrOhosImpl->getFamilyName(7, &str);
105 EXPECT_STREQ(str.c_str(), "serif");
106 m_fontMgrOhosImpl->getFamilyName(8, &str);
107 EXPECT_STREQ(str.c_str(), "monospace");
108 }
109
110 /**
111 * @tc.name: CreateStyleSet001
112 * @tc.desc: Test CreateStyleSet
113 * @tc.type: FUNC
114 */
115 HWTEST_F(SkFontMgrOhosTest, CreateStyleSet001, TestSize.Level1)
116 {
117 #ifdef USE_M133_SKIA
118 sk_sp<SkFontStyleSet> styleSet = m_fontMgrOhosImpl->createStyleSet(9);
119 #else
120 SkFontStyleSet* styleSet = m_fontMgrOhosImpl->createStyleSet(9);
121 #endif
122 ASSERT_NE(styleSet, nullptr);
123 ASSERT_EQ(styleSet->count(), 1);
124 #ifndef USE_M133_SKIA
125 delete styleSet;
126 #endif
127 styleSet = nullptr;
128
129 styleSet = m_fontMgrOhosImpl->createStyleSet(0);
130 ASSERT_NE(styleSet, nullptr);
131 ASSERT_EQ(styleSet->count(), 2);
132 SkString styleName;
133 SkFontStyle style;
134 styleSet->getStyle(0, &style, &styleName);
135 EXPECT_STREQ(styleName.c_str(), "normal");
136 EXPECT_EQ(style.weight(), SkFontStyle::kNormal_Weight);
137 EXPECT_EQ(style.width(), SkFontStyle::kNormal_Width);
138 EXPECT_EQ(style.slant(), SkFontStyle::kUpright_Slant);
139
140 styleSet->getStyle(1, &style, &styleName);
141 EXPECT_STREQ(styleName.c_str(), "normal");
142 EXPECT_EQ(style.weight(), SkFontStyle::kNormal_Weight);
143 EXPECT_EQ(style.width(), SkFontStyle::kNormal_Width);
144 EXPECT_EQ(style.slant(), SkFontStyle::kItalic_Slant);
145 #ifndef USE_M133_SKIA
146 delete styleSet;
147 #endif
148 }
149
150 /**
151 * @tc.name: MatchFamily001
152 * @tc.desc: Test MatchFamily
153 * @tc.type: FUNC
154 */
155 HWTEST_F(SkFontMgrOhosTest, MatchFamily001, TestSize.Level1)
156 {
157 std::string testRes1 = BASE_NAME + " Sans";
158 #ifdef USE_M133_SKIA
159 sk_sp<SkFontStyleSet> styleSet1 = m_fontMgrOhosImpl->matchFamily(testRes1.c_str());
160 #else
161 SkFontStyleSet* styleSet1 = m_fontMgrOhosImpl->matchFamily(testRes1.c_str());
162 #endif
163 ASSERT_NE(styleSet1, nullptr);
164 ASSERT_EQ(styleSet1->count(), 0);
165
166 std::string testRes2 = BASE_NAME + "-Sans";
167 #ifdef USE_M133_SKIA
168 sk_sp<SkFontStyleSet> styleSet2 = m_fontMgrOhosImpl->matchFamily(testRes2.c_str());
169 #else
170 SkFontStyleSet* styleSet2 = m_fontMgrOhosImpl->matchFamily(testRes2.c_str());
171 #endif
172 ASSERT_NE(styleSet2, nullptr);
173 ASSERT_EQ(styleSet2->count(), 2);
174
175 std::string testRes3 = BASE_NAME + " Sans SC";
176 #ifdef USE_M133_SKIA
177 sk_sp<SkFontStyleSet> styleSet3 = m_fontMgrOhosImpl->matchFamily(testRes3.c_str());
178 #else
179 SkFontStyleSet* styleSet3 = m_fontMgrOhosImpl->matchFamily(testRes3.c_str());
180 #endif
181 ASSERT_NE(styleSet3, nullptr);
182 ASSERT_EQ(styleSet3->count(), 1);
183 #ifndef USE_M133_SKIA
184 delete styleSet1;
185 delete styleSet2;
186 delete styleSet3;
187 #endif
188 }
189
190 /**
191 * @tc.name: MatchFamilyStyle001
192 * @tc.desc: Test MatchFamilyStyle
193 * @tc.type: FUNC
194 */
195 #ifdef USE_M133_SKIA
196 HWTEST_F(SkFontMgrOhosTest, MatchFamilyStyle001, TestSize.Level1)
197 {
198 SkFontStyle style;
199 sk_sp<SkTypeface> tp = m_fontMgrOhosImpl->matchFamilyStyle(nullptr, style);
200 ASSERT_NE(tp, nullptr);
201 SkString familyName;
202 tp->getFamilyName(&familyName);
203 std::string testRes = BASE_NAME + "-Sans";
204 ASSERT_STREQ(familyName.c_str(), testRes.c_str());
205
206 std::string inputName1 = BASE_NAME + "-Sans";
207 sk_sp<SkTypeface> tp1 = m_fontMgrOhosImpl->matchFamilyStyle(inputName1.c_str(), style);
208 ASSERT_NE(tp1, nullptr);
209 SkString familyName1;
210 tp1->getFamilyName(&familyName1);
211 std::string testRes1 = BASE_NAME + "-Sans";
212 ASSERT_STREQ(familyName1.c_str(), testRes1.c_str());
213
214 sk_sp<SkTypeface> tp2 = m_fontMgrOhosImpl->matchFamilyStyle("TestNoFound", style);
215 ASSERT_EQ(tp2, nullptr);
216
217 std::string inputName3 = BASE_NAME + " Sans SC";
218 sk_sp<SkTypeface> tp3 = m_fontMgrOhosImpl->matchFamilyStyle(inputName3.c_str(), style);
219 ASSERT_NE(tp3, nullptr);
220 SkString familyName3;
221 tp3->getFamilyName(&familyName3);
222 std::string testRes3 = BASE_NAME + " Sans SC";
223 ASSERT_STREQ(familyName3.c_str(), testRes3.c_str());
224 }
225 #else
226 HWTEST_F(SkFontMgrOhosTest, MatchFamilyStyle001, TestSize.Level1)
227 {
228 SkFontStyle style;
229 SkTypeface* tp = m_fontMgrOhosImpl->matchFamilyStyle(nullptr, style);
230 ASSERT_NE(tp, nullptr);
231 SkString familyName;
232 tp->getFamilyName(&familyName);
233 std::string testRes = BASE_NAME + "-Sans";
234 ASSERT_STREQ(familyName.c_str(), testRes.c_str());
235 SkSafeUnref(tp);
236
237 std::string inputName1 = BASE_NAME + "-Sans";
238 SkTypeface* tp1 = m_fontMgrOhosImpl->matchFamilyStyle(inputName1.c_str(), style);
239 ASSERT_NE(tp1, nullptr);
240 SkString familyName1;
241 tp1->getFamilyName(&familyName1);
242 std::string testRes1 = BASE_NAME + "-Sans";
243 ASSERT_STREQ(familyName1.c_str(), testRes1.c_str());
244 SkSafeUnref(tp1);
245
246 SkTypeface* tp2 = m_fontMgrOhosImpl->matchFamilyStyle("TestNoFound", style);
247 ASSERT_EQ(tp2, nullptr);
248
249 std::string inputName3 = BASE_NAME + " Sans SC";
250 SkTypeface* tp3 = m_fontMgrOhosImpl->matchFamilyStyle(inputName3.c_str(), style);
251 ASSERT_NE(tp3, nullptr);
252 SkString familyName3;
253 tp3->getFamilyName(&familyName3);
254 std::string testRes3 = BASE_NAME + " Sans SC";
255 ASSERT_STREQ(familyName3.c_str(), testRes3.c_str());
256 SkSafeUnref(tp3);
257 }
258 #endif
259
260 /**
261 * @tc.name: MatchFamilyStyleCharacter001
262 * @tc.desc: Test MatchFamilyStyleCharacter
263 * @tc.type: FUNC
264 */
265 #ifdef USE_M133_SKIA
266 HWTEST_F(SkFontMgrOhosTest, MatchFamilyStyleCharacter001, TestSize.Level1)
267 {
268 SkFontStyle style;
269 sk_sp<SkTypeface> tp = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, nullptr, 0, 0x0626);
270 ASSERT_NE(tp, nullptr);
271 SkString familyName;
272 tp->getFamilyName(&familyName);
273 std::string testRes1 = BASE_NAME + " Sans Naskh Arabic UI";
274 ASSERT_STREQ(familyName.c_str(), testRes1.c_str());
275
276 std::vector<const char*> bcp47;
277 sk_sp<SkTypeface> tp1 = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, bcp47.data(),
278 bcp47.size(), 0x63CF);
279 ASSERT_NE(tp1, nullptr);
280 tp1->getFamilyName(&familyName);
281 std::string testRes2 = BASE_NAME + " Sans SC";
282 ASSERT_STREQ(familyName.c_str(), testRes2.c_str());
283
284 bcp47.push_back("zh-Hant");
285 sk_sp<SkTypeface> tp2 = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, bcp47.data(),
286 bcp47.size(), 0x63CF);
287 ASSERT_NE(tp2, nullptr);
288 tp2->getFamilyName(&familyName);
289 std::string testRes3 = BASE_NAME + " Sans TC";
290 ASSERT_STREQ(familyName.c_str(), testRes3.c_str());
291
292 sk_sp<SkTypeface> tp3 = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, bcp47.data(),
293 bcp47.size(), 0x1F600);
294 ASSERT_NE(tp3, nullptr);
295 tp3->getFamilyName(&familyName);
296 ASSERT_STREQ(familyName.c_str(), "HMOS Color Emoji");
297 }
298 #else
299 HWTEST_F(SkFontMgrOhosTest, MatchFamilyStyleCharacter001, TestSize.Level1)
300 {
301 SkFontStyle style;
302 SkTypeface* tp = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, nullptr, 0, 0x0626);
303 ASSERT_NE(tp, nullptr);
304 SkString familyName;
305 tp->getFamilyName(&familyName);
306 std::string testRes1 = BASE_NAME + " Sans Naskh Arabic UI";
307 ASSERT_STREQ(familyName.c_str(), testRes1.c_str());
308
309 std::vector<const char*> bcp47;
310 SkTypeface* tp1 = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, bcp47.data(), bcp47.size(), 0x63CF);
311 ASSERT_NE(tp1, nullptr);
312 tp1->getFamilyName(&familyName);
313 std::string testRes2 = BASE_NAME + " Sans SC";
314 ASSERT_STREQ(familyName.c_str(), testRes2.c_str());
315
316 bcp47.push_back("zh-Hant");
317 SkTypeface* tp2 = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, bcp47.data(), bcp47.size(), 0x63CF);
318 ASSERT_NE(tp2, nullptr);
319 tp2->getFamilyName(&familyName);
320 std::string testRes3 = BASE_NAME + " Sans TC";
321 ASSERT_STREQ(familyName.c_str(), testRes3.c_str());
322
323 SkTypeface* tp3 = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, bcp47.data(), bcp47.size(), 0x1F600);
324 ASSERT_NE(tp3, nullptr);
325 tp3->getFamilyName(&familyName);
326 ASSERT_STREQ(familyName.c_str(), "HMOS Color Emoji");
327
328 SkSafeUnref(tp);
329 SkSafeUnref(tp1);
330 SkSafeUnref(tp2);
331 SkSafeUnref(tp3);
332 }
333 #endif
334
335 /**
336 * @tc.name: MakeFromStreamIndex001
337 * @tc.desc: Test MakeFromStreamIndex
338 * @tc.type: FUNC
339 */
340 HWTEST_F(SkFontMgrOhosTest, MakeFromStreamIndex001, TestSize.Level1)
341 {
342 std::unique_ptr<SkStreamAsset> skStream = SkStreamAsset::MakeFromFile(TTF_FILE_PATH);
343 sk_sp<SkTypeface> sktp = m_fontMgrOhosImpl->makeFromStream(std::move(skStream), 0);
344 SkString familyName;
345 sktp->getFamilyName(&familyName);
346 std::string testRes = BASE_NAME + " Sans SC";
347 ASSERT_STREQ(familyName.c_str(), testRes.c_str());
348 }
349
350 /**
351 * @tc.name: MakeFromStreamArgs001
352 * @tc.desc: Test MakeFromStreamArgs
353 * @tc.type: FUNC
354 */
355 HWTEST_F(SkFontMgrOhosTest, MakeFromStreamArgs001, TestSize.Level1)
356 {
357 std::unique_ptr<SkStreamAsset> skStream = SkStreamAsset::MakeFromFile(TTF_FILE_PATH);
358 SkFontArguments skFontArguments;
359 sk_sp<SkTypeface> sktp = m_fontMgrOhosImpl->makeFromStream(std::move(skStream), skFontArguments);
360 SkString familyName;
361 sktp->getFamilyName(&familyName);
362 std::string testRes = BASE_NAME + " Sans SC";
363 ASSERT_STREQ(familyName.c_str(), testRes.c_str());
364 }
365
366 /**
367 * @tc.name: MakeFromFile001
368 * @tc.desc: Test MakeFromFile
369 * @tc.type: FUNC
370 */
371 HWTEST_F(SkFontMgrOhosTest, MakeFromFile001, TestSize.Level1)
372 {
373 sk_sp<SkTypeface> sktp = m_fontMgrOhosImpl->makeFromFile(TTF_FILE_PATH, 0);
374 ASSERT_NE(sktp, nullptr);
375 SkString familyName;
376 sktp->getFamilyName(&familyName);
377 std::string testRes = BASE_NAME + " Sans SC";
378 ASSERT_STREQ(familyName.c_str(), testRes.c_str());
379 }
380
381 /**
382 * @tc.name: LegacyMakeTypeface001
383 * @tc.desc: Test LegacyMakeTypeface
384 * @tc.type: FUNC
385 */
386 HWTEST_F(SkFontMgrOhosTest, LegacyMakeTypeface001, TestSize.Level1)
387 {
388 SkFontStyle style;
389 sk_sp<SkTypeface> sktp = m_fontMgrOhosImpl->legacyMakeTypeface("TestNoFound", style);
390 ASSERT_NE(sktp, nullptr);
391 SkString familyName;
392 sktp->getFamilyName(&familyName);
393 std::string testRes1 = BASE_NAME + "-Sans";
394 ASSERT_STREQ(familyName.c_str(), testRes1.c_str());
395
396 std::string testRes2 = BASE_NAME + "-Sans-Condensed";
397 sk_sp<SkTypeface> sktp1 = m_fontMgrOhosImpl->legacyMakeTypeface(testRes2.c_str(), style);
398 ASSERT_NE(sktp1, nullptr);
399 sktp1->getFamilyName(&familyName);
400 std::string testRes3 = BASE_NAME + "-Sans-Condensed";
401 ASSERT_STREQ(familyName.c_str(), testRes3.c_str());
402 }