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     ASSERT_NE(font, nullptr);
119     EXPECT_EQ(font->fontFamily, "Noto Sans");
120 }
121 
122 /**
123  * @tc.name: FontConfigTest1
124  * @tc.desc: test font file parser
125  * @tc.type:FUNC
126  */
127 HWTEST_F(FontParserTest, FontConfigTest1, TestSize.Level1)
128 {
129     FontConfigJson fontConfigJson;
130     EXPECT_EQ(fontConfigJson.ParseFile(), 0);
131     fontConfigJson.Dump();
132 }
133 
134 /**
135  * @tc.name: FontConfigTest2
136  * @tc.desc: test font file parser
137  * @tc.type:FUNC
138  */
139 HWTEST_F(FontParserTest, FontConfigTest2, TestSize.Level1)
140 {
141     FontConfigJson fontConfigJson;
142     EXPECT_EQ(fontConfigJson.ParseFontFileMap(), 0);
143     fontConfigJson.Dump();
144 }
145 
146 /**
147  * @tc.name: CmapTableParserTest1
148  * @tc.desc: opentype parser test
149  * @tc.type:FUNC
150  */
151 HWTEST_F(FontParserTest, CmapTableParserTest1, TestSize.Level1)
152 {
153     MockCmapTableParser mockCmapTableParser;
154     CmapTableParser cmapTableParser_default;
155     CmapTableParser cmapTableParser("test data", 9);
156     struct NameRecord nameRecord;
157     struct NameTable nameTable;
158     nameRecord.encodingId = nameTable.count;
159     EXPECT_EQ(CmapTableParser::Parse(nullptr, 0), nullptr);
160     EXPECT_CALL(mockCmapTableParser, Dump()).Times(1);
161     mockCmapTableParser.Dump();
162 }
163 
164 /**
165  * @tc.name: NameTableParserTest1
166  * @tc.desc: opentype parser test
167  * @tc.type:FUNC
168  */
169 HWTEST_F(FontParserTest, NameTableParserTest1, TestSize.Level1)
170 {
171     NameTableParser nameTableParser(nullptr, 0);
172     struct NameRecord nameRecord;
173     struct NameTable nameTable;
174     nameRecord.encodingId = nameTable.count;
175     EXPECT_EQ(NameTableParser::Parse(nullptr, 0), nullptr);
176     nameTableParser.Dump();
177 }
178 
179 /**
180  * @tc.name: NameTableParserTest2
181  * @tc.desc: opentype parser test
182  * @tc.type:FUNC
183  */
184 HWTEST_F(FontParserTest, NameTableParserTest2, TestSize.Level1)
185 {
186     auto typeface = Drawing::Typeface::MakeDefault();
187     if (typeface == nullptr) {
188         LOGSO_FUNC_LINE(ERROR) << "typeface is nullptr";
189         return;
190     }
191     auto tag = HB_TAG('n', 'a', 'm', 'e');
192     auto size = typeface->GetTableSize(tag);
193     if (size <= 0) {
194         LOGSO_FUNC_LINE(ERROR) << "haven't name";
195         return ;
196     }
197     std::unique_ptr<char[]> tableData = nullptr;
198     tableData = std::make_unique<char[]>(size);
199     auto retTableData = typeface->GetTableData(tag, 0, size, tableData.get());
200     if (size != retTableData) {
201         LOGSO_FUNC_LINE(ERROR) << "get table data failed size:" << size << ",ret:" << retTableData;
202         return ;
203     }
204     hb_blob_t* hblob = nullptr;
205     hblob = hb_blob_create(
206             reinterpret_cast<const char *>(tableData.get()), size, HB_MEMORY_MODE_WRITABLE, tableData.get(), nullptr);
207     if (hblob == nullptr) {
208         LOGSO_FUNC_LINE(ERROR) << "hblob is nullptr";
209         return ;
210     }
211     const char* data_ = nullptr;
212     unsigned int length_ = 0;
213     data_ = hb_blob_get_data(hblob, nullptr);
214     length_ = hb_blob_get_length(hblob);
215     auto parseName = std::make_shared<NameTableParser>(data_, length_);
216     auto nameTable = parseName->Parse(data_, length_);
217     parseName->Dump();
218     hb_blob_destroy(hblob);
219     EXPECT_NE(nameTable, nullptr);
220 }
221 
222 /**
223  * @tc.name: PostTableParserTest1
224  * @tc.desc: opentype parser test
225  * @tc.type:FUNC
226  */
227 HWTEST_F(FontParserTest, PostTableParserTest1, TestSize.Level1)
228 {
229     PostTableParser postTableParser("test data", 9);
230     struct PostTable postTable;
231     postTable.underlinePosition = postTable.underlineThickness;
232     EXPECT_EQ(PostTableParser::Parse(nullptr, 0), nullptr);
233     postTableParser.Dump();
234 }
235 
236 /**
237  * @tc.name: OpenTypeBasicTypeTest1
238  * @tc.desc: opentype parser test
239  * @tc.type:FUNC
240  */
241 HWTEST_F(FontParserTest, OpenTypeBasicTypeTest1, TestSize.Level1)
242 {
243     char test[] = {'a', 'b', 'c', 'd', 0};
244     struct OpenTypeBasicType::Tag tag;
245     EXPECT_EQ(tag.Get(), "\0\0\0\0");
246     struct OpenTypeBasicType::Int16 int16;
247     EXPECT_EQ(int16.Get(), 0);
248     struct OpenTypeBasicType::Uint16 uint16;
249     EXPECT_EQ(uint16.Get(), 0);
250     struct OpenTypeBasicType::Int32 int32;
251     EXPECT_EQ(int32.Get(), 0);
252     struct OpenTypeBasicType::Uint32 uint32;
253     EXPECT_EQ(uint32.Get(), 0);
254     struct OpenTypeBasicType::Fixed fixed;
255     EXPECT_EQ(fixed.Get(), 0);
256     std::copy(std::begin(test), std::begin(test) + 4, std::begin(tag.tags));
257     EXPECT_EQ(tag.Get(), test);
258 }
259 
260 /**
261  * @tc.name: RangesTest1
262  * @tc.desc: opentype parser test
263  * @tc.type:FUNC
264  */
265 HWTEST_F(FontParserTest, RangesTest1, TestSize.Level1)
266 {
267     Ranges ranges;
268     struct Ranges::Range range = { 0, 2, 1 };
269     ranges.AddRange(range);
270     struct Ranges::Range range2 = { 4, 5, 2 };
271     ranges.AddRange(range2);
272     EXPECT_EQ(ranges.GetGlyphId(3), Ranges::INVALID_GLYPH_ID);
273     EXPECT_EQ(ranges.GetGlyphId(0), 1);
274     EXPECT_EQ(ranges.GetGlyphId(4), 6);
275     ranges.Dump();
276 }
277 } // namespace TextEngine
278 } // namespace Rosen
279 } // namespace OHOS
280