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_config_test.h"
18 #define private public
19 #define protected public
20 #include "font_config.h"
21 #include "font_manager.h"
22 #undef private
23 #undef protected
24 #include "file_utils.h"
25 #include <string>
26 #include <vector>
27
28 namespace {
29 const std::string INSTALL_PATH = "/data/service/el1/public/for-all-app/fonts/";
30 const std::string TEMP_PATH = "/data/service/el1/public/for-all-app/fonts/temp/";
31 const std::string FONT_CONFIG_FILE = INSTALL_PATH + "install_fontconfig.json";
32 }
33
34 using testing::ext::TestSize;
35 using namespace std;
36
37 namespace OHOS {
38 namespace Global {
39 namespace FontManager {
40
41 class FontConfigTest : public testing::Test {
42 public:
FontConfigTest()43 FontConfigTest() : config_(FONT_CONFIG_FILE)
44 {}
45 static void SetUpTestCase(void);
46 static void TearDownTestCase(void);
47 void SetUp();
48 void TearDown();
49
50 protected:
51 FontConfig config_;
52 FontManager fontManager_;
53 };
54
SetUpTestCase(void)55 void FontConfigTest::SetUpTestCase(void)
56 {
57 FileUtils::RemoveFile(FONT_CONFIG_FILE);
58 }
59
TearDownTestCase(void)60 void FontConfigTest::TearDownTestCase(void)
61 {
62 FileUtils::RemoveFile(FONT_CONFIG_FILE);
63 }
64
SetUp(void)65 void FontConfigTest::SetUp(void)
66 {
67 FileUtils::RemoveFile(FONT_CONFIG_FILE);
68 }
69
TearDown(void)70 void FontConfigTest::TearDown(void)
71 {
72 FileUtils::RemoveFile(FONT_CONFIG_FILE);
73 }
74
75 /**
76 * @tc.name: FontConfigFuncTest001
77 * @tc.desc: Test FontConfig TTF InsertFontRecord case
78 * @tc.type: FUNC
79 */
80 HWTEST_F(FontConfigTest, FontConfigFuncTest001, TestSize.Level1)
81 {
82 ASSERT_EQ(fontManager_.CheckFontConfigPath(), true);
83 std::string fontFullPath = INSTALL_PATH + "HarmonyOS_Sans.ttf";
84 std::vector<std::string> fullName{"HarmonyOS-Sans"};
85
86 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath, fullName), true);
87
88 auto t = this->config_.GetFontsMap(this->config_.ConfigPath_);
89 EXPECT_EQ(t.find(fontFullPath) == t.end(), false);
90 }
91
92 /**
93 * @tc.name: FontConfigFuncTest002
94 * @tc.desc: Test FontConfig TTF DeleteFontRecord case
95 * @tc.type: FUNC
96 */
97 HWTEST_F(FontConfigTest, FontConfigFuncTest002, TestSize.Level1)
98 {
99 ASSERT_EQ(fontManager_.CheckFontConfigPath(), true);
100 std::string fontFullPath = INSTALL_PATH + "HarmonyOS_Sans.ttf";
101 std::vector<std::string> fullName{"HarmonyOS-Sans"};
102
103 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath, fullName), true);
104
105 auto t = this->config_.GetFontsMap(this->config_.ConfigPath_);
106
107 EXPECT_EQ(t.find(fontFullPath) == t.end(), false);
108
109 EXPECT_EQ(this->config_.DeleteFontRecord(fontFullPath), true);
110
111 t = this->config_.GetFontsMap(this->config_.ConfigPath_);
112
113 EXPECT_EQ(t.find(fontFullPath) == t.end(), true);
114 }
115
116 /**
117 * @tc.name: FontConfigFuncTest003
118 * @tc.desc: Test FontConfig TTF GetInstalledFontsNum case
119 * @tc.type: FUNC
120 */
121 HWTEST_F(FontConfigTest, FontConfigFuncTest003, TestSize.Level1)
122 {
123 ASSERT_EQ(fontManager_.CheckFontConfigPath(), true);
124 std::string fontFullPath1 = INSTALL_PATH + "HarmonyOS_Sans1.ttf";
125 std::vector<std::string> fullName1{"HarmonyOS-Sans1"};
126 std::string fontFullPath2 = INSTALL_PATH + "HarmonyOS_Sans2.ttf";
127 std::vector<std::string> fullName2{"HarmonyOS-Sans2"};
128 std::string fontFullPath3 = INSTALL_PATH + "HarmonyOS_Sans3.ttf";
129 std::vector<std::string> fullName3{"HarmonyOS-Sans3"};
130 std::string fontFullPath4 = INSTALL_PATH + "HarmonyOS_Sans4.ttf";
131 std::vector<std::string> fullName4{"HarmonyOS-Sans4"};
132
133 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath1, fullName1), true);
134 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath2, fullName2), true);
135 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath3, fullName3), true);
136 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath4, fullName4), true);
137
138 EXPECT_EQ(this->config_.GetInstalledFontsNum(), 4);
139 }
140
141 /**
142 * @tc.name: FontConfigFuncTest004
143 * @tc.desc: Test FontConfig TTF getFontFileByName case
144 * @tc.type: FUNC
145 */
146 HWTEST_F(FontConfigTest, FontConfigFuncTest004, TestSize.Level1)
147 {
148 ASSERT_EQ(fontManager_.CheckFontConfigPath(), true);
149 std::string fontFullPath = INSTALL_PATH + "HarmonyOS_Sans.ttf";
150 std::vector<std::string> fullName{"HarmonyOS-Sans"};
151
152 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath, fullName), true);
153
154 EXPECT_EQ(this->config_.GetFontFileByName("HarmonyOS-Sans"), fontFullPath);
155 EXPECT_EQ(this->config_.GetFontFileByName("ERROR"), "");
156 }
157
158 /**
159 * @tc.name: FontConfigFuncTest005
160 * @tc.desc: Test FontConfig TTC InsertFontRecord case
161 * @tc.type: FUNC
162 */
163 HWTEST_F(FontConfigTest, FontConfigFuncTest005, TestSize.Level1)
164 {
165 ASSERT_EQ(fontManager_.CheckFontConfigPath(), true);
166 std::string fontFullPath = INSTALL_PATH + "NotoSansCJK-Regular.ttc";
167 std::vector<std::string> fullName{
168 "Noto Sans CJK JP", "Noto Sans CJK KR", "Noto Sans CJK SC", "Noto Sans CJK TC",
169 "Noto Sans CJK HK", "Noto Sans Mono CJK JP", "Noto Sans Mono CJK KR",
170 "Noto Sans Mono CJK SC", "Noto Sans Mono CJK TC", "Noto Sans Mono CJK HK"};
171
172 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath, fullName), true);
173
174 auto t = this->config_.GetFontsMap(this->config_.ConfigPath_);
175 EXPECT_EQ(t.find(fontFullPath) == t.end(), false);
176 }
177
178 /**
179 * @tc.name: FontConfigFuncTest006
180 * @tc.desc: Test FontConfig TTC DeleteFontRecord case
181 * @tc.type: FUNC
182 */
183 HWTEST_F(FontConfigTest, FontConfigFuncTest006, TestSize.Level1)
184 {
185 ASSERT_EQ(fontManager_.CheckFontConfigPath(), true);
186 std::string fontFullPath = INSTALL_PATH + "NotoSansCJK-Regular.ttc";
187 std::vector<std::string> fullName{
188 "Noto Sans CJK JP", "Noto Sans CJK KR", "Noto Sans CJK SC", "Noto Sans CJK TC",
189 "Noto Sans CJK HK", "Noto Sans Mono CJK JP", "Noto Sans Mono CJK KR",
190 "Noto Sans Mono CJK SC", "Noto Sans Mono CJK TC", "Noto Sans Mono CJK HK"};
191
192 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath, fullName), true);
193
194 auto t = this->config_.GetFontsMap(this->config_.ConfigPath_);
195
196 EXPECT_EQ(t.find(fontFullPath) == t.end(), false);
197
198 EXPECT_EQ(this->config_.DeleteFontRecord(fontFullPath), true);
199
200 t = this->config_.GetFontsMap(this->config_.ConfigPath_);
201
202 EXPECT_EQ(t.find(fontFullPath) == t.end(), true);
203 }
204
205 /**
206 * @tc.name: FontConfigFuncTest007
207 * @tc.desc: Test FontConfig TTC GetInstalledFontsNum case
208 * @tc.type: FUNC
209 */
210 HWTEST_F(FontConfigTest, FontConfigFuncTest007, TestSize.Level1)
211 {
212 ASSERT_EQ(fontManager_.CheckFontConfigPath(), true);
213 std::string fontFullPath1 = INSTALL_PATH + "NotoSansCJK-Regular1.ttc";
214 std::vector<std::string> fullName1{"Noto Serif CJK JP",
215 "Noto Serif CJK KR", "Noto Serif CJK SC", "Noto Serif CJK TC", "Noto Serif CJK HK"};
216 std::string fontFullPath2 = INSTALL_PATH + "NotoSansCJK-Regular2.ttc";
217 std::vector<std::string> fullName2{"Noto Serif CJK JP",
218 "Noto Serif CJK KR", "Noto Serif CJK SC", "Noto Serif CJK TC", "Noto Serif CJK HK"};
219 std::string fontFullPath3 = INSTALL_PATH + "NotoSansCJK-Regular3.ttc";
220 std::vector<std::string> fullName3{"Noto Serif CJK JP",
221 "Noto Serif CJK KR", "Noto Serif CJK SC", "Noto Serif CJK TC", "Noto Serif CJK HK"};
222 std::string fontFullPath4 = INSTALL_PATH + "NotoSansCJK-Regular4.ttc";
223 std::vector<std::string> fullName4{"Noto Serif CJK JP",
224 "Noto Serif CJK KR", "Noto Serif CJK SC", "Noto Serif CJK TC", "Noto Serif CJK HK"};
225
226 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath1, fullName1), true);
227 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath2, fullName2), true);
228 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath3, fullName3), true);
229 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath4, fullName4), true);
230
231 EXPECT_EQ(this->config_.GetInstalledFontsNum(), 4);
232 }
233
234 /**
235 * @tc.name: FontConfigFuncTest008
236 * @tc.desc: Test FontConfig TTC getFontFileByName case
237 * @tc.type: FUNC
238 */
239 HWTEST_F(FontConfigTest, FontConfigFuncTest008, TestSize.Level1)
240 {
241 ASSERT_EQ(fontManager_.CheckFontConfigPath(), true);
242 std::string fontFullPath = INSTALL_PATH + "NotoSansCJK-Regular.ttc";
243 std::vector<std::string> fullName{
244 "Noto Serif CJK JP", "Noto Serif CJK KR", "Noto Serif CJK SC", "Noto Serif CJK TC", "Noto Serif CJK HK"};
245
246 EXPECT_EQ(this->config_.InsertFontRecord(fontFullPath, fullName), true);
247
248 EXPECT_EQ(this->config_.GetFontFileByName("Noto Serif CJK SC"), fontFullPath);
249 EXPECT_EQ(this->config_.GetFontFileByName("ERROR"), "");
250 }
251
252 } // namespace FontManager
253 } // namespace Global
254 } // namespace OHOS
255