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, Hardware 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 "drawing_font_mgr.h" 18 #include "drawing_text_typography.h" 19 #include "drawing_typeface.h" 20 21 using namespace testing; 22 using namespace testing::ext; 23 24 namespace OHOS { 25 namespace Rosen { 26 namespace Drawing { 27 class OH_Drawing_FontMgrTest : public testing::Test { 28 }; 29 30 /* 31 * @tc.name: OH_Drawing_FontMgrTest001 32 * @tc.desc: test for creating and destroying font manager. 33 * @tc.type: FUNC 34 */ 35 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest001, TestSize.Level1) 36 { 37 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 38 EXPECT_NE(mgr, nullptr); 39 OH_Drawing_FontMgrDestroy(mgr); 40 OH_Drawing_FontMgrDestroy(nullptr); 41 } 42 43 /* 44 * @tc.name: OH_Drawing_FontMgrTest002 45 * @tc.desc: test for getting family name. 46 * @tc.type: FUNC 47 */ 48 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest002, TestSize.Level1) 49 { 50 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 51 EXPECT_NE(mgr, nullptr); 52 int count = OH_Drawing_FontMgrGetFamilyCount(mgr); 53 EXPECT_EQ(count, 9); 54 55 char *familyName = OH_Drawing_FontMgrGetFamilyName(mgr, 0); 56 std::string result = { 0x48, 0x61, 0x72, 0x6d, 0x6f, 0x6e, 0x79, 0x4f, 0x53, 0x2d, 0x53, 0x61, 0x6e, 0x73 }; 57 EXPECT_EQ(std::string(familyName), result); 58 OH_Drawing_FontMgrDestroyFamilyName(familyName); 59 60 OH_Drawing_FontMgrDestroy(mgr); 61 } 62 63 /* 64 * @tc.name: OH_Drawing_FontMgrTest003 65 * @tc.desc: test for creating and destroying font style set by font mannager. 66 * @tc.type: FUNC 67 */ 68 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest003, TestSize.Level1) 69 { 70 OH_Drawing_FontMgr* mgr = OH_Drawing_FontMgrCreate(); 71 EXPECT_NE(mgr, nullptr); 72 73 OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0); 74 OH_Drawing_FontMgrCreateFontStyleSet(reinterpret_cast<OH_Drawing_FontMgr*>(1), 0); 75 EXPECT_NE(fontStyleSet, nullptr); 76 OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet); 77 OH_Drawing_FontMgrDestroyFontStyleSet(nullptr); 78 79 OH_Drawing_FontMgrDestroy(mgr); 80 } 81 82 /* 83 * @tc.name: OH_Drawing_FontMgrTest004 84 * @tc.desc: test for matching font family by family name. 85 * @tc.type: FUNC 86 */ 87 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest004, TestSize.Level1) 88 { 89 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 90 EXPECT_NE(mgr, nullptr); 91 const char* matchFamilyName = "HarmonyOS-Sans"; 92 OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrMatchFamily(mgr, matchFamilyName); 93 EXPECT_NE(fontStyleSet, nullptr); 94 EXPECT_EQ(OH_Drawing_FontStyleSetCount(fontStyleSet), 2); 95 OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet); 96 97 OH_Drawing_FontMgrDestroy(mgr); 98 } 99 100 101 /* 102 * @tc.name: OH_Drawing_FontMgrTest005 103 * @tc.desc: test for matching font typeface by family name and font style. 104 * @tc.type: FUNC 105 */ 106 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest005, TestSize.Level1) 107 { 108 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 109 EXPECT_NE(mgr, nullptr); 110 const char* matchFamilyName = "HarmonyOS-Sans"; 111 OH_Drawing_FontStyleStruct normalStyle; 112 normalStyle.weight = FONT_WEIGHT_400; 113 normalStyle.width = FONT_WIDTH_NORMAL; 114 normalStyle.slant = FONT_STYLE_NORMAL; 115 OH_Drawing_Typeface *typeface = OH_Drawing_FontMgrMatchFamilyStyle(mgr, matchFamilyName, normalStyle); 116 EXPECT_NE(typeface, nullptr); 117 OH_Drawing_TypefaceDestroy(typeface); 118 119 OH_Drawing_FontMgrDestroy(mgr); 120 } 121 122 /* 123 * @tc.name: OH_Drawing_FontMgrTest006 124 * @tc.desc: test for matching font typeface by family name, font style and specific character. 125 * @tc.type: FUNC 126 */ 127 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest006, TestSize.Level1) 128 { 129 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 130 EXPECT_NE(mgr, nullptr); 131 132 const char* matchFamilyName = "HarmonyOS-Sans"; 133 OH_Drawing_FontStyleStruct normalStyle; 134 normalStyle.weight = FONT_WEIGHT_400; 135 normalStyle.width = FONT_WIDTH_NORMAL; 136 normalStyle.slant = FONT_STYLE_NORMAL; 137 138 const char *bcp47[] = {"zh-Hans", "zh-CN"}; 139 OH_Drawing_Typeface *typeface = OH_Drawing_FontMgrMatchFamilyStyleCharacter(mgr, matchFamilyName, 140 normalStyle, bcp47, 1, ' '); 141 EXPECT_NE(typeface, nullptr); 142 OH_Drawing_TypefaceDestroy(typeface); 143 144 OH_Drawing_FontMgrDestroy(mgr); 145 } 146 147 /* 148 * @tc.name: OH_Drawing_FontMgrTest007 149 * @tc.desc: test for getting family name. 150 * @tc.type: FUNC 151 */ 152 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest007, TestSize.Level1) 153 { 154 int count = OH_Drawing_FontMgrGetFamilyCount(nullptr); 155 EXPECT_EQ(count, 0); 156 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 157 EXPECT_NE(mgr, nullptr); 158 char *familyName = OH_Drawing_FontMgrGetFamilyName(nullptr, 0); 159 EXPECT_EQ(familyName, nullptr); 160 OH_Drawing_FontMgrDestroyFamilyName(familyName); 161 OH_Drawing_FontMgrDestroy(mgr); 162 } 163 164 /* 165 * @tc.name: OH_Drawing_FontMgrTest008 166 * @tc.desc: test for matching family style. 167 * @tc.type: FUNC 168 */ 169 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest008, TestSize.Level1) 170 { 171 OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(nullptr, 0); 172 EXPECT_EQ(fontStyleSet, nullptr); 173 const char* matchFamilyName = "HarmonyOS-Sans"; 174 fontStyleSet = OH_Drawing_FontMgrMatchFamily(nullptr, matchFamilyName); 175 EXPECT_EQ(fontStyleSet, nullptr); 176 177 OH_Drawing_FontStyleStruct normalStyle; 178 normalStyle.weight = FONT_WEIGHT_400; 179 normalStyle.width = FONT_WIDTH_NORMAL; 180 normalStyle.slant = FONT_STYLE_NORMAL; 181 OH_Drawing_Typeface *typeface = OH_Drawing_FontMgrMatchFamilyStyle(nullptr, matchFamilyName, normalStyle); 182 EXPECT_EQ(typeface, nullptr); 183 184 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 185 typeface = OH_Drawing_FontMgrMatchFamilyStyleCharacter(mgr, nullptr, normalStyle, nullptr, 0, ' '); 186 EXPECT_NE(typeface, nullptr); 187 OH_Drawing_TypefaceDestroy(typeface); 188 189 typeface = OH_Drawing_FontMgrMatchFamilyStyleCharacter(mgr, matchFamilyName, normalStyle, nullptr, 0, ' '); 190 EXPECT_NE(typeface, nullptr); 191 OH_Drawing_TypefaceDestroy(typeface); 192 193 OH_Drawing_FontMgrDestroy(mgr); 194 } 195 196 /* 197 * @tc.name: OH_Drawing_FontMgrTest009 198 * @tc.desc: test for matching family style character. 199 * @tc.type: FUNC 200 */ 201 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest009, TestSize.Level1) 202 { 203 const char* matchFamilyName = "HarmonyOS-Sans"; 204 OH_Drawing_FontStyleStruct normalStyle; 205 normalStyle.weight = FONT_WEIGHT_400; 206 normalStyle.width = FONT_WIDTH_NORMAL; 207 normalStyle.slant = FONT_STYLE_NORMAL; 208 209 const char *bcp47[] = {"zh-Hans", "zh-CN"}; 210 OH_Drawing_Typeface *typeface = OH_Drawing_FontMgrMatchFamilyStyleCharacter(nullptr, matchFamilyName, 211 normalStyle, bcp47, 1, ' '); 212 EXPECT_EQ(typeface, nullptr); 213 OH_Drawing_TypefaceDestroy(typeface); 214 } 215 216 /* 217 * @tc.name: OH_Drawing_FontMgrTest010 218 * @tc.desc: test for create a typeface for the given index. 219 * @tc.type: FUNC 220 */ 221 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest010, TestSize.Level1) 222 { 223 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 224 OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0); 225 OH_Drawing_Typeface *typeface = OH_Drawing_FontStyleSetCreateTypeface(fontStyleSet, 0); 226 EXPECT_NE(typeface, nullptr); 227 typeface = OH_Drawing_FontStyleSetCreateTypeface(nullptr, 0); 228 EXPECT_EQ(typeface, nullptr); 229 OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet); 230 OH_Drawing_TypefaceDestroy(typeface); 231 OH_Drawing_FontMgrDestroy(mgr); 232 } 233 234 /* 235 * @tc.name: OH_Drawing_FontMgrTest011 236 * @tc.desc: test for get font style struct. 237 * @tc.type: FUNC 238 */ 239 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest011, TestSize.Level1) 240 { 241 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 242 OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0); 243 OH_Drawing_FontStyleStruct normalStyle; 244 char* styleName = nullptr; 245 normalStyle = OH_Drawing_FontStyleSetGetStyle(fontStyleSet, 0, &styleName); 246 ASSERT_NE(styleName, nullptr); 247 EXPECT_EQ(std::string(styleName), "normal"); 248 EXPECT_EQ(normalStyle.weight, 400); 249 OH_Drawing_FontStyleSetFreeStyleName(&styleName); 250 251 normalStyle = OH_Drawing_FontStyleSetGetStyle(fontStyleSet, 0, nullptr); 252 OH_Drawing_FontStyleSetFreeStyleName(nullptr); 253 254 OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet); 255 OH_Drawing_FontMgrDestroy(mgr); 256 } 257 258 /* 259 * @tc.name: OH_Drawing_FontMgrTest012 260 * @tc.desc: test for get typeface by match style. 261 * @tc.type: FUNC 262 */ 263 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest012, TestSize.Level1) 264 { 265 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 266 OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0); 267 OH_Drawing_FontStyleStruct normalStyle; 268 normalStyle.weight = FONT_WEIGHT_400; 269 normalStyle.width = FONT_WIDTH_NORMAL; 270 normalStyle.slant = FONT_STYLE_NORMAL; 271 OH_Drawing_Typeface* typeface = OH_Drawing_FontStyleSetMatchStyle(fontStyleSet, normalStyle); 272 EXPECT_NE(typeface, nullptr); 273 274 typeface = OH_Drawing_FontStyleSetMatchStyle(nullptr, normalStyle); 275 EXPECT_EQ(typeface, nullptr); 276 OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet); 277 OH_Drawing_TypefaceDestroy(typeface); 278 OH_Drawing_FontMgrDestroy(mgr); 279 } 280 281 /* 282 * @tc.name: OH_Drawing_FontMgrTest013 283 * @tc.desc: test for get font style set. 284 * @tc.type: FUNC 285 */ 286 HWTEST_F(OH_Drawing_FontMgrTest, OH_Drawing_FontMgrTest013, TestSize.Level1) 287 { 288 OH_Drawing_FontMgr *mgr = OH_Drawing_FontMgrCreate(); 289 OH_Drawing_FontStyleSet* fontStyleSet = OH_Drawing_FontMgrCreateFontStyleSet(mgr, 0); 290 int count = OH_Drawing_FontStyleSetCount(fontStyleSet); 291 EXPECT_EQ(count, 2); 292 293 count = OH_Drawing_FontStyleSetCount(nullptr); 294 EXPECT_EQ(count, 0); 295 OH_Drawing_FontMgrDestroyFontStyleSet(fontStyleSet); 296 OH_Drawing_FontMgrDestroy(mgr); 297 } 298 } // namespace Drawing 299 } // namespace Rosen 300 } // namespace OHOS