1 /*
2  * Copyright (c) 2020-2021 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 "font/ui_font.h"
17 #if ENABLE_VECTOR_FONT
18 #include "font/ui_font_vector.h"
19 #else
20 #include "font/ui_font_bitmap.h"
21 #endif
22 #include <climits>
23 #include <gtest/gtest.h>
24 
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace {
28     constexpr uint8_t FONT_ERROR_RET = 0xFF;
29     constexpr int8_t INVALID_RET = -1;
30     constexpr uint8_t FONT_ID = 0xFF;
31     constexpr uint8_t FONT_BPP = 8;
32 }
33 class UIFontTest : public testing::Test {
34 public:
UIFontTest()35     UIFontTest() {}
~UIFontTest()36     virtual ~UIFontTest() {}
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     static BaseFont* font_;
40 };
41 
42 BaseFont* UIFontTest::font_ = nullptr;
43 
SetUpTestCase()44 void UIFontTest::SetUpTestCase()
45 {
46     if (font_ == nullptr) {
47 #if ENABLE_VECTOR_FONT
48         font_ = new UIFontVector();
49 #else
50         font_ = new UIFontBitmap();
51 #endif
52     }
53 }
54 
TearDownTestCase()55 void UIFontTest::TearDownTestCase()
56 {
57     if (font_ != nullptr) {
58         delete font_;
59         font_ = nullptr;
60     }
61 }
62 
63 /**
64  * @tc.name: Graphic_Font_Test_GetInstance_001
65  * @tc.desc: Verify UIFont::GetInstance function, not nullptr.
66  * @tc.type: FUNC
67  * @tc.require: SR000F3PEK
68  */
69 HWTEST_F(UIFontTest, Graphic_Font_Test_GetInstance_001, TestSize.Level0)
70 {
71     UIFont* font = UIFont::GetInstance();
72     EXPECT_NE(font, nullptr);
73 }
74 
75 /**
76  * @tc.name: Graphic_Font_Test_IsVectorFont_001
77  * @tc.desc: Verify IsVectorFont function, equal.
78  * @tc.type: FUNC
79  * @tc.require: SR000FQNFQ
80  */
81 HWTEST_F(UIFontTest, Graphic_Font_Test_IsVectorFont_001, TestSize.Level1)
82 {
83 #if ENABLE_VECTOR_FONT
84     bool ret = UIFont::GetInstance()->IsVectorFont();
85     EXPECT_EQ(ret, true);
86 #else
87     bool ret = UIFont::GetInstance()->IsVectorFont();
88     EXPECT_EQ(ret, false);
89 #endif
90 }
91 
92 /**
93  * @tc.name: Graphic_Font_Test_SetFontPath_001
94  * @tc.desc: Verify SetFontPath function, nullptr test.
95  * @tc.type: FUNC
96  * @tc.require: SR000FQNFQ
97  */
98 HWTEST_F(UIFontTest, Graphic_Font_Test_SetFontPath_001, TestSize.Level1)
99 {
100     int8_t ret = UIFont::GetInstance()->SetFontPath(nullptr, BaseFont::FontType::DYNAMIC_FONT);
101     EXPECT_EQ(ret, INVALID_RET);
102 }
103 
104 /**
105  * @tc.name: Graphic_Font_Test_SetFontPath_002
106  * @tc.desc: Verify SetFontPath function, empty string test.
107  * @tc.type: FUNC
108  * @tc.require: SR000FQNFQ
109  */
110 HWTEST_F(UIFontTest, Graphic_Font_Test_SetFontPath_002, TestSize.Level1)
111 {
112     int8_t ret = UIFont::GetInstance()->SetFontPath("", BaseFont::FontType::DYNAMIC_FONT);
113 #if ENABLE_VECTOR_FONT
114     EXPECT_EQ(ret, 0);
115 #else
116     EXPECT_EQ(ret, INVALID_RET);
117 #endif
118 }
119 
120 /**
121  * @tc.name: Graphic_Font_Test_GetFontId_001
122  * @tc.desc: Verify GetFontId function, nullptr test.
123  * @tc.type: FUNC
124  * @tc.require: AR000FQNFR
125  */
126 HWTEST_F(UIFontTest, Graphic_Font_Test_GetFontId_001, TestSize.Level1)
127 {
128     uint8_t ret = UIFont::GetInstance()->GetFontId(nullptr);
129     EXPECT_EQ(ret, UIFontBuilder::GetInstance()->GetTotalFontId());
130 }
131 
132 /**
133  * @tc.name: Graphic_Font_Test_GetFontId_002
134  * @tc.desc: Verify GetFontId function, empty string test.
135  * @tc.type: FUNC
136  * @tc.require: AR000FQNFR
137  */
138 HWTEST_F(UIFontTest, Graphic_Font_Test_GetFontId_002, TestSize.Level1)
139 {
140     uint8_t id = UIFont::GetInstance()->GetFontId("", 0);
141     EXPECT_EQ(id, UIFontBuilder::GetInstance()->GetTotalFontId());
142 }
143 
144 /**
145  * @tc.name: Graphic_Font_Test_GetHeight_001
146  * @tc.desc: Verify GetHeight function, abnormal value test.
147  * @tc.type: FUNC
148  * @tc.require: AR000FQNFR
149  */
150 HWTEST_F(UIFontTest, Graphic_Font_Test_GetHeight_001, TestSize.Level1)
151 {
152     uint16_t height = UIFont::GetInstance()->GetHeight(FONT_ID, 0);
153     EXPECT_EQ(height, static_cast<uint16_t>(INVALID_RET));
154 }
155 
156 /**
157  * @tc.name: Graphic_Font_Test_GetWidth_001
158  * @tc.desc: Verify GetWidth function, abnormal value test.
159  * @tc.type: FUNC
160  * @tc.require: AR000FQNFR
161  */
162 HWTEST_F(UIFontTest, Graphic_Font_Test_GetWidth_001, TestSize.Level1)
163 {
164     uint16_t width = UIFont::GetInstance()->GetWidth(0, 0, 0, 0);
165     EXPECT_EQ(width, 0);
166 }
167 
168 /**
169  * @tc.name: Graphic_Font_Test_GetBitmap_001
170  * @tc.desc: Verify GetBitmap function, abnormal value test.
171  * @tc.type: FUNC
172  * @tc.require: AR000FQNFR
173  */
174 HWTEST_F(UIFontTest, Graphic_Font_Test_GetBitmap_001, TestSize.Level1)
175 {
176     GlyphNode node;
177     uint8_t* bitmap = UIFont::GetInstance()->GetBitmap(0, node, 0, 0, 0);
178     EXPECT_EQ(bitmap, nullptr);
179 }
180 
181 /**
182  * @tc.name: Graphic_Font_Test_GetFontHeader_001
183  * @tc.desc: Verify GetFontHeader function, abnormal value test.
184  * @tc.type: FUNC
185  * @tc.require: AR000FQNFR
186  */
187 HWTEST_F(UIFontTest, Graphic_Font_Test_GetFontHeader_001, TestSize.Level1)
188 {
189     FontHeader header;
190     int8_t res = UIFont::GetInstance()->GetFontHeader(header, FONT_ID, 0);
191     EXPECT_EQ(res, INVALID_RET);
192 }
193 
194 /**
195  * @tc.name: Graphic_Font_Test_GetFontWeight_001
196  * @tc.desc: Verify GetFontWeight function, abnormal value test.
197  * @tc.type: FUNC
198  * @tc.require: AR000FQNFR
199  */
200 HWTEST_F(UIFontTest, Graphic_Font_Test_GetFontWeight_001, TestSize.Level1)
201 {
202     uint8_t fontWeight = UIFont::GetInstance()->GetFontWeight(FONT_ID);
203 #if ENABLE_VECTOR_FONT
204     EXPECT_EQ(fontWeight, FONT_BPP);
205 #else
206     EXPECT_EQ(fontWeight, 0);
207 #endif
208 }
209 
210 /**
211  * @tc.name: Graphic_Font_Test_GetFontInfo_001
212  * @tc.desc: Verify GetFontInfo function, abnormal value test.
213  * @tc.type: FUNC
214  * @tc.require: AR000FQNFR
215  */
216 HWTEST_F(UIFontTest, Graphic_Font_Test_GetFontInfo_001, TestSize.Level1)
217 {
218     const UITextLanguageFontParam* fontParam = UIFont::GetInstance()->GetFontInfo(FONT_ID);
219     EXPECT_EQ(fontParam, nullptr);
220 }
221 
222 /**
223  * @tc.name: Graphic_Font_Test_GetShapingFontId_001
224  * @tc.desc: Verify GetShapingFontId function, abnormal value test.
225  * @tc.type: FUNC
226  * @tc.require: AR000FQNFR
227  */
228 HWTEST_F(UIFontTest, Graphic_Font_Test_GetShapingFontId_001, TestSize.Level1)
229 {
230     uint8_t ttfId = 0;
231     uint32_t script = 0;
232     uint16_t fontId = UIFont::GetInstance()->GetShapingFontId("", ttfId, script, FONT_ID, 0);
233     EXPECT_EQ(fontId, 0);
234 }
235 
236 /**
237  * @tc.name: Graphic_Font_Test_RegisterFontInfo_001
238  * @tc.desc: Verify UIFont::RegisterFontInfo function, error font name.
239  * @tc.type: FUNC
240  * @tc.require: AR000F3R7C
241  */
242 HWTEST_F(UIFontTest, Graphic_Font_Test_RegisterFontInfo_001, TestSize.Level1)
243 {
244     uint8_t ret = UIFont::GetInstance()->RegisterFontInfo("error");
245 #if ENABLE_VECTOR_FONT
246     EXPECT_EQ(ret, FONT_ERROR_RET);
247 #else
248     EXPECT_EQ(ret, 0);
249 #endif
250 }
251 
252 /**
253  * @tc.name: Graphic_Font_Test_RegisterFontInfo_002
254  * @tc.desc: Verify UIFont::RegisterFontInfo function, error font file path.
255  * @tc.type: FUNC
256  * @tc.require: AR000F3R7C
257  */
258 HWTEST_F(UIFontTest, Graphic_Font_Test_RegisterFontInfo_002, TestSize.Level0)
259 {
260     uint8_t ret = UIFont::GetInstance()->RegisterFontInfo("ui-font.ttf");
261 #if ENABLE_VECTOR_FONT
262     EXPECT_EQ(ret, FONT_ERROR_RET);
263 #else
264     EXPECT_EQ(ret, 0);
265 #endif
266 }
267 
268 /**
269  * @tc.name: Graphic_Font_Test_UnregisterFontInfo_001
270  * @tc.desc: Verify UIFont::UnregisterFontInfo function, error font name.
271  * @tc.type: FUNC
272  * @tc.require: AR000F3R7C
273  */
274 HWTEST_F(UIFontTest, Graphic_Font_Test_UnregisterFontInfo_001, TestSize.Level1)
275 {
276     uint8_t ret = UIFont::GetInstance()->UnregisterFontInfo("error font name");
277 #if ENABLE_VECTOR_FONT
278     EXPECT_EQ(ret, FONT_ERROR_RET);
279 #else
280     EXPECT_EQ(ret, 0);
281 #endif
282 }
283 
284 /**
285  * @tc.name: Graphic_Font_Test_UnregisterFontInfo_002
286  * @tc.desc: Verify UIFont::UnregisterFontInfo function, unregister fontsTable.
287  * @tc.type: FUNC
288  * @tc.require: AR000F3R7C
289  */
290 HWTEST_F(UIFontTest, Graphic_Font_Test_UnregisterFontInfo_002, TestSize.Level0)
291 {
292     const UITextLanguageFontParam* fontsTable = nullptr;
293     uint8_t ret = UIFont::GetInstance()->UnregisterFontInfo(fontsTable, 0);
294     EXPECT_EQ(ret, 0);
295 }
296 } // namespace OHOS
297