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, nullptr);
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("", nullptr);
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_SetCurrentFontId_001
122 * @tc.desc: Verify SetCurrentFontId function, abnormal value test.
123 * @tc.type: FUNC
124 * @tc.require: AR000FQNFR
125 */
126 HWTEST_F(UIFontTest, Graphic_Font_Test_SetCurrentFontId_001, TestSize.Level1)
127 {
128 int8_t ret = UIFont::GetInstance()->SetCurrentFontId(0, 0);
129 EXPECT_EQ(ret, INVALID_RET);
130 }
131
132 /**
133 * @tc.name: Graphic_Font_Test_GetFontId_001
134 * @tc.desc: Verify GetFontId function, nullptr test.
135 * @tc.type: FUNC
136 * @tc.require: AR000FQNFR
137 */
138 HWTEST_F(UIFontTest, Graphic_Font_Test_GetFontId_001, TestSize.Level1)
139 {
140 uint8_t ret = UIFont::GetInstance()->GetFontId(nullptr);
141 EXPECT_EQ(ret, UIFontBuilder::GetInstance()->GetTotalFontId());
142 }
143
144 /**
145 * @tc.name: Graphic_Font_Test_GetFontId_002
146 * @tc.desc: Verify GetFontId function, empty string test.
147 * @tc.type: FUNC
148 * @tc.require: AR000FQNFR
149 */
150 HWTEST_F(UIFontTest, Graphic_Font_Test_GetFontId_002, TestSize.Level1)
151 {
152 uint8_t id = UIFont::GetInstance()->GetFontId("", 0);
153 EXPECT_EQ(id, UIFontBuilder::GetInstance()->GetTotalFontId());
154 }
155
156 /**
157 * @tc.name: Graphic_Font_Test_GetHeight_001
158 * @tc.desc: Verify GetHeight function, abnormal value test.
159 * @tc.type: FUNC
160 * @tc.require: AR000FQNFR
161 */
162 HWTEST_F(UIFontTest, Graphic_Font_Test_GetHeight_001, TestSize.Level1)
163 {
164 uint16_t height = UIFont::GetInstance()->GetHeight();
165 #if ENABLE_VECTOR_FONT
166 EXPECT_EQ(height, 0);
167 #else
168 EXPECT_EQ(height, static_cast<uint16_t>(INVALID_RET));
169 #endif
170 }
171
172 /**
173 * @tc.name: Graphic_Font_Test_GetWidth_001
174 * @tc.desc: Verify GetWidth function, abnormal value test.
175 * @tc.type: FUNC
176 * @tc.require: AR000FQNFR
177 */
178 HWTEST_F(UIFontTest, Graphic_Font_Test_GetWidth_001, TestSize.Level1)
179 {
180 uint16_t width = UIFont::GetInstance()->GetWidth(0, 0);
181 EXPECT_EQ(width, 0);
182 }
183
184 /**
185 * @tc.name: Graphic_Font_Test_GetBitmap_001
186 * @tc.desc: Verify GetBitmap function, abnormal value test.
187 * @tc.type: FUNC
188 * @tc.require: AR000FQNFR
189 */
190 HWTEST_F(UIFontTest, Graphic_Font_Test_GetBitmap_001, TestSize.Level1)
191 {
192 GlyphNode node;
193 uint8_t* bitmap = UIFont::GetInstance()->GetBitmap(0, node, 0);
194 EXPECT_EQ(bitmap, nullptr);
195 }
196
197 /**
198 * @tc.name: Graphic_Font_Test_GetCurrentFontHeader_001
199 * @tc.desc: Verify GetCurrentFontHeader function, abnormal value test.
200 * @tc.type: FUNC
201 * @tc.require: AR000FQNFR
202 */
203 HWTEST_F(UIFontTest, Graphic_Font_Test_GetCurrentFontHeader_001, TestSize.Level1)
204 {
205 FontHeader header;
206 int8_t res = UIFont::GetInstance()->GetCurrentFontHeader(header);
207 EXPECT_EQ(res, INVALID_RET);
208 }
209
210 /**
211 * @tc.name: Graphic_Font_Test_GetFontWeight_001
212 * @tc.desc: Verify GetFontWeight function, abnormal value test.
213 * @tc.type: FUNC
214 * @tc.require: AR000FQNFR
215 */
216 HWTEST_F(UIFontTest, Graphic_Font_Test_GetFontWeight_001, TestSize.Level1)
217 {
218 uint8_t fontWeight = UIFont::GetInstance()->GetFontWeight(FONT_ID);
219 #if ENABLE_VECTOR_FONT
220 EXPECT_EQ(fontWeight, FONT_BPP);
221 #else
222 EXPECT_EQ(fontWeight, 0);
223 #endif
224 }
225
226 /**
227 * @tc.name: Graphic_Font_Test_GetFontInfo_001
228 * @tc.desc: Verify GetFontInfo function, abnormal value test.
229 * @tc.type: FUNC
230 * @tc.require: AR000FQNFR
231 */
232 HWTEST_F(UIFontTest, Graphic_Font_Test_GetFontInfo_001, TestSize.Level1)
233 {
234 const UITextLanguageFontParam* fontParam = UIFont::GetInstance()->GetFontInfo(FONT_ID);
235 EXPECT_EQ(fontParam, nullptr);
236 }
237
238 /**
239 * @tc.name: Graphic_Font_Test_GetShapingFontId_001
240 * @tc.desc: Verify GetShapingFontId function, abnormal value test.
241 * @tc.type: FUNC
242 * @tc.require: AR000FQNFR
243 */
244 HWTEST_F(UIFontTest, Graphic_Font_Test_GetShapingFontId_001, TestSize.Level1)
245 {
246 uint8_t ttfId = 0;
247 uint32_t script = 0;
248 uint8_t fontId = UIFont::GetInstance()->GetShapingFontId("", ttfId, script, FONT_ID, 0);
249 EXPECT_EQ(fontId, 0);
250 }
251
252 /**
253 * @tc.name: Graphic_Font_Test_RegisterFontInfo_001
254 * @tc.desc: Verify UIFont::RegisterFontInfo function, error font name.
255 * @tc.type: FUNC
256 * @tc.require: AR000F3R7C
257 */
258 HWTEST_F(UIFontTest, Graphic_Font_Test_RegisterFontInfo_001, TestSize.Level1)
259 {
260 uint8_t ret = UIFont::GetInstance()->RegisterFontInfo("error");
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_RegisterFontInfo_002
270 * @tc.desc: Verify UIFont::RegisterFontInfo function, error font file path.
271 * @tc.type: FUNC
272 * @tc.require: AR000F3R7C
273 */
274 HWTEST_F(UIFontTest, Graphic_Font_Test_RegisterFontInfo_002, TestSize.Level0)
275 {
276 uint8_t ret = UIFont::GetInstance()->RegisterFontInfo("ui-font.ttf");
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_001
286 * @tc.desc: Verify UIFont::UnregisterFontInfo function, error font name.
287 * @tc.type: FUNC
288 * @tc.require: AR000F3R7C
289 */
290 HWTEST_F(UIFontTest, Graphic_Font_Test_UnregisterFontInfo_001, TestSize.Level1)
291 {
292 uint8_t ret = UIFont::GetInstance()->UnregisterFontInfo("error font name");
293 #if ENABLE_VECTOR_FONT
294 EXPECT_EQ(ret, FONT_ERROR_RET);
295 #else
296 EXPECT_EQ(ret, 0);
297 #endif
298 }
299
300 /**
301 * @tc.name: Graphic_Font_Test_UnregisterFontInfo_002
302 * @tc.desc: Verify UIFont::UnregisterFontInfo function, unregister fontsTable.
303 * @tc.type: FUNC
304 * @tc.require: AR000F3R7C
305 */
306 HWTEST_F(UIFontTest, Graphic_Font_Test_UnregisterFontInfo_002, TestSize.Level0)
307 {
308 const UITextLanguageFontParam* fontsTable = nullptr;
309 uint8_t ret = UIFont::GetInstance()->UnregisterFontInfo(fontsTable, 0);
310 EXPECT_EQ(ret, 0);
311 }
312 } // namespace OHOS