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, 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 #include "drawing_font_mgr.h"
18 #include "drawing_text_typography.h"
19 #include "drawing_typeface.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class OH_Drawing_FontMgrTest : public testing::Test {
28 };
29 
30 /*
31  * @tc.name: OH_Drawing_FontMgrTest001
32  * @tc.desc: test for creating and destroying font manager.
33  * @tc.type: FUNC
34  */
35 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest001, TestSize.Level1)
36 {
37     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
38     EXPECT_NE(mgr, nullptr);
39     OH_Drawing_FontMgrDestroy(mgr);
40 }
41 
42 /*
43  * @tc.name: OH_Drawing_FontMgrTest002
44  * @tc.desc: test for getting family name.
45  * @tc.type: FUNC
46  */
47 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest002, TestSize.Level1)
48 {
49     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
50     EXPECT_NE(mgr, nullptr);
51     int count = OH_Drawing_FontMgrGetFamilyCount(mgr);
52     EXPECT_TRUE(count > 0);
53 
54     char *familyName = OH_Drawing_FontMgrGetFamilyName(mgr, 0);
55     OH_Drawing_FontMgrDestroyFamilyName(familyName);
56 
57     OH_Drawing_FontMgrDestroy(mgr);
58 }
59 
60 /*
61  * @tc.name: OH_Drawing_FontMgrTest003
62  * @tc.desc: test for creating and destroying font style set by font mannager.
63  * @tc.type: FUNC
64  */
65 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest003, TestSize.Level1)
66 {
67     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
68     EXPECT_NE(mgr, nullptr);
69 
70     OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0);
71     EXPECT_NE(fontStyleSet, nullptr);
72     OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet);
73 
74     OH_Drawing_FontMgrDestroy(mgr);
75 }
76 
77 /*
78  * @tc.name: OH_Drawing_FontMgrTest004
79  * @tc.desc: test for matching font family by family name.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest004, TestSize.Level1)
83 {
84     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
85     EXPECT_NE(mgr, nullptr);
86     const char* matchFamilyName = "HarmonyOS-Sans";
87     OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrMatchFamily(mgr, matchFamilyName);
88     EXPECT_NE(fontStyleSet, nullptr);
89     OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet);
90 
91     OH_Drawing_FontMgrDestroy(mgr);
92 }
93 
94 
95 /*
96  * @tc.name: OH_Drawing_FontMgrTest005
97  * @tc.desc: test for matching font typeface by family name and font style.
98  * @tc.type: FUNC
99  */
100 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest005, TestSize.Level1)
101 {
102     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
103     EXPECT_NE(mgr, nullptr);
104     const char* matchFamilyName = "HarmonyOS-Sans";
105     OH_Drawing_FontStyleStruct normalStyle;
106     normalStyle.weight = FONT_WEIGHT_400;
107     normalStyle.width = FONT_WIDTH_NORMAL;
108     normalStyle.slant = FONT_STYLE_NORMAL;
109     OH_Drawing_Typeface *typeface = OH_Drawing_FontMgrMatchFamilyStyle(mgr, matchFamilyName, normalStyle);
110     EXPECT_NE(typeface, nullptr);
111     OH_Drawing_TypefaceDestroy(typeface);
112 
113     OH_Drawing_FontMgrDestroy(mgr);
114 }
115 
116 /*
117  * @tc.name: OH_Drawing_FontMgrTest006
118  * @tc.desc: test for matching font typeface by family name, font style and specific character.
119  * @tc.type: FUNC
120  */
121 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest006, TestSize.Level1)
122 {
123     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
124     EXPECT_NE(mgr, nullptr);
125 
126     const char* matchFamilyName = "HarmonyOS-Sans";
127     OH_Drawing_FontStyleStruct normalStyle;
128     normalStyle.weight = FONT_WEIGHT_400;
129     normalStyle.width = FONT_WIDTH_NORMAL;
130     normalStyle.slant = FONT_STYLE_NORMAL;
131 
132     const char *bcp47[] = {"zh-Hans", "zh-CN"};
133     OH_Drawing_Typeface *CharTypeface = OH_Drawing_FontMgrMatchFamilyStyleCharacter(mgr, matchFamilyName,
134                                                                                     normalStyle, bcp47, 1, ' ');
135     EXPECT_NE(CharTypeface, nullptr);
136     OH_Drawing_TypefaceDestroy(CharTypeface);
137 
138     OH_Drawing_FontMgrDestroy(mgr);
139 }
140 
141 /*
142  * @tc.name: OH_Drawing_FontMgrTest007
143  * @tc.desc: test for getting family name.
144  * @tc.type: FUNC
145  */
146 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest007, TestSize.Level1)
147 {
148     int count = OH_Drawing_FontMgrGetFamilyCount(nullptr);
149     EXPECT_TRUE(count == 0);
150     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
151     EXPECT_NE(mgr, nullptr);
152     char *familyName = OH_Drawing_FontMgrGetFamilyName(nullptr, 0);
153     EXPECT_TRUE(familyName == nullptr);
154     OH_Drawing_FontMgrDestroyFamilyName(familyName);
155     OH_Drawing_FontMgrDestroy(mgr);
156 }
157 
158 /*
159  * @tc.name: OH_Drawing_FontMgrTest008
160  * @tc.desc: test for matching family style.
161  * @tc.type: FUNC
162  */
163 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest008, TestSize.Level1)
164 {
165     OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(nullptr, 0);
166     EXPECT_TRUE(fontStyleSet == nullptr);
167     const char* matchFamilyName = "HarmonyOS-Sans";
168     fontStyleSet = OH_Drawing_FontMgrMatchFamily(nullptr, matchFamilyName);
169     EXPECT_TRUE(fontStyleSet == nullptr);
170 
171     OH_Drawing_FontStyleStruct normalStyle;
172     normalStyle.weight = FONT_WEIGHT_400;
173     normalStyle.width = FONT_WIDTH_NORMAL;
174     normalStyle.slant = FONT_STYLE_NORMAL;
175     OH_Drawing_Typeface *typeface = OH_Drawing_FontMgrMatchFamilyStyle(nullptr, matchFamilyName, normalStyle);
176     EXPECT_TRUE(typeface == nullptr);
177 }
178 
179 /*
180  * @tc.name: OH_Drawing_FontMgrTest009
181  * @tc.desc: test for matching family style character.
182  * @tc.type: FUNC
183  */
184 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest009, TestSize.Level1)
185 {
186     const char* matchFamilyName = "HarmonyOS-Sans";
187     OH_Drawing_FontStyleStruct normalStyle;
188     normalStyle.weight = FONT_WEIGHT_400;
189     normalStyle.width = FONT_WIDTH_NORMAL;
190     normalStyle.slant = FONT_STYLE_NORMAL;
191 
192     const char *bcp47[] = {"zh-Hans", "zh-CN"};
193     OH_Drawing_Typeface *CharTypeface = OH_Drawing_FontMgrMatchFamilyStyleCharacter(nullptr, matchFamilyName,
194                                                                                     normalStyle, bcp47, 1, ' ');
195     EXPECT_TRUE(CharTypeface == nullptr);
196 }
197 
198 /*
199  * @tc.name: OH_Drawing_FontMgrTest010
200  * @tc.desc: test for create a typeface for the given index.
201  * @tc.type: FUNC
202  */
203 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest010, TestSize.Level1)
204 {
205     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
206     OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0);
207     OH_Drawing_Typeface *typeface = OH_Drawing_FontStyleSetCreateTypeface(fontStyleSet, 0);
208     EXPECT_NE(typeface, nullptr);
209     typeface = OH_Drawing_FontStyleSetCreateTypeface(nullptr, 0);
210     EXPECT_TRUE(typeface == nullptr);
211     OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet);
212     OH_Drawing_TypefaceDestroy(typeface);
213     OH_Drawing_FontMgrDestroy(mgr);
214 }
215 
216 /*
217  * @tc.name: OH_Drawing_FontMgrTest011
218  * @tc.desc: test for get font style struct.
219  * @tc.type: FUNC
220  */
221 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest011, TestSize.Level1)
222 {
223     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
224     OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0);
225     OH_Drawing_FontStyleStruct normalStyle;
226     char** styleName = nullptr;
227     normalStyle = OH_Drawing_FontStyleSetGetStyle(fontStyleSet, 0, styleName);
228     EXPECT_TRUE(normalStyle.weight == FONT_WEIGHT_400);
229     OH_Drawing_FontStyleSetFreeStyleName(styleName);
230     OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet);
231     OH_Drawing_FontMgrDestroy(mgr);
232 }
233 
234 /*
235  * @tc.name: OH_Drawing_FontMgrTest012
236  * @tc.desc: test for get typeface by match style.
237  * @tc.type: FUNC
238  */
239 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest012, TestSize.Level1)
240 {
241     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
242     OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0);
243     OH_Drawing_FontStyleStruct normalStyle;
244     normalStyle.weight = FONT_WEIGHT_400;
245     normalStyle.width = FONT_WIDTH_NORMAL;
246     normalStyle.slant = FONT_STYLE_NORMAL;
247     OH_Drawing_Typeface* typeface = OH_Drawing_FontStyleSetMatchStyle(fontStyleSet, normalStyle);
248     EXPECT_NE(typeface, nullptr);
249 
250     typeface = OH_Drawing_FontStyleSetMatchStyle(nullptr, normalStyle);
251     EXPECT_TRUE(typeface == nullptr);
252     OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet);
253     OH_Drawing_TypefaceDestroy(typeface);
254     OH_Drawing_FontMgrDestroy(mgr);
255 }
256 
257 /*
258  * @tc.name: OH_Drawing_FontMgrTest013
259  * @tc.desc: test for get font style set.
260  * @tc.type: FUNC
261  */
262 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest013, TestSize.Level1)
263 {
264     OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate();
265     OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0);
266     int count = OH_Drawing_FontStyleSetCount(fontStyleSet);
267     EXPECT_TRUE(count > 0);
268 
269     count = OH_Drawing_FontStyleSetCount(nullptr);
270     EXPECT_TRUE(count == 0);
271     OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet);
272     OH_Drawing_FontMgrDestroy(mgr);
273 }
274 } // namespace Drawing
275 } // namespace Rosen
276 } // namespace OHOS