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 <fcntl.h>
17 #include <fstream>
18 #include <iostream>
19 #include "gtest/gtest.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkStream.h"
22 #include "include/core/SkTypeface.h"
23 #include "SkFontMgr.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 class SkFontMgrOhosTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 private:
35 static sk_sp<SkFontMgr> m_fontMgrOhosImpl;
36 };
37
38 sk_sp<SkFontMgr> SkFontMgrOhosTest::m_fontMgrOhosImpl = nullptr;
39
SetUpTestCase()40 void SkFontMgrOhosTest::SetUpTestCase()
41 {
42 m_fontMgrOhosImpl = SkFontMgr::RefDefault();
43 }
TearDownTestCase()44 void SkFontMgrOhosTest::TearDownTestCase()
45 {
46 m_fontMgrOhosImpl = nullptr;
47 }
SetUp()48 void SkFontMgrOhosTest::SetUp() {}
TearDown()49 void SkFontMgrOhosTest::TearDown() {}
50
51 const std::string BASE_NAME = {0x48, 0x61, 0x72, 0x6D, 0x6F, 0x6E, 0x79, 0x4F, 0x53};
52
53 const char TTF_FILE_PATH[] = {0x2F, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2F, 0x66, 0x6F, 0x6E, 0x74, 0x73,
54 0x2F, 0x48, 0x61, 0x72, 0x6D, 0x6F, 0x6E, 0x79, 0x4F, 0x53, 0x5F, 0x53, 0x61, 0x6E, 0x73, 0x5F,
55 0x53, 0x43, 0x2E, 0x74, 0x74, 0x66, 0x00};
56
57 /**
58 * @tc.name: GetFontFullName001
59 * @tc.desc: Test GetFontFullName
60 * @tc.type: FUNC
61 */
62 HWTEST_F(SkFontMgrOhosTest, GetFontFullName001, TestSize.Level1)
63 {
64 int fd = open(TTF_FILE_PATH, O_RDONLY);
65 ASSERT_NE(fd, -1);
66 std::vector<SkByteArray> fullnameVec;
67 int ret = m_fontMgrOhosImpl->GetFontFullName(fd, fullnameVec);
68 close(fd);
69 EXPECT_EQ(ret, SUCCESSED);
70 std::string testRes = BASE_NAME + " Sans SC";
71 ASSERT_EQ(fullnameVec.size(), 1);
72 ASSERT_NE(fullnameVec[0].strData, nullptr);
73 ASSERT_EQ(fullnameVec[0].strLen, testRes.size() * 2);
74 }
75
76 /**
77 * @tc.name: CountFamilies001
78 * @tc.desc: Test CountFamilies
79 * @tc.type: FUNC
80 */
81 HWTEST_F(SkFontMgrOhosTest, CountFamilies001, TestSize.Level1)
82 {
83 int count = m_fontMgrOhosImpl->countFamilies();
84 ASSERT_EQ(count, 9);
85 }
86
87 /**
88 * @tc.name: GetFamilyName001
89 * @tc.desc: Test GetFamilyName
90 * @tc.type: FUNC
91 */
92 HWTEST_F(SkFontMgrOhosTest, GetFamilyName001, TestSize.Level1)
93 {
94 SkString str;
95 m_fontMgrOhosImpl->getFamilyName(0, &str);
96 std::string testRes1 = BASE_NAME + "-Sans";
97 EXPECT_STREQ(str.c_str(), testRes1.c_str());
98 m_fontMgrOhosImpl->getFamilyName(5, &str);
99 std::string testRes2 = BASE_NAME + "-Sans-Condensed";
100 EXPECT_STREQ(str.c_str(), testRes2.c_str());
101 m_fontMgrOhosImpl->getFamilyName(6, &str);
102 std::string testRes3 = BASE_NAME + "-Sans-Digit";
103 EXPECT_STREQ(str.c_str(), testRes3.c_str());
104 m_fontMgrOhosImpl->getFamilyName(7, &str);
105 EXPECT_STREQ(str.c_str(), "serif");
106 m_fontMgrOhosImpl->getFamilyName(8, &str);
107 EXPECT_STREQ(str.c_str(), "monospace");
108 }
109
110 /**
111 * @tc.name: CreateStyleSet001
112 * @tc.desc: Test CreateStyleSet
113 * @tc.type: FUNC
114 */
115 HWTEST_F(SkFontMgrOhosTest, CreateStyleSet001, TestSize.Level1)
116 {
117 SkFontStyleSet* styleSet = m_fontMgrOhosImpl->createStyleSet(9);
118 ASSERT_NE(styleSet, nullptr);
119 ASSERT_EQ(styleSet->count(), 0);
120 delete styleSet;
121 styleSet = nullptr;
122
123 styleSet = m_fontMgrOhosImpl->createStyleSet(0);
124 ASSERT_NE(styleSet, nullptr);
125 ASSERT_EQ(styleSet->count(), 2);
126 SkString styleName;
127 SkFontStyle style;
128 styleSet->getStyle(0, &style, &styleName);
129 EXPECT_STREQ(styleName.c_str(), "normal");
130 EXPECT_EQ(style.weight(), SkFontStyle::kNormal_Weight);
131 EXPECT_EQ(style.width(), SkFontStyle::kNormal_Width);
132 EXPECT_EQ(style.slant(), SkFontStyle::kUpright_Slant);
133
134 styleSet->getStyle(1, &style, &styleName);
135 EXPECT_STREQ(styleName.c_str(), "normal");
136 EXPECT_EQ(style.weight(), SkFontStyle::kNormal_Weight);
137 EXPECT_EQ(style.width(), SkFontStyle::kNormal_Width);
138 EXPECT_EQ(style.slant(), SkFontStyle::kItalic_Slant);
139
140 delete styleSet;
141 }
142
143 /**
144 * @tc.name: MatchFamily001
145 * @tc.desc: Test MatchFamily
146 * @tc.type: FUNC
147 */
148 HWTEST_F(SkFontMgrOhosTest, MatchFamily001, TestSize.Level1)
149 {
150 std::string testRes1 = BASE_NAME + " Sans";
151 SkFontStyleSet* styleSet1 = m_fontMgrOhosImpl->matchFamily(testRes1.c_str());
152 ASSERT_NE(styleSet1, nullptr);
153 ASSERT_EQ(styleSet1->count(), 0);
154
155 std::string testRes2 = BASE_NAME + "-Sans";
156 SkFontStyleSet* styleSet2 = m_fontMgrOhosImpl->matchFamily(testRes2.c_str());
157 ASSERT_NE(styleSet2, nullptr);
158 ASSERT_EQ(styleSet2->count(), 2);
159
160 std::string testRes3 = BASE_NAME + " Sans SC";
161 SkFontStyleSet* styleSet3 = m_fontMgrOhosImpl->matchFamily(testRes3.c_str());
162 ASSERT_NE(styleSet3, nullptr);
163 ASSERT_EQ(styleSet3->count(), 1);
164
165 delete styleSet1;
166 delete styleSet2;
167 delete styleSet3;
168 }
169
170 /**
171 * @tc.name: MatchFamilyStyle001
172 * @tc.desc: Test MatchFamilyStyle
173 * @tc.type: FUNC
174 */
175 HWTEST_F(SkFontMgrOhosTest, MatchFamilyStyle001, TestSize.Level1)
176 {
177 SkFontStyle style;
178 SkTypeface* tp = m_fontMgrOhosImpl->matchFamilyStyle(nullptr, style);
179 ASSERT_NE(tp, nullptr);
180 SkString familyName;
181 tp->getFamilyName(&familyName);
182 std::string testRes = BASE_NAME + "-Sans";
183 ASSERT_STREQ(familyName.c_str(), testRes.c_str());
184 SkSafeUnref(tp);
185
186 std::string inputName1 = BASE_NAME + "-Sans";
187 SkTypeface* tp1 = m_fontMgrOhosImpl->matchFamilyStyle(inputName1.c_str(), style);
188 ASSERT_NE(tp1, nullptr);
189 SkString familyName1;
190 tp1->getFamilyName(&familyName1);
191 std::string testRes1 = BASE_NAME + "-Sans";
192 ASSERT_STREQ(familyName1.c_str(), testRes1.c_str());
193 SkSafeUnref(tp1);
194
195 SkTypeface* tp2 = m_fontMgrOhosImpl->matchFamilyStyle("TestNoFound", style);
196 ASSERT_EQ(tp2, nullptr);
197
198 std::string inputName3 = BASE_NAME + " Sans SC";
199 SkTypeface* tp3 = m_fontMgrOhosImpl->matchFamilyStyle(inputName3.c_str(), style);
200 ASSERT_NE(tp3, nullptr);
201 SkString familyName3;
202 tp3->getFamilyName(&familyName3);
203 std::string testRes3 = BASE_NAME + " Sans SC";
204 ASSERT_STREQ(familyName3.c_str(), testRes3.c_str());
205 SkSafeUnref(tp3);
206 }
207
208 /**
209 * @tc.name: MatchFamilyStyleCharacter001
210 * @tc.desc: Test MatchFamilyStyleCharacter
211 * @tc.type: FUNC
212 */
213 HWTEST_F(SkFontMgrOhosTest, MatchFamilyStyleCharacter001, TestSize.Level1)
214 {
215 SkFontStyle style;
216 SkTypeface* tp = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, nullptr, 0, 0x0626);
217 ASSERT_NE(tp, nullptr);
218 SkString familyName;
219 tp->getFamilyName(&familyName);
220 std::string testRes1 = BASE_NAME + " Sans Naskh Arabic UI";
221 ASSERT_STREQ(familyName.c_str(), testRes1.c_str());
222
223 std::vector<const char*> bcp47;
224 SkTypeface* tp1 = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, bcp47.data(), bcp47.size(), 0x63CF);
225 ASSERT_NE(tp1, nullptr);
226 tp1->getFamilyName(&familyName);
227 std::string testRes2 = BASE_NAME + " Sans SC";
228 ASSERT_STREQ(familyName.c_str(), testRes2.c_str());
229
230 bcp47.push_back("zh-Hant");
231 SkTypeface* tp2 = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, bcp47.data(), bcp47.size(), 0x63CF);
232 ASSERT_NE(tp2, nullptr);
233 tp2->getFamilyName(&familyName);
234 std::string testRes3 = BASE_NAME + " Sans TC";
235 ASSERT_STREQ(familyName.c_str(), testRes3.c_str());
236
237 SkTypeface* tp3 = m_fontMgrOhosImpl->matchFamilyStyleCharacter(nullptr, style, bcp47.data(), bcp47.size(), 0x1F600);
238 ASSERT_NE(tp3, nullptr);
239 tp3->getFamilyName(&familyName);
240 ASSERT_STREQ(familyName.c_str(), "HMOS Color Emoji");
241
242 SkSafeUnref(tp);
243 SkSafeUnref(tp1);
244 SkSafeUnref(tp2);
245 SkSafeUnref(tp3);
246 }
247
248 /**
249 * @tc.name: MakeFromStreamIndex001
250 * @tc.desc: Test MakeFromStreamIndex
251 * @tc.type: FUNC
252 */
253 HWTEST_F(SkFontMgrOhosTest, MakeFromStreamIndex001, TestSize.Level1)
254 {
255 std::unique_ptr<SkStreamAsset> skStream = SkStreamAsset::MakeFromFile(TTF_FILE_PATH);
256 sk_sp<SkTypeface> sktp = m_fontMgrOhosImpl->makeFromStream(std::move(skStream), 0);
257 SkString familyName;
258 sktp->getFamilyName(&familyName);
259 std::string testRes = BASE_NAME + " Sans SC";
260 ASSERT_STREQ(familyName.c_str(), testRes.c_str());
261 }
262
263 /**
264 * @tc.name: MakeFromStreamArgs001
265 * @tc.desc: Test MakeFromStreamArgs
266 * @tc.type: FUNC
267 */
268 HWTEST_F(SkFontMgrOhosTest, MakeFromStreamArgs001, TestSize.Level1)
269 {
270 std::unique_ptr<SkStreamAsset> skStream = SkStreamAsset::MakeFromFile(TTF_FILE_PATH);
271 SkFontArguments skFontArguments;
272 sk_sp<SkTypeface> sktp = m_fontMgrOhosImpl->makeFromStream(std::move(skStream), skFontArguments);
273 SkString familyName;
274 sktp->getFamilyName(&familyName);
275 std::string testRes = BASE_NAME + " Sans SC";
276 ASSERT_STREQ(familyName.c_str(), testRes.c_str());
277 }
278
279 /**
280 * @tc.name: MakeFromFile001
281 * @tc.desc: Test MakeFromFile
282 * @tc.type: FUNC
283 */
284 HWTEST_F(SkFontMgrOhosTest, MakeFromFile001, TestSize.Level1)
285 {
286 sk_sp<SkTypeface> sktp = m_fontMgrOhosImpl->makeFromFile(TTF_FILE_PATH, 0);
287 ASSERT_NE(sktp, nullptr);
288 SkString familyName;
289 sktp->getFamilyName(&familyName);
290 std::string testRes = BASE_NAME + " Sans SC";
291 ASSERT_STREQ(familyName.c_str(), testRes.c_str());
292 }
293
294 /**
295 * @tc.name: LegacyMakeTypeface001
296 * @tc.desc: Test LegacyMakeTypeface
297 * @tc.type: FUNC
298 */
299 HWTEST_F(SkFontMgrOhosTest, LegacyMakeTypeface001, TestSize.Level1)
300 {
301 SkFontStyle style;
302 sk_sp<SkTypeface> sktp = m_fontMgrOhosImpl->legacyMakeTypeface("TestNoFound", style);
303 ASSERT_NE(sktp, nullptr);
304 SkString familyName;
305 sktp->getFamilyName(&familyName);
306 std::string testRes1 = BASE_NAME + "-Sans";
307 ASSERT_STREQ(familyName.c_str(), testRes1.c_str());
308
309 std::string testRes2 = BASE_NAME + "-Sans-Condensed";
310 sk_sp<SkTypeface> sktp1 = m_fontMgrOhosImpl->legacyMakeTypeface(testRes2.c_str(), style);
311 ASSERT_NE(sktp1, nullptr);
312 sktp1->getFamilyName(&familyName);
313 std::string testRes3 = BASE_NAME + "-Sans-Condensed";
314 ASSERT_STREQ(familyName.c_str(), testRes3.c_str());
315 }