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 <gtest/gtest.h>
17 #include "font_manager_test.h"
18 #define private public
19 #define protected public
20 #include "font_manager.h"
21 #undef private
22 #undef protected
23 #include "file_utils.h"
24 #include "font_define.h"
25 #include <fcntl.h>
26 #include <string>
27 #include <vector>
28 #include <algorithm>
29 #include <filesystem>
30 #include <iostream>
31 #include <dirent.h>
32 #include <unistd.h>
33
34 namespace {
35 const std::string INSTALL_PATH = "/data/service/el1/public/for-all-app/fonts/";
36 const std::string TEMP_PATH = "/data/service/el1/public/for-all-app/fonts/temp/";
37 const std::string FONT_CONFIG_FILE = INSTALL_PATH + "install_fontconfig.json";
38 const std::string FONT_PATH = "/data/test/HarmonyOS_Sans.ttf";
39 const std::string FONT_FULL_NAME = "HarmonyOS Sans";
40 const std::string TTC_FONT_PATH = "/data/test/NotoSansCJK-Regular.ttc";
41 const std::vector<std::string> TTC_FONT_FULL_NAME{"Noto Sans CJK JP",
42 "Noto Sans CJK KR",
43 "Noto Sans CJK SC",
44 "Noto Sans CJK TC",
45 "Noto Sans CJK HK",
46 "Noto Sans Mono CJK JP",
47 "Noto Sans Mono CJK KR",
48 "Noto Sans Mono CJK SC",
49 "Noto Sans Mono CJK TC",
50 "Noto Sans Mono CJK HK"};
51 }
52
53 using testing::ext::TestSize;
54 using namespace std;
55
56 namespace OHOS {
57 namespace Global {
58 namespace FontManager {
59
60 class FontManagerTest : public testing::Test {
61 public:
FontManagerTest()62 FontManagerTest(){};
63 static void SetUpTestCase(void);
64 static void TearDownTestCase(void);
65 void SetUp();
66 void TearDown();
67
68 protected:
69 FontManager manager_;
70 };
71
SetUpTestCase(void)72 void FontManagerTest::SetUpTestCase(void)
73 {}
74
TearDownTestCase(void)75 void FontManagerTest::TearDownTestCase(void)
76 {
77 FileUtils::DeleteDir(TEMP_PATH, true);
78 }
79
SetUp(void)80 void FontManagerTest::SetUp(void)
81 {}
82
TearDown(void)83 void FontManagerTest::TearDown(void)
84 {
85 FileUtils::DeleteDir(INSTALL_PATH, false);
86 }
87
88 /**
89 * @tc.name: FontManagerFuncTest001
90 * @tc.desc: Test FontManager InstallFont case
91 * @tc.type: FUNC
92 */
93 HWTEST_F(FontManagerTest, FontManagerFuncTest001, TestSize.Level1)
94 {
95 int fd = open(FONT_PATH.c_str(), O_RDONLY);
96 EXPECT_EQ(fd >= 0, true);
97 int ret = this->manager_.InstallFont(fd);
98 EXPECT_EQ(ret, SUCCESS);
99 if (fd >= 0) {
100 close(fd);
101 }
102 fd = open(FONT_PATH.c_str(), O_RDONLY);
103 ret = this->manager_.InstallFont(fd);
104 EXPECT_EQ(ret, ERR_INSTALLED_ALRADY);
105 }
106
107 /**
108 * @tc.name: FontManagerFuncTest002
109 * @tc.desc: Test FontManager UninstallFont case
110 * @tc.type: FUNC
111 */
112 HWTEST_F(FontManagerTest, FontManagerFuncTest002, TestSize.Level1)
113 {
114 int fd = open(FONT_PATH.c_str(), O_RDONLY);
115 EXPECT_EQ(fd >= 0, true);
116 int ret = this->manager_.InstallFont(fd);
117 EXPECT_EQ(ret, SUCCESS);
118 if (fd >= 0) {
119 close(fd);
120 }
121 ret = this->manager_.UninstallFont(FONT_FULL_NAME);
122 EXPECT_EQ(ret, SUCCESS);
123 ret = this->manager_.UninstallFont(FONT_FULL_NAME);
124 EXPECT_EQ(ret, ERR_UNINSTALL_FILE_NOT_EXISTS);
125 ret = this->manager_.UninstallFont("");
126 EXPECT_EQ(ret, ERR_UNINSTALL_FILE_NOT_EXISTS);
127 }
128
129 /**
130 * @tc.name: FontManagerFuncTest003
131 * @tc.desc: Test FontManager GetFontFullName case
132 * @tc.type: FUNC
133 */
134 HWTEST_F(FontManagerTest, FontManagerFuncTest003, TestSize.Level1)
135 {
136 int fd = open(FONT_PATH.c_str(), O_RDONLY);
137 EXPECT_EQ(fd >= 0, true);
138 auto fullNameVector = this->manager_.GetFontFullName(fd);
139 std::vector<std::string> fullName{"HarmonyOS Sans"};
140 EXPECT_EQ(fullName, fullNameVector);
141 if (fd >= 0) {
142 close(fd);
143 }
144 }
145
146 /**
147 * @tc.name: FontManagerFuncTest004
148 * @tc.desc: Test FontManager GetFontFullName error case
149 * @tc.type: FUNC
150 */
151 HWTEST_F(FontManagerTest, FontManagerFuncTest004, TestSize.Level1)
152 {
153 const std::string errTTFPath = "/data/test/errorTTF.ttf";
154 const std::string emptyTTFPath = "/data/test/emptyTTF.ttf";
155 const std::string errorType = "/data/test/errorType.txt";
156 const std::string errorTTCPath = "/data/test/errorTTC.ttc";
157 const std::string emptyTTCPath = "/data/test/emptyTTC.ttc";
158 int fd = open(errTTFPath.c_str(), O_RDONLY);
159 EXPECT_EQ(fd >= 0, true);
160 auto fullNameVector = this->manager_.GetFontFullName(fd);
161 EXPECT_EQ(fullNameVector.size(), 0);
162 if (fd >= 0) {
163 close(fd);
164 }
165 fd = open(emptyTTFPath.c_str(), O_RDONLY);
166 EXPECT_EQ(fd >= 0, true);
167 fullNameVector = this->manager_.GetFontFullName(fd);
168 EXPECT_EQ(fullNameVector.size(), 0);
169 if (fd >= 0) {
170 close(fd);
171 }
172 fd = open(errorType.c_str(), O_RDONLY);
173 EXPECT_EQ(fd >= 0, true);
174 fullNameVector = this->manager_.GetFontFullName(fd);
175 EXPECT_EQ(fullNameVector.size(), 0);
176 if (fd >= 0) {
177 close(fd);
178 }
179 fd = open(errorTTCPath.c_str(), O_RDONLY);
180 EXPECT_EQ(fd >= 0, true);
181 fullNameVector = this->manager_.GetFontFullName(fd);
182 EXPECT_EQ(fullNameVector.size(), 0);
183 if (fd >= 0) {
184 close(fd);
185 }
186 fd = open(emptyTTCPath.c_str(), O_RDONLY);
187 EXPECT_EQ(fd >= 0, true);
188 fullNameVector = this->manager_.GetFontFullName(fd);
189 EXPECT_EQ(fullNameVector.size(), 0);
190 if (fd >= 0) {
191 close(fd);
192 }
193 }
194
195 /**
196 * @tc.name: FontManagerFuncTest005
197 * @tc.desc: Test FontManager CheckFontConfigPath case
198 * @tc.type: FUNC
199 */
200 HWTEST_F(FontManagerTest, FontManagerFuncTest005, TestSize.Level1)
201 {
202 int ret = this->manager_.CheckFontConfigPath();
203 EXPECT_EQ(ret, true);
204 FileUtils::RemoveFile(FONT_CONFIG_FILE);
205 ret = this->manager_.CheckFontConfigPath();
206 EXPECT_EQ(ret, true);
207 ret = this->manager_.CheckFontConfigPath();
208 EXPECT_EQ(ret, true);
209 }
210
211 /**
212 * @tc.name: FontManagerFuncTest006
213 * @tc.desc: Test FontManager ERR_MAX_FILE_COUNT more than 200 case
214 * @tc.type: FUNC
215 */
216 HWTEST_F(FontManagerTest, FontManagerFuncTest006, TestSize.Level1)
217 {
218 int fd = open("/data/test/200install_fontconfig.json", O_RDONLY);
219 EXPECT_EQ(FileUtils::CopyFile(fd, FONT_CONFIG_FILE), true);
220 fd = open(FONT_PATH.c_str(), O_RDONLY);
221 EXPECT_EQ(fd >= 0, true);
222 int ret = this->manager_.InstallFont(fd);
223 EXPECT_EQ(ret, ERR_MAX_FILE_COUNT);
224 if (fd >= 0) {
225 close(fd);
226 }
227 }
228
229 /**
230 * @tc.name: FontManagerFuncTest007
231 * @tc.desc: Test FontManager ERR_MAX_FILE_COUNT less than 200 case
232 * @tc.type: FUNC
233 */
234 HWTEST_F(FontManagerTest, FontManagerFuncTest007, TestSize.Level1)
235 {
236 int fd = open("/data/test/199install_fontconfig.json", O_RDONLY);
237 EXPECT_EQ(FileUtils::CopyFile(fd, FONT_CONFIG_FILE), true);
238 if (fd > 0) {
239 close(fd);
240 }
241 fd = open(FONT_PATH.c_str(), O_RDONLY);
242 EXPECT_EQ(fd >= 0, true);
243 int ret = this->manager_.InstallFont(fd);
244 EXPECT_EQ(ret, SUCCESS);
245 if (fd >= 0) {
246 close(fd);
247 }
248 }
249
250 /**
251 * @tc.name: FontManagerFuncTest008
252 * @tc.desc: Test FontManager TTC InstallFont case
253 * @tc.type: FUNC
254 */
255 HWTEST_F(FontManagerTest, FontManagerFuncTest008, TestSize.Level1)
256 {
257 int fd = open(TTC_FONT_PATH.c_str(), O_RDONLY);
258 EXPECT_EQ(fd >= 0, true);
259 int ret = this->manager_.InstallFont(fd);
260 EXPECT_EQ(ret, SUCCESS);
261 if (fd >= 0) {
262 close(fd);
263 }
264 fd = open(TTC_FONT_PATH.c_str(), O_RDONLY);
265 ret = this->manager_.InstallFont(fd);
266 EXPECT_EQ(ret, ERR_INSTALLED_ALRADY);
267 }
268
269 /**
270 * @tc.name: FontManagerFuncTest009
271 * @tc.desc: Test FontManager TTC UninstallFont case
272 * @tc.type: FUNC
273 */
274 HWTEST_F(FontManagerTest, FontManagerFuncTest009, TestSize.Level1)
275 {
276 int fd = open(TTC_FONT_PATH.c_str(), O_RDONLY);
277 EXPECT_EQ(fd >= 0, true);
278 int ret = this->manager_.InstallFont(fd);
279 EXPECT_EQ(ret, SUCCESS);
280 if (fd >= 0) {
281 close(fd);
282 }
283 ret = this->manager_.UninstallFont(TTC_FONT_FULL_NAME[0]);
284 EXPECT_EQ(ret, SUCCESS);
285 ret = this->manager_.UninstallFont(TTC_FONT_FULL_NAME[0]);
286 EXPECT_EQ(ret, ERR_UNINSTALL_FILE_NOT_EXISTS);
287 ret = this->manager_.UninstallFont("");
288 EXPECT_EQ(ret, ERR_UNINSTALL_FILE_NOT_EXISTS);
289 }
290
291 /**
292 * @tc.name: FontManagerFuncTest010
293 * @tc.desc: Test FontManager TTC GetFontFullName case
294 * @tc.type: FUNC
295 */
296 HWTEST_F(FontManagerTest, FontManagerFuncTest010, TestSize.Level1)
297 {
298 int fd = open(TTC_FONT_PATH.c_str(), O_RDONLY);
299 EXPECT_EQ(fd >= 0, true);
300 auto fullNameVector = this->manager_.GetFontFullName(fd);
301 std::sort(fullNameVector.begin(), fullNameVector.end());
302 auto ttcFullName = TTC_FONT_FULL_NAME;
303 std::sort(ttcFullName.begin(), ttcFullName.end());
304 EXPECT_EQ(fullNameVector, ttcFullName);
305 if (fd >= 0) {
306 close(fd);
307 }
308 }
309
310 /**
311 * @tc.name: FontManagerFuncTest011
312 * @tc.desc: Test FontManager TTC GetFontFullName case
313 * @tc.type: FUNC
314 */
315 HWTEST_F(FontManagerTest, FontManagerFuncTest011, TestSize.Level1)
316 {
317 const std::string errTTFPath = "/data/test/errorTTF.ttf";
318 const std::string emptyTTFPath = "/data/test/emptyTTF.ttf";
319 const std::string errorType = "/data/test/errorType.txt";
320 const std::string errorTTCPath = "/data/test/errorTTC.ttc";
321 const std::string emptyTTCPath = "/data/test/emptyTTC.ttc";
322 int fd = open(errTTFPath.c_str(), O_RDONLY);
323 EXPECT_EQ(fd >= 0, true);
324 int ret = this->manager_.InstallFont(fd);
325 EXPECT_EQ(ret, ERR_FILE_VERIFY_FAIL);
326 if (fd >= 0) {
327 close(fd);
328 }
329 fd = open(emptyTTFPath.c_str(), O_RDONLY);
330 EXPECT_EQ(fd >= 0, true);
331 ret = this->manager_.InstallFont(fd);
332 EXPECT_EQ(ret, ERR_FILE_VERIFY_FAIL);
333 if (fd >= 0) {
334 close(fd);
335 }
336 fd = open(errorType.c_str(), O_RDONLY);
337 EXPECT_EQ(fd >= 0, true);
338 ret = this->manager_.InstallFont(fd);
339 EXPECT_EQ(ret, ERR_FILE_VERIFY_FAIL);
340 if (fd >= 0) {
341 close(fd);
342 }
343 fd = open(errorTTCPath.c_str(), O_RDONLY);
344 EXPECT_EQ(fd >= 0, true);
345 ret = this->manager_.InstallFont(fd);
346 EXPECT_EQ(ret, ERR_FILE_VERIFY_FAIL);
347 if (fd >= 0) {
348 close(fd);
349 }
350 fd = open(emptyTTCPath.c_str(), O_RDONLY);
351 EXPECT_EQ(fd >= 0, true);
352 ret = this->manager_.InstallFont(fd);
353 EXPECT_EQ(ret, ERR_FILE_VERIFY_FAIL);
354 if (fd >= 0) {
355 close(fd);
356 }
357 }
358
359 /**
360 * @tc.name: FontManagerFuncTest012
361 * @tc.desc: Test FontManager Repeat InstallFont case
362 * @tc.type: FUNC
363 */
364 HWTEST_F(FontManagerTest, FontManagerFuncTest012, TestSize.Level1)
365 {
366 FileUtils::CreatDirWithPermission("/data/test/testRepeats/");
367 const std::string fontPath1 = "/data/test/HarmonyOS_Sans.ttf";
368 const std::string fontPath4 = "/data/test/NotoSansVai-Regular.ttf";
369 const std::string fontPath5 = "/data/test/testRepeats/HarmonyOS_Sans.ttf";
370
371 int fd = open(fontPath1.c_str(), O_RDONLY);
372 EXPECT_EQ(fd >= 0, true);
373 int ret = this->manager_.InstallFont(fd);
374 if (fd > 0) {
375 close(fd);
376 }
377 EXPECT_EQ(ret, SUCCESS);
378 ASSERT_TRUE(FileUtils::CheckPathExist("/data/test/testRepeats/"));
379 fd = open(fontPath4.c_str(), O_RDONLY);
380 FileUtils::CopyFile(fd, fontPath5);
381 if (fd > 0) {
382 close(fd);
383 }
384 fd = open(fontPath5.c_str(), O_RDONLY);
385 EXPECT_EQ(fd >= 0, true);
386 ret = this->manager_.InstallFont(fd);
387 if (fd > 0) {
388 close(fd);
389 }
390 EXPECT_EQ(ret, SUCCESS);
391 std::filesystem::path rPath("/data/test/testRepeats/");
392 for (const auto &file : std::filesystem::directory_iterator(rPath)) {
393 if (file.is_regular_file()) {
394 std::filesystem::remove(file);
395 }
396 }
397 EXPECT_EQ(rmdir("/data/test/testRepeats") != -1, true);
398 }
399
400 /**
401 * @tc.name: FontManagerFuncTest012
402 * @tc.desc: Test FontManager Repeat InstallFont case
403 * @tc.type: FUNC
404 */
405 HWTEST_F(FontManagerTest, FontManagerFuncTest013, TestSize.Level1)
406 {
407 FileUtils::CreatDirWithPermission("/data/test/testRepeats/");
408 const std::string fontPath2 = "/data/test/NotoSansCJK-Regular.ttc";
409 const std::string fontPath3 = "/data/test/NotoSerifCJK-Regular.ttc";
410 const std::string fontPath6 = "/data/test/testRepeats/NotoSerifCJK-Regular.ttc";
411
412 int fd = open(fontPath2.c_str(), O_RDONLY);
413 EXPECT_EQ(fd >= 0, true);
414 int ret = this->manager_.InstallFont(fd);
415 if (fd > 0) {
416 close(fd);
417 }
418 EXPECT_EQ(ret, SUCCESS);
419 ASSERT_TRUE(FileUtils::CheckPathExist("/data/test/testRepeats/"));
420 fd = open(fontPath3.c_str(), O_RDONLY);
421 FileUtils::CopyFile(fd, fontPath6);
422 if (fd > 0) {
423 close(fd);
424 }
425 fd = open(fontPath6.c_str(), O_RDONLY);
426 EXPECT_EQ(fd >= 0, true);
427 ret = this->manager_.InstallFont(fd);
428 if (fd > 0) {
429 close(fd);
430 }
431 EXPECT_EQ(ret, SUCCESS);
432 std::filesystem::path rPath("/data/test/testRepeats/");
433 for (const auto &file : std::filesystem::directory_iterator(rPath)) {
434 if (file.is_regular_file()) {
435 std::filesystem::remove(file);
436 }
437 }
438 EXPECT_EQ(rmdir("/data/test/testRepeats") != -1, true);
439 }
440
441 } // namespace FontManager
442 } // namespace Global
443 } // namespace OHOS