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 <codecvt>
17 #include <filesystem>
18 #include <fstream>
19
20 #include "drawing_font_collection.h"
21 #include "drawing_register_font.h"
22 #include "drawing_text_font_descriptor.h"
23 #include "font_descriptor_mgr.h"
24 #include "gtest/gtest.h"
25 #include "unicode/unistr.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace {
32 namespace fs = std::filesystem;
33
34 const std::string STYLISH_FONT_CONFIG_FILE = "/system/fonts/visibility_list.json";
35 const std::string STYLISH_FONT_CONFIG_PROD_FILE = "/sys_prod/fonts/visibility_list.json";
36 const std::string INSTALLED_FONT_CONFIG_FILE = "/data/service/el1/public/for-all-app/fonts/install_fontconfig.json";
37 const std::string INSTALLED_FONT_CONFIG_FILE_BAK =
38 "/data/service/el1/public/for-all-app/fonts/install_fontconfig.json.bak";
39
40 // "Noto Sans Mono CJK KR" exchange with "Noto Sans Mono CJK JP", make "Noto Sans Mono CJK HK" invalid index
41 const std::string INSTALL_CONFIG = R"(
42 {
43 "fontlist": [
44 {
45 "fontfullpath": "/system/fonts/NotoSansCJK-Regular.ttc",
46 "fullname": [
47 "Noto Sans CJK JP",
48 "Noto Sans CJK KR",
49 "Noto Sans CJK SC",
50 "Noto Sans CJK TC",
51 "Noto Sans CJK HK",
52 "Noto Sans Mono CJK KR",
53 "Noto Sans Mono CJK JP",
54 "Noto Sans Mono CJK SC",
55 "Noto Sans Mono CJK TC",
56 "Unknown",
57 "Noto Sans Mono CJK HK"
58 ]
59 },
60 { "fontfullpath": "/system/fonts/NotoSans[wdth,wght].ttf", "fullname": ["Noto Sans Regular"] }
61 ]
62 }
63 )";
64
ExistStylishFontConfigFile()65 bool ExistStylishFontConfigFile()
66 {
67 return fs::exists(STYLISH_FONT_CONFIG_FILE) || fs::exists(STYLISH_FONT_CONFIG_PROD_FILE);
68 }
69 } // namespace
70
71 class NdkFontDescriptorTest : public testing::Test {};
72
CreateFile(const std::string & file)73 void CreateFile(const std::string& file)
74 {
75 fs::path filePath(file);
76 fs::path dirPath = filePath.parent_path();
77 if (!fs::exists(dirPath)) {
78 fs::create_directories(dirPath);
79 }
80 std::ofstream ofs(file, std::ios::trunc);
81 ofs << INSTALL_CONFIG;
82 }
83
InitInstallConfig()84 void InitInstallConfig()
85 {
86 if (fs::exists(INSTALLED_FONT_CONFIG_FILE)) {
87 fs::rename(INSTALLED_FONT_CONFIG_FILE, INSTALLED_FONT_CONFIG_FILE_BAK);
88 }
89 CreateFile(INSTALLED_FONT_CONFIG_FILE);
90 }
91
DestroyInstallConfig()92 void DestroyInstallConfig()
93 {
94 if (fs::exists(INSTALLED_FONT_CONFIG_FILE_BAK)) {
95 fs::copy_file(INSTALLED_FONT_CONFIG_FILE_BAK, INSTALLED_FONT_CONFIG_FILE, fs::copy_options::overwrite_existing);
96 fs::remove(INSTALLED_FONT_CONFIG_FILE_BAK);
97 } else {
98 fs::remove(INSTALLED_FONT_CONFIG_FILE);
99 }
100 }
101
102 class InstallConfig {
103 public:
InstallConfig()104 InstallConfig()
105 {
106 InitInstallConfig();
107 }
108
~InstallConfig()109 ~InstallConfig()
110 {
111 DestroyInstallConfig();
112 }
113 };
114
115 /*
116 * @tc.name: NdkFontDescriptorTest001
117 * @tc.desc: test for the fontDescriptor.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest001, TestSize.Level0)
121 {
122 OH_Drawing_FontDescriptor* descArr = OH_Drawing_MatchFontDescriptors(nullptr, nullptr);
123 EXPECT_EQ(descArr, nullptr);
124 OH_Drawing_DestroyFontDescriptors(descArr, 0);
125 }
126
127 /*
128 * @tc.name: NdkFontDescriptorTest002
129 * @tc.desc: test for the fontDescriptor.
130 * @tc.type: FUNC
131 */
132 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest002, TestSize.Level0)
133 {
134 OH_Drawing_FontDescriptor* desc = OH_Drawing_CreateFontDescriptor();
135 size_t num = 0;
136 OH_Drawing_FontDescriptor* descArr = OH_Drawing_MatchFontDescriptors(desc, &num);
137 OH_Drawing_DestroyFontDescriptor(desc);
138 EXPECT_NE(descArr, nullptr);
139 if (ExistStylishFontConfigFile()) {
140 EXPECT_EQ(num, 143);
141 } else {
142 EXPECT_EQ(num, 142);
143 }
144 OH_Drawing_DestroyFontDescriptors(descArr, num);
145 }
146
147 /*
148 * @tc.name: NdkFontDescriptorTest003
149 * @tc.desc: test for the fontDescriptor.
150 * @tc.type: FUNC
151 */
152 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest003, TestSize.Level0)
153 {
154 OH_Drawing_FontDescriptor* desc = OH_Drawing_CreateFontDescriptor();
155 desc->weight = -1;
156 size_t num = 0;
157 OH_Drawing_FontDescriptor* descArr = OH_Drawing_MatchFontDescriptors(desc, &num);
158 OH_Drawing_DestroyFontDescriptor(desc);
159 EXPECT_EQ(descArr, nullptr);
160 EXPECT_EQ(num, 0);
161 OH_Drawing_DestroyFontDescriptors(descArr, num);
162 }
163
164 /*
165 * @tc.name: NdkFontDescriptorTest004
166 * @tc.desc: test for the fontDescriptor.
167 * @tc.type: FUNC
168 */
169 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest004, TestSize.Level0)
170 {
171 OH_Drawing_FontDescriptor* desc = OH_Drawing_CreateFontDescriptor();
172 char* fontFamily = strdup("HarmonyOS Sans");
173 desc->fontFamily = fontFamily;
174 size_t num = 0;
175 OH_Drawing_FontDescriptor* descArr = OH_Drawing_MatchFontDescriptors(desc, &num);
176 ASSERT_NE(descArr, nullptr);
177 EXPECT_GE(num, 1);
178 EXPECT_STREQ(descArr[0].fontFamily, fontFamily);
179 OH_Drawing_DestroyFontDescriptors(descArr, num);
180 free(fontFamily);
181
182 fontFamily = strdup("HarmonyOS Sans Condensed");
183 desc->fontFamily = fontFamily;
184 descArr = OH_Drawing_MatchFontDescriptors(desc, &num);
185 ASSERT_NE(descArr, nullptr);
186 EXPECT_EQ(num, 1);
187 EXPECT_STREQ(descArr[0].fontFamily, fontFamily);
188 OH_Drawing_DestroyFontDescriptors(descArr, num);
189 OH_Drawing_DestroyFontDescriptor(desc);
190 }
191
192 /*
193 * @tc.name: NdkFontDescriptorTest005
194 * @tc.desc: test for the fontDescriptor.
195 * @tc.type: FUNC
196 */
197 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest005, TestSize.Level0)
198 {
199 OH_Drawing_FontDescriptor* desc = OH_Drawing_CreateFontDescriptor();
200 char* fontFamily = strdup("HarmonyOS Sans");
201 desc->fontFamily = fontFamily;
202 desc->weight = 400;
203
204 size_t num = 0;
205 OH_Drawing_FontDescriptor* descArr = OH_Drawing_MatchFontDescriptors(desc, &num);
206 ASSERT_NE(descArr, nullptr);
207 EXPECT_GE(num, 1);
208 EXPECT_STREQ(descArr[0].fontFamily, fontFamily);
209 EXPECT_EQ(descArr[0].weight, 400);
210 OH_Drawing_DestroyFontDescriptors(descArr, num);
211 OH_Drawing_DestroyFontDescriptor(desc);
212 }
213
214 /*
215 * @tc.name: NdkFontDescriptorTest006
216 * @tc.desc: test for abnormal parameters when obtaining the font list and obtaining the FontDescriptor by fullName.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest006, TestSize.Level0)
220 {
221 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(0b10000);
222 OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
223 EXPECT_EQ(fontList, nullptr);
224
225 OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(nullptr, fontType);
226 EXPECT_EQ(descriptor, nullptr);
227
228 // The array ttfFullName represents the UTF-16 encoded version of a non-existent font full name "你好openharmony".
229 const uint8_t ttfFullName[] = { 0x4F, 0x60, 0x59, 0x7D, 0x00, 0x6F, 0x00, 0x70, 0x00, 0x65, 0x00, 0x6E, 0x00, 0x68,
230 0x00, 0x61, 0x00, 0x72, 0x00, 0x6D, 0x00, 0x6F, 0x00, 0x6E, 0x00, 0x79 };
231 OH_Drawing_String drawingString;
232 drawingString.strData = const_cast<uint8_t*>(ttfFullName);
233 drawingString.strLen = sizeof(ttfFullName);
234 OH_Drawing_FontDescriptor *descriptor1 =
235 OH_Drawing_GetFontDescriptorByFullName(&drawingString, OH_Drawing_SystemFontType::ALL);
236 EXPECT_EQ(descriptor1, nullptr);
237 }
238
239 /*
240 * @tc.name: NdkFontDescriptorTest007
241 * @tc.desc: test for obtaining the array of installed fonts.
242 * @tc.type: FUNC
243 */
244 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest007, TestSize.Level0)
245 {
246 InstallConfig installConfig;
247 std::unordered_set<std::string> fullnames { "Noto Sans CJK JP", "Noto Sans CJK KR", "Noto Sans CJK SC",
248 "Noto Sans CJK TC", "Noto Sans CJK HK", "Noto Sans Mono CJK JP", "Noto Sans Mono CJK KR",
249 "Noto Sans Mono CJK SC", "Noto Sans Mono CJK TC", "Noto Sans Mono CJK HK", "Noto Sans Regular" };
250 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::INSTALLED;
251 OH_Drawing_Array* fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
252 EXPECT_NE(fontList, nullptr);
253 size_t size = OH_Drawing_GetDrawingArraySize(fontList);
254 EXPECT_EQ(size, 12);
255 for (size_t i = 0; i < size; i++) {
256 const OH_Drawing_String* fullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
257 ASSERT_NE(fullName, nullptr);
258 OH_Drawing_FontDescriptor* fd = OH_Drawing_GetFontDescriptorByFullName(fullName, INSTALLED);
259 if (fd != nullptr) {
260 EXPECT_TRUE(fullnames.count(fd->fullName));
261 OH_Drawing_DestroyFontDescriptor(fd);
262 } else {
263 std::string s;
264 icu::UnicodeString ustr(reinterpret_cast<UChar*>(fullName->strData), fullName->strLen / sizeof(char16_t));
265 ustr.toUTF8String(s);
266 EXPECT_EQ(s, "Unknown");
267 }
268 }
269 std::u16string fullName = u"not exist";
270 OH_Drawing_String fn { reinterpret_cast<uint8_t*>(fullName.data()), fullName.size() * sizeof(char16_t) };
271 OH_Drawing_FontDescriptor* fd = OH_Drawing_GetFontDescriptorByFullName(&fn, INSTALLED);
272 EXPECT_EQ(fd, nullptr);
273 OH_Drawing_DestroySystemFontFullNames(fontList);
274 }
275
276 /*
277 * @tc.name: NdkFontDescriptorTest008
278 * @tc.desc: test for obtaining the array of stylish fonts.
279 * @tc.type: FUNC
280 */
281 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest008, TestSize.Level0)
282 {
283 if (!ExistStylishFontConfigFile()) {
284 return;
285 }
286 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::STYLISH;
287 OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
288 ASSERT_NE(fontList, nullptr);
289 size_t size = OH_Drawing_GetDrawingArraySize(fontList);
290 EXPECT_EQ(size, 1);
291 for (size_t i = 0; i < size; i++) {
292 const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
293 EXPECT_NE(fontFullName, nullptr);
294 OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
295 ASSERT_NE(descriptor, nullptr);
296 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
297 char16_t* begin = reinterpret_cast<char16_t*>(fontFullName->strData);
298 char16_t* end = reinterpret_cast<char16_t*>(fontFullName->strData + fontFullName->strLen);
299 EXPECT_EQ(convert.to_bytes(begin, end), descriptor->fullName);
300 OH_Drawing_DestroyFontDescriptor(descriptor);
301 }
302 OH_Drawing_DestroySystemFontFullNames(fontList);
303 }
304
305 /*
306 * @tc.name: NdkFontDescriptorTest009
307 * @tc.desc: test for obtaining the array of system generic fonts.
308 * @tc.type: FUNC
309 */
310 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest009, TestSize.Level0)
311 {
312 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::GENERIC;
313 OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
314 ASSERT_NE(fontList, nullptr);
315 size_t size = OH_Drawing_GetDrawingArraySize(fontList);
316 if (ExistStylishFontConfigFile()) {
317 EXPECT_EQ(size, 142);
318 } else {
319 EXPECT_EQ(size, 141);
320 }
321 for (size_t i = 0; i < size; i++) {
322 const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
323 EXPECT_NE(fontFullName, nullptr);
324 OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
325 ASSERT_NE(descriptor, nullptr);
326 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
327 char16_t* begin = reinterpret_cast<char16_t*>(fontFullName->strData);
328 char16_t* end = reinterpret_cast<char16_t*>(fontFullName->strData + fontFullName->strLen);
329 EXPECT_EQ(convert.to_bytes(begin, end), descriptor->fullName);
330 OH_Drawing_DestroyFontDescriptor(descriptor);
331 }
332 OH_Drawing_DestroySystemFontFullNames(fontList);
333 }
334
335 /*
336 * @tc.name: NdkFontDescriptorTest010
337 * @tc.desc: test for obtaining the list of composite fonts.
338 * @tc.type: FUNC
339 */
340 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest010, TestSize.Level0)
341 {
342 if (!ExistStylishFontConfigFile()) {
343 return;
344 }
345 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(STYLISH);
346 OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
347 ASSERT_NE(fontList, nullptr);
348 size_t size = OH_Drawing_GetDrawingArraySize(fontList);
349 EXPECT_EQ(size, 1);
350 for (size_t i = 0; i < size; i++) {
351 const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
352 EXPECT_NE(fontFullName, nullptr);
353 OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
354 ASSERT_NE(descriptor, nullptr);
355 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
356 char16_t* begin = reinterpret_cast<char16_t*>(fontFullName->strData);
357 char16_t* end = reinterpret_cast<char16_t*>(fontFullName->strData + fontFullName->strLen);
358 EXPECT_EQ(convert.to_bytes(begin, end), descriptor->fullName);
359 OH_Drawing_DestroyFontDescriptor(descriptor);
360 }
361 OH_Drawing_DestroySystemFontFullNames(fontList);
362 }
363
364 /*
365 * @tc.name: NdkFontDescriptorTest011
366 * @tc.desc: test for obtaining the list of composite fonts that include "ALL".
367 * @tc.type: FUNC
368 */
369 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest011, TestSize.Level0)
370 {
371 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(ALL | STYLISH);
372 OH_Drawing_Array* fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
373 ASSERT_NE(fontList, nullptr);
374 size_t size = OH_Drawing_GetDrawingArraySize(fontList);
375 if (ExistStylishFontConfigFile()) {
376 EXPECT_EQ(size, 143);
377 } else {
378 EXPECT_EQ(size, 141);
379 }
380 for (size_t i = 0; i < size; i++) {
381 const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
382 EXPECT_NE(fontFullName, nullptr);
383 OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
384 ASSERT_NE(descriptor, nullptr);
385 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
386 char16_t* begin = reinterpret_cast<char16_t*>(fontFullName->strData);
387 char16_t* end = reinterpret_cast<char16_t*>(fontFullName->strData + fontFullName->strLen);
388 EXPECT_EQ(convert.to_bytes(begin, end), descriptor->fullName);
389 OH_Drawing_DestroyFontDescriptor(descriptor);
390 }
391 OH_Drawing_DestroySystemFontFullNames(fontList);
392 }
393
394 /*
395 * @tc.name: NdkFontDescriptorTest012
396 * @tc.desc: test for abnormal parameters when obtaining the fullName by index and releasing array memory.
397 * @tc.type: FUNC
398 */
399 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest012, TestSize.Level0)
400 {
401 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::GENERIC;
402 OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
403 ASSERT_NE(fontList, nullptr);
404 const OH_Drawing_String* fullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, 500);
405 EXPECT_EQ(fullName, nullptr);
406 OH_Drawing_DestroySystemFontFullNames(fontList);
407
408 const OH_Drawing_String* fullName1 = OH_Drawing_GetSystemFontFullNameByIndex(nullptr, 0);
409 EXPECT_EQ(fullName1, nullptr);
410
411 const OH_Drawing_String* fullName2 = OH_Drawing_GetSystemFontFullNameByIndex(nullptr, 500);
412 EXPECT_EQ(fullName2, nullptr);
413
414 OH_Drawing_DestroySystemFontFullNames(nullptr);
415 }
416
417 /*
418 * @tc.name: NdkFontDescriptorTest013
419 * @tc.desc: test for obtaining the list of composite fonts that include "ALL".
420 * @tc.type: FUNC
421 */
422 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest013, TestSize.Level0)
423 {
424 OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
425 const char* fontFamily = "HM Symbol Regular";
426 const char* fontPath = "/system/fonts/HMSymbolVF.ttf";
427 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
428
429 OH_Drawing_Array *ttfs = OH_Drawing_GetSystemFontFullNamesByType(ALL);
430 size_t num = OH_Drawing_GetDrawingArraySize(ttfs);
431 if (ExistStylishFontConfigFile()) {
432 EXPECT_EQ(num, 143);
433 } else {
434 EXPECT_EQ(num, 141);
435 }
436 FontDescriptorMgrInstance.ClearFontFileCache();
437 OH_Drawing_DestroyFontCollection(fc);
438 }
439
440 /*
441 * @tc.name: NdkFontDescriptorTest014
442 * @tc.desc: test for registering a font once and query it.
443 * @tc.type: FUNC
444 */
445 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest014, TestSize.Level0)
446 {
447 OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
448 const char* fontFamily = "HM Symbol Regular";
449 const char* fontPath = "/system/fonts/HMSymbolVF.ttf";
450 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
451
452 OH_Drawing_Array *ttfs = OH_Drawing_GetSystemFontFullNamesByType(CUSTOMIZED);
453 size_t num = OH_Drawing_GetDrawingArraySize(ttfs);
454 EXPECT_EQ(num, 1);
455 for (size_t i = 0; i < num; i++) {
456 const OH_Drawing_String *fullName = OH_Drawing_GetSystemFontFullNameByIndex(ttfs, i);
457 OH_Drawing_FontDescriptor *fd = OH_Drawing_GetFontDescriptorByFullName(fullName, CUSTOMIZED);
458 ASSERT_STREQ(fd->fullName, "HM Symbol Regular");
459 }
460 FontDescriptorMgrInstance.ClearFontFileCache();
461 OH_Drawing_DestroyFontCollection(fc);
462 }
463
464 /*
465 * @tc.name: NdkFontDescriptorTest015
466 * @tc.desc: test for registering a font five times and query it.
467 * @tc.type: FUNC
468 */
469 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest015, TestSize.Level0)
470 {
471 OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
472 const char* fontFamily = "HM Symbol Regular";
473 const char* fontPath = "/system/fonts/HMSymbolVF.ttf";
474 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
475 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
476 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
477 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
478 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
479
480 OH_Drawing_Array *ttfs = OH_Drawing_GetSystemFontFullNamesByType(CUSTOMIZED);
481 size_t num = OH_Drawing_GetDrawingArraySize(ttfs);
482 EXPECT_EQ(num, 1);
483 for (size_t i = 0; i < num; i++) {
484 const OH_Drawing_String *fullName = OH_Drawing_GetSystemFontFullNameByIndex(ttfs, i);
485 OH_Drawing_FontDescriptor *fd = OH_Drawing_GetFontDescriptorByFullName(fullName, CUSTOMIZED);
486 ASSERT_STREQ(fd->fullName, "HM Symbol Regular");
487 }
488 FontDescriptorMgrInstance.ClearFontFileCache();
489 OH_Drawing_DestroyFontCollection(fc);
490 }
491
492 /*
493 * @tc.name: NdkFontDescriptorTest016
494 * @tc.desc: test for registering a TTC font and query it.
495 * @tc.type: FUNC
496 */
497 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest016, TestSize.Level0)
498 {
499 OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
500 const char* fontFamily = "NotoSansCJKjp-Regular-Alphabetic";
501 const char* fontPath = "/system/fonts/NotoSansCJK-Regular.ttc";
502 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
503
504 OH_Drawing_Array *ttfs = OH_Drawing_GetSystemFontFullNamesByType(CUSTOMIZED);
505 size_t num = OH_Drawing_GetDrawingArraySize(ttfs);
506 EXPECT_EQ(num, 1);
507 for (size_t i = 0; i < num; i++) {
508 const OH_Drawing_String *fullName = OH_Drawing_GetSystemFontFullNameByIndex(ttfs, i);
509 OH_Drawing_FontDescriptor *fd = OH_Drawing_GetFontDescriptorByFullName(fullName, CUSTOMIZED);
510 ASSERT_STREQ(fd->fullName, "Noto Sans CJK JP");
511 }
512 FontDescriptorMgrInstance.ClearFontFileCache();
513 OH_Drawing_DestroyFontCollection(fc);
514 }
515
516 /*
517 * @tc.name: NdkFontDescriptorTest017
518 * @tc.desc: test for registering a OTF font and query it.
519 * @tc.type: FUNC
520 */
521 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest017, TestSize.Level0)
522 {
523 OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
524 const char* fontFamily = "Birch std";
525 const char* fontPath = "/system/fonts/Birchstd.otf";
526 if (!fs::exists(fontPath)) {
527 return;
528 }
529 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
530
531 OH_Drawing_Array *ttfs = OH_Drawing_GetSystemFontFullNamesByType(CUSTOMIZED);
532 size_t num = OH_Drawing_GetDrawingArraySize(ttfs);
533 EXPECT_EQ(num, 1);
534 for (size_t i = 0; i < num; i++) {
535 const OH_Drawing_String *fullName = OH_Drawing_GetSystemFontFullNameByIndex(ttfs, i);
536 OH_Drawing_FontDescriptor *fd = OH_Drawing_GetFontDescriptorByFullName(fullName, CUSTOMIZED);
537 ASSERT_STREQ(fd->fullName, "Birch std");
538 }
539 FontDescriptorMgrInstance.ClearFontFileCache();
540 OH_Drawing_DestroyFontCollection(fc);
541 }
542
543 /*
544 * @tc.name: NdkFontDescriptorTest018
545 * @tc.desc: test for registering failed.
546 * @tc.type: FUNC
547 */
548 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest018, TestSize.Level0)
549 {
550 OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
551 const char* fontFamily = "xxxxxxx";
552 const char* fontPath = "/system/fonts/xxxxxxx.ttf";
553 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
554
555 OH_Drawing_Array *ttfs = OH_Drawing_GetSystemFontFullNamesByType(CUSTOMIZED);
556 size_t num = OH_Drawing_GetDrawingArraySize(ttfs);
557 EXPECT_EQ(num, 0);
558 FontDescriptorMgrInstance.ClearFontFileCache();
559 OH_Drawing_DestroyFontCollection(fc);
560 }
561
562 /*
563 * @tc.name: NdkFontDescriptorTest019
564 * @tc.desc: test for registering a font with a local fontCollection.
565 * @tc.type: FUNC
566 */
567 HWTEST_F(NdkFontDescriptorTest, NdkFontDescriptorTest019, TestSize.Level0)
568 {
569 OH_Drawing_FontCollection *fc = OH_Drawing_CreateFontCollection();
570 const char* fontFamily = "HM Symbol Regular";
571 const char* fontPath = "/system/fonts/HMSymbolVF.ttf";
572 OH_Drawing_RegisterFont(fc, fontFamily, fontPath);
573
574 OH_Drawing_Array *ttfs = OH_Drawing_GetSystemFontFullNamesByType(CUSTOMIZED);
575 size_t num = OH_Drawing_GetDrawingArraySize(ttfs);
576 EXPECT_EQ(num, 1);
577 for (size_t i = 0; i < num; i++) {
578 const OH_Drawing_String *fullName = OH_Drawing_GetSystemFontFullNameByIndex(ttfs, i);
579 OH_Drawing_FontDescriptor *fd = OH_Drawing_GetFontDescriptorByFullName(fullName, CUSTOMIZED);
580 ASSERT_STREQ(fd->fullName, "HM Symbol Regular");
581 }
582
583 OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
584 OH_Drawing_CreateTypographyHandler(typoStyle, fc);
585 OH_Drawing_Array *ttfs1 = OH_Drawing_GetSystemFontFullNamesByType(CUSTOMIZED);
586 size_t num1 = OH_Drawing_GetDrawingArraySize(ttfs1);
587 EXPECT_EQ(num1, 0);
588 FontDescriptorMgrInstance.ClearFontFileCache();
589 OH_Drawing_DestroyFontCollection(fc);
590 }
591 } // namespace OHOS