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 <filesystem>
17 
18 #include "drawing_text_font_descriptor.h"
19 #include "gtest/gtest.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace {
26 namespace fs = std::filesystem;
27 
28 const std::string STYLISH_FONT_CONFIG_FILE = "/system/fonts/visibility_list.json";
29 const std::string STYLISH_FONT_CONFIG_PROD_FILE = "/sys_prod/fonts/visibility_list.json";
30 const std::string INSTALLED_FONT_CONFIG_FILE =
31     "/data/service/el1/public/for-all-app/fonts/install_fontconfig.json";
32 }
33 
34 class OH_Drawing_FontDescriptorTest : public testing::Test {
35 };
36 
37 /*
38  * @tc.name: OH_Drawing_FontDescriptorTest006
39  * @tc.desc: test for abnormal parameters when obtaining the font list and obtaining the FontDescriptor by fullName.
40  * @tc.type: FUNC
41  */
42 HWTEST_F(OH_Drawing_FontDescriptorTest, OH_Drawing_FontDescriptorTest006, TestSize.Level1)
43 {
44     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(0b10000);
45     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
46     EXPECT_EQ(fontList, nullptr);
47 
48     OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(nullptr, fontType);
49     EXPECT_EQ(descriptor, nullptr);
50 
51     // The array TTF_FULLNAME represents the UTF-16 encoded version of a non-existent font full name "你好openharmony".
52     const uint8_t TTF_FULLNAME[] = {
53         0x4F, 0x60,
54         0x59, 0x7D,
55         0x00, 0x6F,
56         0x00, 0x70,
57         0x00, 0x65,
58         0x00, 0x6E,
59         0x00, 0x68,
60         0x00, 0x61,
61         0x00, 0x72,
62         0x00, 0x6D,
63         0x00, 0x6F,
64         0x00, 0x6E,
65         0x00, 0x79
66     };
67     OH_Drawing_String drawingString;
68     drawingString.strData = const_cast<uint8_t*>(TTF_FULLNAME);
69     drawingString.strLen = sizeof(TTF_FULLNAME);
70     OH_Drawing_FontDescriptor *descriptor1 =
71         OH_Drawing_GetFontDescriptorByFullName(&drawingString, OH_Drawing_SystemFontType::ALL);
72     EXPECT_EQ(descriptor1, nullptr);
73 }
74 
75 /*
76  * @tc.name: OH_Drawing_FontDescriptorTest007
77  * @tc.desc: test for obtaining the array of installed fonts.
78  * @tc.type: FUNC
79  */
80 HWTEST_F(OH_Drawing_FontDescriptorTest, OH_Drawing_FontDescriptorTest007, TestSize.Level1)
81 {
82     if (!fs::exists(INSTALLED_FONT_CONFIG_FILE)) {
83         return;
84     }
85     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::INSTALLED;
86     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
87     ASSERT_NE(fontList, nullptr);
88     size_t size = OH_Drawing_GetDrawingArraySize(fontList);
89     EXPECT_NE(size, 0);
90     for (size_t i = 0; i < size; i++) {
91         const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
92         EXPECT_NE(fontFullName, nullptr);
93         OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
94         EXPECT_NE(descriptor, nullptr);
95         OH_Drawing_DestroyFontDescriptor(descriptor);
96     }
97     OH_Drawing_DestroySystemFontFullNames(fontList);
98 }
99 
100 /*
101  * @tc.name: OH_Drawing_FontDescriptorTest008
102  * @tc.desc: test for obtaining the array of stylish fonts.
103  * @tc.type: FUNC
104  */
105 HWTEST_F(OH_Drawing_FontDescriptorTest, OH_Drawing_FontDescriptorTest008, TestSize.Level1)
106 {
107     if (!fs::exists(STYLISH_FONT_CONFIG_FILE) && !fs::exists(STYLISH_FONT_CONFIG_PROD_FILE)) {
108         return;
109     }
110     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::STYLISH;
111     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
112     ASSERT_NE(fontList, nullptr);
113     size_t size = OH_Drawing_GetDrawingArraySize(fontList);
114     EXPECT_NE(size, 0);
115     for (size_t i = 0; i < size; i++) {
116         const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
117         EXPECT_NE(fontFullName, nullptr);
118         OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
119         EXPECT_NE(descriptor, nullptr);
120         OH_Drawing_DestroyFontDescriptor(descriptor);
121     }
122     OH_Drawing_DestroySystemFontFullNames(fontList);
123 }
124 
125 /*
126  * @tc.name: OH_Drawing_FontDescriptorTest009
127  * @tc.desc: test for obtaining the array of system generic fonts.
128  * @tc.type: FUNC
129  */
130 HWTEST_F(OH_Drawing_FontDescriptorTest, OH_Drawing_FontDescriptorTest009, TestSize.Level1)
131 {
132     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::GENERIC;
133     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
134     ASSERT_NE(fontList, nullptr);
135     size_t size = OH_Drawing_GetDrawingArraySize(fontList);
136     EXPECT_NE(size, 0);
137     for (size_t i = 0; i < size; i++) {
138         const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
139         EXPECT_NE(fontFullName, nullptr);
140         OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
141         EXPECT_NE(descriptor, nullptr);
142         OH_Drawing_DestroyFontDescriptor(descriptor);
143     }
144     OH_Drawing_DestroySystemFontFullNames(fontList);
145 }
146 
147 /*
148  * @tc.name: OH_Drawing_FontDescriptorTest010
149  * @tc.desc: test for obtaining the list of composite fonts.
150  * @tc.type: FUNC
151  */
152 HWTEST_F(OH_Drawing_FontDescriptorTest, OH_Drawing_FontDescriptorTest010, TestSize.Level1)
153 {
154     if (!fs::exists(STYLISH_FONT_CONFIG_FILE) && !fs::exists(STYLISH_FONT_CONFIG_PROD_FILE) &&
155         !fs::exists(INSTALLED_FONT_CONFIG_FILE)) {
156         return;
157     }
158     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(INSTALLED | STYLISH);
159     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
160     ASSERT_NE(fontList, nullptr);
161     size_t size = OH_Drawing_GetDrawingArraySize(fontList);
162     EXPECT_NE(size, 0);
163     for (size_t i = 0; i < size; i++) {
164         const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
165         EXPECT_NE(fontFullName, nullptr);
166         OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
167         EXPECT_NE(descriptor, nullptr);
168         OH_Drawing_DestroyFontDescriptor(descriptor);
169     }
170     OH_Drawing_DestroySystemFontFullNames(fontList);
171 }
172 
173 /*
174  * @tc.name: OH_Drawing_FontDescriptorTest011
175  * @tc.desc: test for obtaining the list of composite fonts that include "ALL".
176  * @tc.type: FUNC
177  */
178 HWTEST_F(OH_Drawing_FontDescriptorTest, OH_Drawing_FontDescriptorTest011, TestSize.Level1)
179 {
180     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(ALL | STYLISH);
181     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
182     ASSERT_NE(fontList, nullptr);
183     size_t size = OH_Drawing_GetDrawingArraySize(fontList);
184     EXPECT_NE(size, 0);
185     for (size_t i = 0; i < size; i++) {
186         const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
187         EXPECT_NE(fontFullName, nullptr);
188         OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
189         EXPECT_NE(descriptor, nullptr);
190         OH_Drawing_DestroyFontDescriptor(descriptor);
191     }
192     OH_Drawing_DestroySystemFontFullNames(fontList);
193 }
194 
195 /*
196  * @tc.name: OH_Drawing_FontDescriptorTest012
197  * @tc.desc: test for abnormal parameters when obtaining the fullName by index and releasing array memory.
198  * @tc.type: FUNC
199  */
200 HWTEST_F(OH_Drawing_FontDescriptorTest, OH_Drawing_FontDescriptorTest012, TestSize.Level1)
201 {
202     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::GENERIC;
203     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
204     ASSERT_NE(fontList, nullptr);
205     const OH_Drawing_String* fullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, 500);
206     EXPECT_EQ(fullName, nullptr);
207     OH_Drawing_DestroySystemFontFullNames(fontList);
208 
209     const OH_Drawing_String* fullName1 = OH_Drawing_GetSystemFontFullNameByIndex(nullptr, 0);
210     EXPECT_EQ(fullName1, nullptr);
211 
212     const OH_Drawing_String* fullName2 = OH_Drawing_GetSystemFontFullNameByIndex(nullptr, 500);
213     EXPECT_EQ(fullName2, nullptr);
214 
215     OH_Drawing_DestroySystemFontFullNames(nullptr);
216 }
217 }