1 /*
2 * Copyright (c) 2023 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 <fstream>
17 #include <gtest/gtest.h>
18
19 #include "font_config.h"
20 #include "font_parser.h"
21 #include "cmap_table_parser.h"
22 #include "name_table_parser.h"
23 #include "post_table_parser.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace Rosen {
30 namespace TextEngine {
31 static const std::string FILE_NAME = "/system/fonts/visibility_list.json";
32
33 class FontParserTest : public testing::Test {
34 };
35
GetFontSet(const char * fname)36 std::vector<std::string> GetFontSet(const char* fname)
37 {
38 FontConfig fontConfig(fname);
39 return fontConfig.GetFontSet();
40 }
41
42 /**
43 * @tc.name: FontParserTest1
44 * @tc.desc: test get fontSet file parser
45 * @tc.type:FUNC
46 */
47 HWTEST_F(FontParserTest, FontParserTest1, TestSize.Level1)
48 {
49 auto fontSet1 = GetFontSet(nullptr);
50 EXPECT_EQ(fontSet1.size(), 0);
51
52 std::ifstream fileStream(FILE_NAME.c_str());
53 if (fileStream.is_open()) {
54 auto fontSet2 = GetFontSet(FILE_NAME.c_str());
55 EXPECT_NE(fontSet2.size(), 0);
56 fileStream.close();
57 } else {
58 auto fontSet2 = GetFontSet(FILE_NAME.c_str());
59 EXPECT_EQ(fontSet2.size(), 0);
60 }
61 }
62
63 /**
64 * @tc.name: FontParserTest2
65 * @tc.desc: test font file parser
66 * @tc.type:FUNC
67 */
68 HWTEST_F(FontParserTest, FontParserTest2, TestSize.Level1)
69 {
70 FontParser fontParser;
71 auto visibilityFonts = fontParser.GetVisibilityFonts();
72 fontParser.GetVisibilityFontByName("Noto Sans Regular");
73 std::ifstream fileStream(FILE_NAME.c_str());
74 if (fileStream.is_open()) {
75 EXPECT_NE(visibilityFonts.size(), 0);
76 fileStream.close();
77 } else {
78 EXPECT_EQ(visibilityFonts.size(), 0);
79 }
80 }
81
82 /**
83 * @tc.name: FontParserTest3
84 * @tc.desc: test font file parser
85 * @tc.type:FUNC
86 */
87 HWTEST_F(FontParserTest, FontParserTest3, TestSize.Level1)
88 {
89 FontParser fontParser;
90 std::unique_ptr<FontParser::FontDescriptor> font =
91 fontParser.GetVisibilityFontByName("Noto Sans Regular");
92 ASSERT_NE(font, nullptr);
93 EXPECT_EQ(font->fontFamily, "Noto Sans");
94 }
95
96 /**
97 * @tc.name: FontConfigTest1
98 * @tc.desc: test font file parser
99 * @tc.type:FUNC
100 */
101 HWTEST_F(FontParserTest, FontConfigTest1, TestSize.Level1)
102 {
103 FontConfigJson fontConfigJson;
104 EXPECT_EQ(fontConfigJson.ParseFile(), 0);
105 auto info = fontConfigJson.GetFontConfigJsonInfo();
106 ASSERT_NE(info, nullptr);
107 EXPECT_EQ(info->fontDirSet.size(), 1);
108 fontConfigJson.Dump();
109 }
110
111 /**
112 * @tc.name: FontConfigTest2
113 * @tc.desc: test font file parser
114 * @tc.type:FUNC
115 */
116 HWTEST_F(FontParserTest, FontConfigTest2, TestSize.Level1)
117 {
118 FontConfigJson fontConfigJson;
119 EXPECT_EQ(fontConfigJson.ParseFontFileMap(), 0);
120 auto map = fontConfigJson.GetFontFileMap();
121 ASSERT_NE(map, nullptr);
122 EXPECT_EQ(map->size(), 281);
123 for (auto& it: *map) {
124 ASSERT_GT(it.second.size(), 3);
125 std::string end = it.second.substr(it.second.size() - 3, 3);
126 EXPECT_TRUE(end == "ttf" || end == "oft" || end == "ttc");
127 }
128 fontConfigJson.Dump();
129 }
130
131 /**
132 * @tc.name: OpenTypeBasicTypeTest1
133 * @tc.desc: opentype parser test
134 * @tc.type:FUNC
135 */
136 HWTEST_F(FontParserTest, OpenTypeBasicTypeTest1, TestSize.Level1)
137 {
138 char test[] = {'a', 'b', 'c', 'd', 0};
139 struct OpenTypeBasicType::Tag tag;
140 EXPECT_EQ(tag.Get(), "\0\0\0\0");
141 struct OpenTypeBasicType::Int16 int16;
142 EXPECT_EQ(int16.Get(), 0);
143 struct OpenTypeBasicType::Uint16 uint16;
144 EXPECT_EQ(uint16.Get(), 0);
145 struct OpenTypeBasicType::Int32 int32;
146 EXPECT_EQ(int32.Get(), 0);
147 struct OpenTypeBasicType::Uint32 uint32;
148 EXPECT_EQ(uint32.Get(), 0);
149 struct OpenTypeBasicType::Fixed fixed;
150 EXPECT_EQ(fixed.Get(), 0);
151 std::copy(test, test + sizeof(tag.tags), tag.tags);
152 EXPECT_EQ(tag.Get(), test);
153 }
154 } // namespace TextEngine
155 } // namespace Rosen
156 } // namespace OHOS
157