1 /* 2 * Copyright (c) 2025 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 <securec.h> 17 18 #include "drawing_font_collection.h" 19 #include "drawing_text_typography.h" 20 #include "gtest/gtest.h" 21 22 using namespace testing; 23 using namespace testing::ext; 24 25 constexpr float FLOAT_DATA_EPSILON = 1e-6f; 26 class NdkTypographyStrutStyleTest : public testing::Test { 27 }; 28 29 /* 30 * @tc.name: TypographyStrutStyleTest001 31 * @tc.desc: test for getting and setting strut style 32 * @tc.type: FUNC 33 */ 34 HWTEST_F(NdkTypographyStrutStyleTest, TypographyStrutStyleTest001, TestSize.Level0) 35 { 36 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle(); 37 OH_Drawing_StrutStyle* strutstyle = new OH_Drawing_StrutStyle(); 38 strutstyle->weight = FONT_WEIGHT_400; 39 strutstyle->style = FONT_STYLE_ITALIC; 40 // 17.0 For size 41 strutstyle->size = 17.0; 42 // 2.0 For heightScale 43 strutstyle->heightScale = 2; 44 strutstyle->heightOverride = true; 45 strutstyle->halfLeading = true; 46 // 3.0 For leading 47 strutstyle->leading = 3.0; 48 strutstyle->forceStrutHeight = true; 49 // 4 For families size 50 strutstyle->familiesSize = 4; 51 strutstyle->families = (char**)malloc(strutstyle->familiesSize * sizeof(char*)); 52 const char* temp[] = { "1", "2", "3", "4" }; 53 for (int i = 0; i < strutstyle->familiesSize; i++) { 54 // 2 For families member size 55 strutstyle->families[i] = (char*)malloc(2 * sizeof(char)); 56 strcpy_s(strutstyle->families[i], 2, temp[i]); 57 } 58 OH_Drawing_SetTypographyStyleTextStrutStyle(typoStyle, strutstyle); 59 EXPECT_NE(OH_Drawing_TypographyStyleGetStrutStyle(typoStyle), nullptr); 60 OH_Drawing_TypographyStyleDestroyStrutStyle(strutstyle); 61 OH_Drawing_DestroyTypographyStyle(typoStyle); 62 } 63 64 /* 65 * @tc.name: TypographyStrutStyleTest002 66 * @tc.desc: test for strutstyle equals 67 * @tc.type: FUNC 68 */ 69 HWTEST_F(NdkTypographyStrutStyleTest, TypographyStrutStyleTest002, TestSize.Level0) 70 { 71 OH_Drawing_StrutStyle* from = new OH_Drawing_StrutStyle(); 72 OH_Drawing_StrutStyle* to = new OH_Drawing_StrutStyle(); 73 bool result = OH_Drawing_TypographyStyleStrutStyleEquals(from, to); 74 EXPECT_TRUE(result); 75 result = OH_Drawing_TypographyStyleStrutStyleEquals(nullptr, to); 76 EXPECT_FALSE(result); 77 result = OH_Drawing_TypographyStyleStrutStyleEquals(from, nullptr); 78 EXPECT_FALSE(result); 79 OH_Drawing_TypographyStyleDestroyStrutStyle(from); 80 OH_Drawing_TypographyStyleDestroyStrutStyle(to); 81 } 82 83 /* 84 * @tc.name: TypographyStrutStyleTest003 85 * @tc.desc: test for setting strut style and getting strut style 86 * @tc.type: FUNC 87 */ 88 HWTEST_F(NdkTypographyStrutStyleTest, TypographyStrutStyleTest003, TestSize.Level0) 89 { 90 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle(); 91 OH_Drawing_SetTypographyTextWordBreakType(typoStyle, WORD_BREAK_TYPE_BREAK_ALL); 92 OH_Drawing_SetTypographyTextMaxLines(typoStyle, 10); 93 94 OH_Drawing_StrutStyle* strutstyle = new OH_Drawing_StrutStyle(); 95 strutstyle->weight = FONT_WEIGHT_400; 96 strutstyle->style = FONT_STYLE_ITALIC; 97 // 17.0 For size 98 strutstyle->size = 17.0; 99 // 2.0 For heightScale 100 strutstyle->heightScale = 2; 101 strutstyle->heightOverride = true; 102 strutstyle->halfLeading = true; 103 // 3.0 For leading 104 strutstyle->leading = 3.0; 105 strutstyle->forceStrutHeight = true; 106 // 4 For families size 107 strutstyle->familiesSize = 4; 108 strutstyle->families = (char**)malloc(strutstyle->familiesSize * sizeof(char*)); 109 const char* temp[] = { "1", "2", "3", "4" }; 110 for (int i = 0; i < strutstyle->familiesSize; i++) { 111 // 2 For families member size 112 strutstyle->families[i] = (char*)malloc(2 * sizeof(char)); 113 strcpy_s(strutstyle->families[i], 2, temp[i]); 114 } 115 OH_Drawing_SetTypographyStyleTextStrutStyle(typoStyle, strutstyle); 116 OH_Drawing_SetTypographyTextUseLineStyle(typoStyle, true); 117 ASSERT_NE(OH_Drawing_TypographyStyleGetStrutStyle(typoStyle), nullptr); 118 119 OH_Drawing_TypographyCreate *handler = 120 OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateSharedFontCollection()); 121 OH_Drawing_TextStyle *textStyle = OH_Drawing_CreateTextStyle(); 122 OH_Drawing_SetTextStyleFontSize(textStyle, 52); 123 OH_Drawing_TypographyHandlerPushTextStyle(handler, textStyle); 124 125 const char* text = "HelloWorldHelloWorldHelloWorldHelloWorld"; 126 OH_Drawing_TypographyHandlerAddText(handler, text); 127 OH_Drawing_TypographyHandlerPopTextStyle(handler); 128 129 OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler); 130 OH_Drawing_TypographyLayout(typography, 1200); 131 132 OH_Drawing_LineMetrics lineMerics; 133 OH_Drawing_TypographyGetLineMetricsAt(typography, 0, &lineMerics); 134 EXPECT_EQ(lineMerics.height, 85); 135 136 OH_Drawing_DestroyTypography(typography); 137 OH_Drawing_DestroyTextStyle(textStyle); 138 OH_Drawing_DestroyTypographyHandler(handler); 139 OH_Drawing_TypographyStyleDestroyStrutStyle(strutstyle); 140 OH_Drawing_DestroyTypographyStyle(typoStyle); 141 } 142 143 /* 144 * @tc.name: TypographyStrutStyleTest004 145 * @tc.desc: test for strutstyle not equal 146 * @tc.type: FUNC 147 */ 148 HWTEST_F(NdkTypographyStrutStyleTest, TypographyStrutStyleTest004, TestSize.Level0) 149 { 150 OH_Drawing_StrutStyle* from = new OH_Drawing_StrutStyle(); 151 // 4 For families size 152 from->familiesSize = 4; 153 from->families = (char**)malloc(from->familiesSize * sizeof(char*)); 154 const char* temp[] = { "1", "2", "3", "4" }; 155 for (int i = 0; i < from->familiesSize; i++) { 156 // 2 For families member size 157 from->families[i] = (char*)malloc(2 * sizeof(char)); 158 strcpy_s(from->families[i], 2, temp[i]); 159 } 160 OH_Drawing_StrutStyle* to = new OH_Drawing_StrutStyle(); 161 // 3 For families size 162 to->familiesSize = 3; 163 to->families = (char**)malloc(to->familiesSize * sizeof(char*)); 164 const char* temp1[] = { "3", "2", "1"}; 165 for (int i = 0; i < to->familiesSize; i++) { 166 // 2 For families member size 167 to->families[i] = (char*)malloc(2 * sizeof(char)); 168 strcpy_s(to->families[i], 2, temp1[i]); 169 } 170 bool result = OH_Drawing_TypographyStyleStrutStyleEquals(from, to); 171 EXPECT_FALSE(result); 172 173 OH_Drawing_StrutStyle* from1 = new OH_Drawing_StrutStyle(); 174 from1->size = 98; 175 OH_Drawing_StrutStyle* to1 = new OH_Drawing_StrutStyle(); 176 to1->size = 99; 177 bool result1 = OH_Drawing_TypographyStyleStrutStyleEquals(from1, to1); 178 EXPECT_FALSE(result1); 179 OH_Drawing_TypographyStyleDestroyStrutStyle(from); 180 OH_Drawing_TypographyStyleDestroyStrutStyle(to); 181 OH_Drawing_TypographyStyleDestroyStrutStyle(from1); 182 OH_Drawing_TypographyStyleDestroyStrutStyle(to1); 183 } 184 185 /* 186 * @tc.name: TypographyStrutStyleTest005 187 * @tc.desc: test for getting strut style where familySize equals 0 188 * @tc.type: FUNC 189 */ 190 HWTEST_F(NdkTypographyStrutStyleTest, TypographyStrutStyleTest005, TestSize.Level0) 191 { 192 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle(); 193 OH_Drawing_StrutStyle* strutstyle1 = new OH_Drawing_StrutStyle(); 194 strutstyle1->familiesSize = 0; 195 strutstyle1->families = (char**)malloc(1 * sizeof(char*)); 196 OH_Drawing_SetTypographyStyleTextStrutStyle(typoStyle, strutstyle1); 197 OH_Drawing_StrutStyle* structStyle = OH_Drawing_TypographyStyleGetStrutStyle(typoStyle); 198 EXPECT_EQ(structStyle->familiesSize, 0); 199 EXPECT_EQ(structStyle->families, nullptr); 200 201 free(strutstyle1->families); 202 OH_Drawing_TypographyStyleDestroyStrutStyle(strutstyle1); 203 204 // branch coverage 205 OH_Drawing_SetTypographyStyleTextStrutStyle(typoStyle, nullptr); 206 OH_Drawing_TypographyStyleDestroyStrutStyle(nullptr); 207 208 OH_Drawing_TypographyStyleDestroyStrutStyle(structStyle); 209 OH_Drawing_DestroyTypographyStyle(typoStyle); 210 } 211 212 /* 213 * @tc.name: TypographyStrutStyleTest006 214 * @tc.desc: test for strut style should not take effect when not enabled 215 * @tc.type: FUNC 216 */ 217 HWTEST_F(NdkTypographyStrutStyleTest, TypographyStrutStyleTest006, TestSize.Level0) 218 { 219 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle(); 220 OH_Drawing_FontCollection* fontCollection = OH_Drawing_CreateSharedFontCollection(); 221 OH_Drawing_StrutStyle* strutStyle = new OH_Drawing_StrutStyle(); 222 strutStyle->size = 15; 223 strutStyle->heightScale = 2; 224 strutStyle->heightOverride = true; 225 strutStyle->forceStrutHeight = true; 226 OH_Drawing_SetTypographyStyleTextStrutStyle(typoStyle, strutStyle); 227 228 const char* text = "你好世界"; 229 OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle, fontCollection); 230 OH_Drawing_TextStyle* textStyle = OH_Drawing_CreateTextStyle(); 231 OH_Drawing_SetTextStyleFontSize(textStyle, 15); 232 OH_Drawing_TypographyHandlerPushTextStyle(handler, textStyle); 233 OH_Drawing_TypographyHandlerAddText(handler, text); 234 OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler); 235 OH_Drawing_TypographyLayout(typography, 1000); 236 237 OH_Drawing_LineMetrics lineMetrics; 238 OH_Drawing_TypographyGetLineMetricsAt(typography, 0, &lineMetrics); 239 EXPECT_NEAR(lineMetrics.height, 18.000000, FLOAT_DATA_EPSILON); 240 241 delete strutStyle; 242 OH_Drawing_DestroyFontCollection(fontCollection); 243 OH_Drawing_DestroyTypographyStyle(typoStyle); 244 OH_Drawing_DestroyTypographyHandler(handler); 245 OH_Drawing_DestroyTypography(typography); 246 OH_Drawing_DestroyTextStyle(textStyle); 247 }