• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gmock/gmock.h>
19 
20 #include "font_config.h"
21 #include "font_parser.h"
22 #include "texgine/utils/exlog.h"
23 #include "cmap_table_parser.h"
24 #include "name_table_parser.h"
25 #include "post_table_parser.h"
26 #include "ranges.h"
27 
28 using namespace testing;
29 using namespace testing::ext;
30 
31 namespace OHOS {
32 namespace Rosen {
33 namespace TextEngine {
34 static const std::string FILE_NAME = "/system/fonts/visibility_list.json";
35 
36 class FontParserTest : public testing::Test {
37 };
38 
39 class MockCmapTableParser : public CmapTableParser {
40 public:
MockCmapTableParser()41     MockCmapTableParser() {}
42     MOCK_METHOD0(Dump, void());
43 };
44 
GetFontSet(const char * fname)45 std::vector<std::string> GetFontSet(const char* fname)
46 {
47     FontConfig fontConfig(fname);
48     return fontConfig.GetFontSet();
49 }
50 
ShowVisibilityFonts(std::vector<FontParser::FontDescriptor> & visibilityFonts)51 void ShowVisibilityFonts(std::vector<FontParser::FontDescriptor>& visibilityFonts)
52 {
53     for (auto &it : visibilityFonts) {
54         LOGSO_FUNC_LINE(INFO) << "\n fontFamily: " << it.fontFamily
55                               << "\n fontSubfamily: " << it.fontSubfamily
56                               << "\n fullName: " << it.fullName
57                               << "\n italic: " << it.italic
58                               << "\n monoSpace: " << it.monoSpace
59                               << "\n path: " << it.path
60                               << "\n postScriptName: " << it.postScriptName
61                               << "\n symbolic: " << it.symbolic
62                               << "\n weight: " << it.weight
63                               << "\n width: " << it.width;
64     }
65 }
66 
67 /**
68  * @tc.name: FontParserTest1
69  * @tc.desc: test get fontSet file parser
70  * @tc.type:FUNC
71  */
72 HWTEST_F(FontParserTest, FontParserTest1, TestSize.Level1)
73 {
74     auto fontSet1 = GetFontSet(nullptr);
75     EXPECT_EQ(fontSet1.size(), 0);
76 
77     std::ifstream fileStream(FILE_NAME.c_str());
78     if (fileStream.is_open()) {
79         auto fontSet2 = GetFontSet(FILE_NAME.c_str());
80         EXPECT_NE(fontSet2.size(), 0);
81         fileStream.close();
82     } else {
83         auto fontSet2 = GetFontSet(FILE_NAME.c_str());
84         EXPECT_EQ(fontSet2.size(), 0);
85     }
86 }
87 
88 /**
89  * @tc.name: FontParserTest2
90  * @tc.desc: test font file parser
91  * @tc.type:FUNC
92  */
93 HWTEST_F(FontParserTest, FontParserTest2, TestSize.Level1)
94 {
95     FontParser fontParser;
96     auto visibilityFonts = fontParser.GetVisibilityFonts();
97     fontParser.GetVisibilityFontByName("Noto Sans Regular");
98     std::ifstream fileStream(FILE_NAME.c_str());
99     if (fileStream.is_open()) {
100         EXPECT_NE(visibilityFonts.size(), 0);
101         ShowVisibilityFonts(visibilityFonts);
102         fileStream.close();
103     } else {
104         EXPECT_EQ(visibilityFonts.size(), 0);
105     }
106 }
107 
108 /**
109  * @tc.name: FontParserTest3
110  * @tc.desc: test font file parser
111  * @tc.type:FUNC
112  */
113 HWTEST_F(FontParserTest, FontParserTest3, TestSize.Level1)
114 {
115     FontParser fontParser;
116     std::unique_ptr<FontParser::FontDescriptor> font =
117         fontParser.GetVisibilityFontByName("Noto Sans Regular");
118 }
119 
120 /**
121  * @tc.name: FontConfigTest1
122  * @tc.desc: test font file parser
123  * @tc.type:FUNC
124  */
125 HWTEST_F(FontParserTest, FontConfigTest1, TestSize.Level1)
126 {
127     FontConfigJson fontConfigJson;
128     EXPECT_EQ(fontConfigJson.ParseFile(), 0);
129     fontConfigJson.Dump();
130 }
131 
132 /**
133  * @tc.name: FontConfigTest2
134  * @tc.desc: test font file parser
135  * @tc.type:FUNC
136  */
137 HWTEST_F(FontParserTest, FontConfigTest2, TestSize.Level1)
138 {
139     FontConfigJson fontConfigJson;
140     EXPECT_EQ(fontConfigJson.ParseFontFileMap(), 0);
141     fontConfigJson.Dump();
142 }
143 
144 /**
145  * @tc.name: CmapTableParserTest1
146  * @tc.desc: opentype parser test
147  * @tc.type:FUNC
148  */
149 HWTEST_F(FontParserTest, CmapTableParserTest1, TestSize.Level1)
150 {
151     MockCmapTableParser mockCmapTableParser;
152     CmapTableParser cmapTableParser_default;
153     CmapTableParser cmapTableParser("test data", 9);
154     struct NameRecord nameRecord;
155     struct NameTable nameTable;
156     nameRecord.encodingId = nameTable.count;
157     EXPECT_EQ(CmapTableParser::Parse(nullptr, 0), nullptr);
158     EXPECT_CALL(mockCmapTableParser, Dump()).Times(1);
159     mockCmapTableParser.Dump();
160 }
161 
162 /**
163  * @tc.name: NameTableParserTest1
164  * @tc.desc: opentype parser test
165  * @tc.type:FUNC
166  */
167 HWTEST_F(FontParserTest, NameTableParserTest1, TestSize.Level1)
168 {
169     NameTableParser nameTableParser(nullptr, 0);
170     struct NameRecord nameRecord;
171     struct NameTable nameTable;
172     nameRecord.encodingId = nameTable.count;
173     EXPECT_EQ(NameTableParser::Parse(nullptr, 0), nullptr);
174     nameTableParser.Dump();
175 }
176 
177 /**
178  * @tc.name: NameTableParserTest2
179  * @tc.desc: opentype parser test
180  * @tc.type:FUNC
181  */
182 HWTEST_F(FontParserTest, NameTableParserTest2, TestSize.Level1)
183 {
184     auto typeface = Drawing::Typeface::MakeDefault();
185     if (typeface == nullptr) {
186         LOGSO_FUNC_LINE(ERROR) << "typeface is nullptr";
187         return;
188     }
189     auto tag = HB_TAG('n', 'a', 'm', 'e');
190     auto size = typeface->GetTableSize(tag);
191     if (size <= 0) {
192         LOGSO_FUNC_LINE(ERROR) << "haven't name";
193         return ;
194     }
195     std::unique_ptr<char[]> tableData = nullptr;
196     tableData = std::make_unique<char[]>(size);
197     auto retTableData = typeface->GetTableData(tag, 0, size, tableData.get());
198     if (size != retTableData) {
199         LOGSO_FUNC_LINE(ERROR) << "get table data failed size:" << size << ",ret:" << retTableData;
200         return ;
201     }
202     hb_blob_t* hblob = nullptr;
203     hblob = hb_blob_create(
204             reinterpret_cast<const char *>(tableData.get()), size, HB_MEMORY_MODE_WRITABLE, tableData.get(), nullptr);
205     if (hblob == nullptr) {
206         LOGSO_FUNC_LINE(ERROR) << "hblob is nullptr";
207         return ;
208     }
209     const char* data_ = nullptr;
210     unsigned int length_ = 0;
211     data_ = hb_blob_get_data(hblob, nullptr);
212     length_ = hb_blob_get_length(hblob);
213     auto parseName = std::make_shared<NameTableParser>(data_, length_);
214     auto nameTable = parseName->Parse(data_, length_);
215     parseName->Dump();
216     hb_blob_destroy(hblob);
217     EXPECT_NE(nameTable, nullptr);
218 }
219 
220 /**
221  * @tc.name: PostTableParserTest1
222  * @tc.desc: opentype parser test
223  * @tc.type:FUNC
224  */
225 HWTEST_F(FontParserTest, PostTableParserTest1, TestSize.Level1)
226 {
227     PostTableParser postTableParser("test data", 9);
228     struct PostTable postTable;
229     postTable.underlinePosition = postTable.underlineThickness;
230     EXPECT_EQ(PostTableParser::Parse(nullptr, 0), nullptr);
231     postTableParser.Dump();
232 }
233 
234 /**
235  * @tc.name: OpenTypeBasicTypeTest1
236  * @tc.desc: opentype parser test
237  * @tc.type:FUNC
238  */
239 HWTEST_F(FontParserTest, OpenTypeBasicTypeTest1, TestSize.Level1)
240 {
241     char test[4] = {'a', 'b', 'c', 'd'};
242     struct OpenTypeBasicType::Tag tag;
243     tag.Get();
244     struct OpenTypeBasicType::Int16 int16;
245     int16.Get();
246     struct OpenTypeBasicType::Uint16 uint16;
247     struct OpenTypeBasicType::Int32 int32;
248     int32.Get();
249     struct OpenTypeBasicType::Uint32 uint32;
250     struct OpenTypeBasicType::Fixed fixed;
251     std::copy(std::begin(test), std::end(test), std::begin(tag.tags));
252     int16.data = (int16_t)uint16.data;
253     fixed.data.data = int32.data = (int32_t)uint32.data;
254 }
255 
256 /**
257  * @tc.name: RangesTest1
258  * @tc.desc: opentype parser test
259  * @tc.type:FUNC
260  */
261 HWTEST_F(FontParserTest, RangesTest1, TestSize.Level1)
262 {
263     Ranges ranges;
264     struct Ranges::Range range = { 0, 2, 1 };
265     ranges.AddRange(range);
266     struct Ranges::Range range2 = { 4, 5, 2 };
267     ranges.AddRange(range2);
268     EXPECT_EQ(ranges.GetGlyphId(3), Ranges::INVALID_GLYPH_ID);
269     EXPECT_EQ(ranges.GetGlyphId(0), 1);
270     EXPECT_EQ(ranges.GetGlyphId(4), 6);
271     ranges.Dump();
272 }
273 } // namespace TextEngine
274 } // namespace Rosen
275 } // namespace OHOS
276