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 "test/unittest/adapter/ohos/capability/convert_test_tools.h" 17 18 #undef private 19 #undef protected 20 21 using namespace testing; 22 using namespace testing::ext; 23 namespace OHOS::Ace::NG { 24 25 namespace { 26 std::string testStr[] = { "微软雅黑", "宋体", "Times New Roman", "未知字体", "Unknow", "楷体"}; 27 Font testFont1 { OHOS::Ace::FontWeight::BOLDER, Dimension(0.0, DimensionUnit::VP), OHOS::Ace::FontStyle::NORMAL, 28 std::vector<std::string>(testStr, testStr + 5), OHOS::Ace::Color::WHITE }; 29 Font testFont2 { OHOS::Ace::FontWeight::LIGHTER, Dimension(20.0, DimensionUnit::PX), OHOS::Ace::FontStyle::ITALIC, 30 std::vector<std::string>(testStr, testStr + 5), OHOS::Ace::Color::BLACK }; 31 } 32 33 // imageOption1: default parameter 34 auto imageOption1 = HtmlConvertTestNg::GetImageOption( 35 "src/icon-1.png", 36 Dimension(50.0, DimensionUnit::VP), 37 Dimension(50.0, DimensionUnit::VP) 38 ); 39 40 // imageOption2: define ImageFit, VerticalAlign and BorderRadiusProperty 41 auto imageOption2 = HtmlConvertTestNg::GetImageOption( 42 "src/icon-2.png", 43 Dimension(100.0, DimensionUnit::VP), 44 Dimension(100.0, DimensionUnit::VP), 45 ImageFit::COVER, 46 VerticalAlign::TOP, 47 BorderRadiusProperty(Dimension(10.0, DimensionUnit::VP)) 48 ); 49 50 // imageOption3: ImageFit, VerticalAlign and BorderRadiusProperty set 2 51 auto imageOption3 = HtmlConvertTestNg::GetImageOption( 52 "src/icon-3.png", 53 Dimension(75.0, DimensionUnit::VP), 54 Dimension(75.0, DimensionUnit::VP), 55 ImageFit::FILL, 56 VerticalAlign::BOTTOM, 57 BorderRadiusProperty(Dimension(5.0, DimensionUnit::VP)) 58 ); 59 60 // imageOption3: ImageFit, VerticalAlign and BorderRadiusProperty set 3 61 auto imageOption4 = HtmlConvertTestNg::GetImageOption( 62 "src/icon-4.png", 63 Dimension(150.0, DimensionUnit::VP), 64 Dimension(150.0, DimensionUnit::VP), 65 ImageFit::NONE, 66 VerticalAlign::CENTER, 67 BorderRadiusProperty(Dimension(20.0, DimensionUnit::VP)) 68 ); 69 70 /** 71 * @tc.name: HtmlConvertComplex01 72 * @tc.desc: This test case checks the conversion of a span string with various complex styles like font, 73 * letter spacing, background color, line height, text shadow, image spans, and paragraph styles. 74 * It ensures that all styles are properly applied and the correct number of span items is produced. 75 * @tc.level: 1 76 */ 77 HWTEST_F(HtmlConvertTestNg, HtmlConvertComplex01, TestSize.Level1) 78 { 79 /** 80 * @tc.steps1: Initialize mutable SpanString and add a variety of styles like font, letter spacing, 81 * background color, line height, and text shadow. 82 * @tc.expected: Span string should apply all styles correctly and be ready for conversion. 83 */ 84 auto imageOption = GetImageOption("src/icon-2.png"); 85 auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption); 86 auto spanString3 = AceType::MakeRefPtr<SpanString>(u"Hello World! This is a test to cover complex cases."); 87 88 // Adding Font Spans 89 spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 5)); 90 spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 6, 11)); 91 92 // Adding Letter Spacing 93 spanString3->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(3), 12, 18)); 94 95 // Adding Background Color Span 96 NG::BorderRadiusProperty borderRadius; 97 borderRadius.radiusTopLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP); 98 borderRadius.radiusTopRight = Dimension(0, OHOS::Ace::DimensionUnit::VP); 99 borderRadius.radiusBottomLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP); 100 borderRadius.radiusBottomRight = Dimension(0, OHOS::Ace::DimensionUnit::VP); 101 TextBackgroundStyle textBackgroundStyle; 102 textBackgroundStyle.backgroundColor = Color::RED; 103 textBackgroundStyle.backgroundRadius = borderRadius; 104 spanString3->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle, 19, 25)); 105 106 // Adding Line Height Span 107 spanString3->AddSpan(AceType::MakeRefPtr<LineHeightSpan>(Dimension(1.5), 26, 32)); 108 109 // Adding Text Shadow Span 110 Shadow shadow1; 111 shadow1.SetBlurRadius(2.0); 112 shadow1.SetColor(Color::RED); 113 shadow1.SetOffsetX(4.0); 114 shadow1.SetOffsetY(4.0); 115 116 spanString3->AddSpan(AceType::MakeRefPtr<TextShadowSpan>(std::vector<Shadow>{shadow1}, 15, 25)); 117 118 // Adding Image Span 119 auto imageSpan = AceType::MakeRefPtr<ImageSpan>(imageOption); 120 spanString3->AddSpan(imageSpan); 121 122 // Adding a complex Paragraph Style 123 auto paragraphStyle = GetDefaultParagraphStyle(); 124 auto paraSpan = AceType::MakeRefPtr<ParagraphStyleSpan>(paragraphStyle, 8, 15); 125 spanString3->AddSpan(paraSpan); 126 127 mutableStr->InsertSpanString(0, spanString3); 128 129 /** 130 * @tc.steps2: Create a new SpanString with Chinese content and more complex styles, 131 * and insert into mutable string. 132 * @tc.expected: Correct application of font, letter spacing, and span insertion. 133 */ 134 auto spanString2 = AceType::MakeRefPtr<SpanString>(u"中文文本,包含更多复杂的样式。123456"); 135 spanString2->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 2)); 136 spanString2->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 3, 6)); 137 spanString2->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(8), 7, 10)); 138 139 mutableStr->InsertSpanString(16, spanString2); 140 141 /** 142 * @tc.steps3: Call the conversion function to convert the mutable span string to HTML. 143 * @tc.expected: Ensure that the conversion correctly maintains all span properties and styles. 144 */ 145 SpanToHtml convert; 146 auto out = convert.ToHtml(*mutableStr); 147 148 /** 149 * @tc.steps4: Convert HTML back to SpanString and validate the number of span items. 150 * @tc.expected: The number of span items should match the total number of spans added. 151 */ 152 HtmlToSpan toSpan; 153 auto dstSpan = toSpan.ToSpanString(out); 154 EXPECT_NE(dstSpan, nullptr); 155 auto items = dstSpan->GetSpanItems(); 156 EXPECT_EQ(items.size(), 22); 157 } 158 159 /** 160 * @tc.name: HtmlConvertComplex02 161 * @tc.desc: This test case checks the conversion of a span string with nested and complex styles like 162 * font spans, letter spacing, background color, and image spans. It ensures that the nested 163 * styles are applied correctly and that the correct number of span items is produced. 164 * @tc.level: 1 165 */ 166 HWTEST_F(HtmlConvertTestNg, HtmlConvertComplex02, TestSize.Level1) 167 { 168 /** 169 * @tc.steps1: Initialize mutable SpanString and add various font, letter spacing, background color, 170 * and image spans in a nested manner. 171 * @tc.expected: Nested and complex spans should be applied correctly. 172 */ 173 auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption1); 174 auto spanString4 = AceType::MakeRefPtr<SpanString>(u"Complex Nested Styles Test: Begin"); 175 176 // Add some font spans with different styles 177 spanString4->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 7)); 178 spanString4->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 8, 15)); 179 spanString4->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 16, 21)); 180 181 // Add multiple letter spacing spans 182 spanString4->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(5), 5, 10)); 183 spanString4->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(2), 12, 18)); 184 185 // Add Background Color Span for a range 186 NG::BorderRadiusProperty borderRadius; 187 borderRadius.radiusTopLeft = Dimension(4, OHOS::Ace::DimensionUnit::FP); 188 borderRadius.radiusTopRight = Dimension(3, OHOS::Ace::DimensionUnit::FP); 189 borderRadius.radiusBottomLeft = Dimension(5, OHOS::Ace::DimensionUnit::FP); 190 borderRadius.radiusBottomRight = Dimension(7, OHOS::Ace::DimensionUnit::FP); 191 TextBackgroundStyle textBackgroundStyle; 192 textBackgroundStyle.backgroundColor = Color::RED; 193 textBackgroundStyle.backgroundRadius = borderRadius; 194 spanString4->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle, 5, 15)); 195 196 // Add Image Span at the end 197 auto imageSpan = AceType::MakeRefPtr<ImageSpan>(imageOption2); 198 spanString4->AddSpan(imageSpan); 199 200 mutableStr->InsertSpanString(0, spanString4); 201 202 /** 203 * @tc.steps2: Create another SpanString with more complex Chinese content, 204 * add font spans, letter spacing, and text shadow, and insert it into mutable string. 205 * @tc.expected: The second span string with additional styles should be properly inserted and applied. 206 */ 207 auto spanString5 = AceType::MakeRefPtr<SpanString>(u"进一步增加复杂性,测试多个样式的组合应用。"); 208 spanString5->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 0, 4)); 209 spanString5->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 5, 9)); 210 spanString5->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(7), 9, 14)); 211 212 Shadow shadow1; 213 shadow1.SetBlurRadius(10.0); 214 shadow1.SetColor(Color::RED); 215 shadow1.SetOffsetX(-10.0); 216 shadow1.SetOffsetY(0.0); 217 spanString5->AddSpan(AceType::MakeRefPtr<TextShadowSpan>(std::vector<Shadow>{shadow1}, 10, 20)); 218 219 mutableStr->InsertSpanString(10, spanString5); 220 221 /** 222 * @tc.steps3: Call the conversion function to convert the mutable span string to HTML. 223 * @tc.expected: Ensure that the conversion correctly maintains all nested and complex span properties. 224 */ 225 SpanToHtml convert; 226 auto out = convert.ToHtml(*mutableStr); 227 228 /** 229 * @tc.steps4: Convert HTML back to SpanString and validate the number of span items. 230 * @tc.expected: The number of span items should match the total number of spans added. 231 */ 232 HtmlToSpan toSpan; 233 auto dstSpan = toSpan.ToSpanString(out); 234 EXPECT_NE(dstSpan, nullptr); 235 auto items = dstSpan->GetSpanItems(); 236 EXPECT_EQ(items.size(), 20); 237 } 238 239 /** 240 * @tc.name: HtmlConvertComplex03 241 * @tc.desc: This test case verifies the conversion of a very large text block with multiple font spans, 242 * letter spacing, background color, and text shadow applied in various iterations. It ensures 243 * that the correct number of span items is produced and that the styles are applied consistently. 244 * @tc.level: 1 245 */ 246 HWTEST_F(HtmlConvertTestNg, HtmlConvertComplex03, TestSize.Level1) 247 { 248 /** 249 * @tc.steps1: Initialize mutable SpanString with a large text block and apply various font, 250 * letter spacing, and other styles. 251 * @tc.expected: Large text block with correct number of spans, properly applied styles, and correct span count. 252 */ 253 auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption3); 254 255 // Test with a very large text block, mimicking an article 256 std::u16string largeText = u"这是一段包含多个字体和样式的大段文本。我们将使用不同的样式组合来测试转换的效果。"; 257 auto spanString6 = AceType::MakeRefPtr<SpanString>(largeText); 258 259 // Apply multiple font spans with varied fonts 260 spanString6->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 5)); 261 spanString6->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 6, 10)); 262 spanString6->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 11, 15)); 263 264 // Add a large number of letter spacing spans 265 for (int i = 0; i < 20; ++i) { 266 spanString6->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(i), i * 2, i * 3)); 267 } 268 269 // Add a combination of other spans 270 Shadow shadow; 271 shadow.SetBlurRadius(2.0); 272 shadow.SetColor(Color::TRANSPARENT); 273 shadow.SetOffsetX(2.0); 274 shadow.SetOffsetY(2.0); 275 spanString6->AddSpan(AceType::MakeRefPtr<TextShadowSpan>(std::vector<Shadow>{shadow}, 6, 12)); 276 277 NG::BorderRadiusProperty borderRadius; 278 borderRadius.radiusTopLeft = Dimension(1, OHOS::Ace::DimensionUnit::AUTO); 279 borderRadius.radiusTopRight = Dimension(2, OHOS::Ace::DimensionUnit::AUTO); 280 borderRadius.radiusBottomLeft = Dimension(3, OHOS::Ace::DimensionUnit::AUTO); 281 borderRadius.radiusBottomRight = Dimension(4, OHOS::Ace::DimensionUnit::AUTO); 282 283 TextBackgroundStyle textBackgroundStyle; 284 textBackgroundStyle.backgroundColor = Color::RED; 285 textBackgroundStyle.backgroundRadius = borderRadius; 286 spanString6->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle, 0, 10)); 287 288 mutableStr->InsertSpanString(0, spanString6); 289 290 /** 291 * @tc.steps2: Convert the large mutable string to HTML format and validate the number of span items. 292 * @tc.expected: The conversion should result in a high number of span items due to the large amount of styling. 293 */ 294 SpanToHtml convert; 295 auto out = convert.ToHtml(*mutableStr); 296 297 HtmlToSpan toSpan; 298 auto dstSpan = toSpan.ToSpanString(out); 299 EXPECT_NE(dstSpan, nullptr); 300 auto items = dstSpan->GetSpanItems(); 301 EXPECT_EQ(items.size(), 26); 302 } 303 304 /** 305 * @tc.name: HtmlConvertComplex04 306 * @tc.desc: This test case checks the conversion of multiple text blocks with embedded images and various 307 * styles such as font and letter spacing applied. It verifies the correct insertion of images and 308 * the accurate number of span items after conversion. 309 * @tc.level: 3 310 */ 311 HWTEST_F(HtmlConvertTestNg, HtmlConvertComplex04, TestSize.Level3) 312 { 313 /** 314 * @tc.steps1: Initialize mutable SpanString with imageOption1. 315 * @tc.expected: The mutable string should be ready for inserting span strings with various styles and images. 316 */ 317 auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption1); 318 319 // First text block with an image 320 /** 321 * @tc.steps2: Create the first text span string and apply font and letter spacing styles to it. 322 * Insert an image (imageOption1) into the text span. 323 * @tc.expected: The first span string should be styled correctly and the image should be inserted 324 * at the correct position. 325 */ 326 std::u16string text1 = u"This is the first paragraph with an image display:"; 327 auto spanString1 = AceType::MakeRefPtr<SpanString>(text1); 328 spanString1->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10)); // "This is the first" 329 spanString1->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(4), 10, 20)); // "paragraph" 330 331 // Insert image (using imageOption1) 332 auto imageSpan1 = AceType::MakeRefPtr<ImageSpan>(imageOption1); 333 spanString1->AddSpan(imageSpan1); 334 335 mutableStr->InsertSpanString(0, spanString1); 336 337 // Second text block with different image configurations 338 /** 339 * @tc.steps3: Create the second text span string and apply font and letter spacing styles. 340 * Insert a different image (imageOption2) into this span string. 341 * @tc.expected: The second span string should have proper font styles, letter spacing, and image insertion. 342 */ 343 std::u16string text2 = u"The second paragraph with different image configurations:"; 344 auto spanString2 = AceType::MakeRefPtr<SpanString>(text2); 345 spanString2->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 0, 10)); // "The second" 346 spanString2->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 11, 18)); // "paragraph" 347 spanString2->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(6), 18, 25)); // "with different" 348 349 // Insert image (using imageOption2) 350 auto imageSpan2 = AceType::MakeRefPtr<ImageSpan>(imageOption2); 351 spanString2->AddSpan(imageSpan2); 352 353 mutableStr->InsertSpanString(30, spanString2); 354 355 // Third text block with more images 356 /** 357 * @tc.steps4: Create the third text span string and apply font and letter spacing styles. 358 * Insert an image (imageOption3) into this span string. 359 * @tc.expected: The third span string should have the correct styles and image insertion. 360 */ 361 std::u16string text3 = u"The third paragraph showcasing another image configuration:"; 362 auto spanString3 = AceType::MakeRefPtr<SpanString>(text3); 363 spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10)); // "The third" 364 spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 11, 15)); // "paragraph" 365 spanString3->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(3), 15, 20)); // "showcasing" 366 367 // Insert image (using imageOption3) 368 auto imageSpan3 = AceType::MakeRefPtr<ImageSpan>(imageOption3); 369 spanString3->AddSpan(imageSpan3); 370 371 mutableStr->InsertSpanString(50, spanString3); 372 373 // Fourth text block with the final image 374 /** 375 * @tc.steps5: Create the fourth text span string and apply font and letter spacing styles. 376 * Insert the final image (imageOption4) into this span string. 377 * @tc.expected: The fourth span string should be styled correctly and the image should 378 * be inserted at the right position. 379 */ 380 std::u16string text4 = u"The final paragraph showcasing the last image configuration:"; 381 auto spanString4 = AceType::MakeRefPtr<SpanString>(text4); 382 spanString4->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 0, 10)); // "The final" 383 spanString4->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(4), 10, 18)); // "paragraph" 384 385 // Insert image (using imageOption4) 386 auto imageSpan4 = AceType::MakeRefPtr<ImageSpan>(imageOption4); 387 spanString4->AddSpan(imageSpan4); 388 389 mutableStr->InsertSpanString(70, spanString4); 390 391 /** 392 * @tc.steps6: Convert the entire mutable string to HTML format and verify the result. 393 * @tc.expected: The conversion should result in the correct number of span items, 394 * including those with images and different styles. 395 */ 396 SpanToHtml convert; 397 auto out = convert.ToHtml(*mutableStr); 398 399 HtmlToSpan toSpan; 400 auto dstSpan = toSpan.ToSpanString(out); 401 EXPECT_NE(dstSpan, nullptr); 402 auto items = dstSpan->GetSpanItems(); 403 404 EXPECT_EQ(items.size(), 24); 405 } 406 407 /** 408 * @tc.name: HtmlConvert000 409 * @tc.desc: This test case verifies the conversion of a simple SpanString (without complex styles) 410 * to TLV format, HTML format, and back to SpanString. It ensures the conversion 411 * maintains the integrity of the content and results in identical HTML outputs after 412 * multiple conversions. 413 * @tc.level: 1 414 */ 415 HWTEST_F(HtmlConvertTestNg, HtmlConvert000, TestSize.Level1) 416 { 417 /** 418 * @tc.steps1: Create a simple SpanString with basic text content. 419 * @tc.expected: The SpanString should be successfully created without any errors. 420 */ 421 auto spanString = AceType::MakeRefPtr<SpanString>(u"Hello"); 422 423 /** 424 * @tc.steps2: Convert the SpanString to TLV format and ensure the result is not empty. 425 * @tc.expected: TLV format conversion should produce a non-empty result. 426 */ 427 std::vector<uint8_t> buff; 428 spanString->EncodeTlv(buff); 429 EXPECT_EQ(buff.size() > 0, true); 430 431 /** 432 * @tc.steps3: Convert the TLV format back to HTML and compare it with the direct HTML conversion 433 * of the SpanString. 434 * @tc.expected: The HTML results from both methods should be identical. 435 */ 436 SpanToHtml toHtml; 437 auto htmlFromU8 = toHtml.ToHtml(buff); 438 auto htmlFromSpan = toHtml.ToHtml(*spanString); 439 EXPECT_EQ(htmlFromU8, htmlFromSpan); 440 441 /** 442 * @tc.steps4: Convert the HTML back to SpanString and verify that the items match. 443 * @tc.expected: The SpanString items after conversion should be identical to the original. 444 */ 445 HtmlToSpan toSpan; 446 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 447 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 448 449 /** 450 * @tc.steps5: Convert the SpanString back to HTML and verify consistency. 451 * @tc.expected: The final HTML should match the HTML obtained earlier. 452 */ 453 SpanToHtml toHtml1; 454 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 455 EXPECT_EQ(hmtlString, htmlFromSpan); 456 } 457 458 /** 459 * @tc.name: HtmlConvert001 460 * @tc.desc: This test case checks the conversion of a SpanString with different font styles applied 461 * to different parts of the text. The test ensures that font spans are correctly applied 462 * during conversion to TLV, HTML, and back to SpanString. 463 * @tc.level: 1 464 */ 465 HWTEST_F(HtmlConvertTestNg, HtmlConvert001, TestSize.Level1) 466 { 467 /** 468 * @tc.steps1: Create a SpanString with text and apply font styles to different ranges. 469 * @tc.expected: Font styles should be applied correctly to the specified ranges. 470 */ 471 auto spanString = AceType::MakeRefPtr<SpanString>(u"Hello World"); 472 spanString->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 5)); 473 spanString->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 6, 11)); 474 475 /** 476 * @tc.steps2: Convert the SpanString to TLV format and ensure the result is not empty. 477 * @tc.expected: TLV format conversion should produce a non-empty result. 478 */ 479 std::vector<uint8_t> buff; 480 spanString->EncodeTlv(buff); 481 EXPECT_EQ(buff.size() > 0, true); 482 483 /** 484 * @tc.steps3: Convert the TLV format back to HTML and compare it with the direct HTML conversion 485 * of the SpanString. 486 * @tc.expected: The HTML results from both methods should be identical. 487 */ 488 SpanToHtml toHtml; 489 auto htmlFromU8 = toHtml.ToHtml(buff); 490 auto htmlFromSpan = toHtml.ToHtml(*spanString); 491 EXPECT_EQ(htmlFromU8, htmlFromSpan); 492 493 /** 494 * @tc.steps4: Convert the HTML back to SpanString and verify that the items match. 495 * @tc.expected: The SpanString items after conversion should be identical to the original. 496 */ 497 HtmlToSpan toSpan; 498 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 499 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 500 501 /** 502 * @tc.steps5: Convert the SpanString back to HTML and verify consistency. 503 * @tc.expected: The final HTML should match the HTML obtained earlier. 504 */ 505 SpanToHtml toHtml1; 506 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 507 EXPECT_EQ(hmtlString, htmlFromSpan); 508 } 509 510 /** 511 * @tc.name: HtmlConvert002 512 * @tc.desc: This test case checks the conversion of a SpanString with letter spacing applied to 513 * different parts of the text. The test ensures that letter spacing is correctly applied 514 * during conversion to TLV, HTML, and back to SpanString. 515 * @tc.level: 1 516 */ 517 HWTEST_F(HtmlConvertTestNg, HtmlConvert002, TestSize.Level1) 518 { 519 /** 520 * @tc.steps1: Create a SpanString with text and apply letter spacing to different ranges. 521 * @tc.expected: Letter spacing should be applied correctly to the specified ranges. 522 */ 523 auto spanString = AceType::MakeRefPtr<SpanString>(u"Hello World"); 524 spanString->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(2), 0, 5)); 525 spanString->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(3), 6, 11)); 526 527 /** 528 * @tc.steps2: Convert the SpanString to TLV format and ensure the result is not empty. 529 * @tc.expected: TLV format conversion should produce a non-empty result. 530 */ 531 std::vector<uint8_t> buff; 532 spanString->EncodeTlv(buff); 533 EXPECT_EQ(buff.size() > 0, true); 534 535 /** 536 * @tc.steps3: Convert the TLV format back to HTML and compare it with the direct HTML conversion 537 * of the SpanString. 538 * @tc.expected: The HTML results from both methods should be identical. 539 */ 540 SpanToHtml toHtml; 541 auto htmlFromU8 = toHtml.ToHtml(buff); 542 auto htmlFromSpan = toHtml.ToHtml(*spanString); 543 EXPECT_EQ(htmlFromU8, htmlFromSpan); 544 545 /** 546 * @tc.steps4: Convert the HTML back to SpanString and verify that the items match. 547 * @tc.expected: The SpanString items after conversion should be identical to the original. 548 */ 549 HtmlToSpan toSpan; 550 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 551 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 552 553 /** 554 * @tc.steps5: Convert the SpanString back to HTML and verify consistency. 555 * @tc.expected: The final HTML should match the HTML obtained earlier. 556 */ 557 SpanToHtml toHtml1; 558 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 559 EXPECT_EQ(hmtlString, htmlFromSpan); 560 } 561 562 /** 563 * @tc.name: HtmlConvert003 564 * @tc.desc: This test case checks the conversion of a SpanString with background color and text 565 * shadow applied to different parts of the text. It ensures that background and shadow 566 * styles are properly converted between TLV, HTML, and SpanString formats. 567 * @tc.level: 1 568 */ 569 HWTEST_F(HtmlConvertTestNg, HtmlConvert003, TestSize.Level1) 570 { 571 /** 572 * @tc.steps1: Create a SpanString with text and apply background color and text shadow. 573 * @tc.expected: Background color and text shadow should be applied correctly. 574 */ 575 auto spanString = AceType::MakeRefPtr<SpanString>(u"Hello World"); 576 577 // Add background color 578 NG::BorderRadiusProperty borderRadius; 579 TextBackgroundStyle textBackgroundStyle; 580 textBackgroundStyle.backgroundColor = Color::BLUE; 581 textBackgroundStyle.backgroundRadius = borderRadius; 582 spanString->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle, 0, 5)); 583 584 // Add text shadow 585 Shadow shadow1; 586 shadow1.SetBlurRadius(5.0); 587 shadow1.SetColor(Color::RED); 588 shadow1.SetOffsetX(2.0); 589 shadow1.SetOffsetY(2.0); 590 spanString->AddSpan(AceType::MakeRefPtr<TextShadowSpan>(std::vector<Shadow>{shadow1}, 6, 11)); 591 592 /** 593 * @tc.steps2: Convert the SpanString to TLV format and ensure the result is not empty. 594 * @tc.expected: TLV format conversion should produce a non-empty result. 595 */ 596 std::vector<uint8_t> buff; 597 spanString->EncodeTlv(buff); 598 EXPECT_EQ(buff.size() > 0, true); 599 600 /** 601 * @tc.steps3: Convert the TLV format back to HTML and compare it with the direct HTML conversion 602 * of the SpanString. 603 * @tc.expected: The HTML results from both methods should be identical. 604 */ 605 SpanToHtml toHtml; 606 auto htmlFromU8 = toHtml.ToHtml(buff); 607 auto htmlFromSpan = toHtml.ToHtml(*spanString); 608 EXPECT_EQ(htmlFromU8, htmlFromSpan); 609 610 /** 611 * @tc.steps4: Convert the HTML back to SpanString and verify that the items match. 612 * @tc.expected: The SpanString items after conversion should be identical to the original. 613 */ 614 HtmlToSpan toSpan; 615 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 616 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 617 618 /** 619 * @tc.steps5: Convert the SpanString back to HTML and verify consistency. 620 * @tc.expected: The final HTML should match the HTML obtained earlier. 621 */ 622 SpanToHtml toHtml1; 623 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 624 EXPECT_EQ(hmtlString, htmlFromSpan); 625 } 626 627 /** 628 * @tc.name: HtmlConvert004 629 * @tc.desc: This test case verifies the conversion of a SpanString with multiple complex styles 630 * (font, letter spacing, background color, and text shadow) applied to different 631 * parts of the text. It ensures the complex styles are applied correctly during conversion 632 * to TLV, HTML, and back to SpanString. 633 * @tc.level: 1 634 */ 635 HWTEST_F(HtmlConvertTestNg, HtmlConvert004, TestSize.Level1) 636 { 637 /** 638 * @tc.steps1: Create a SpanString with various complex styles applied (font, letter spacing, 639 * background color, text shadow). 640 * @tc.expected: All styles should be applied correctly to the specified ranges. 641 */ 642 auto spanString = AceType::MakeRefPtr<SpanString>(u"Hello World Test with Styles"); 643 644 // Add fonts, letter spacing, background color, and text shadow 645 spanString->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 5)); 646 spanString->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 6, 11)); 647 spanString->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(2), 12, 17)); 648 649 NG::BorderRadiusProperty borderRadius; 650 TextBackgroundStyle textBackgroundStyle; 651 textBackgroundStyle.backgroundColor = Color::GREEN; 652 textBackgroundStyle.backgroundRadius = borderRadius; 653 spanString->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle, 0, 5)); 654 655 Shadow shadow1; 656 shadow1.SetBlurRadius(5.0); 657 shadow1.SetColor(Color::BLACK); 658 shadow1.SetOffsetX(3.0); 659 shadow1.SetOffsetY(3.0); 660 spanString->AddSpan(AceType::MakeRefPtr<TextShadowSpan>(std::vector<Shadow>{shadow1}, 6, 11)); 661 662 /** 663 * @tc.steps2: Convert the SpanString to TLV format and ensure the result is not empty. 664 * @tc.expected: TLV format conversion should produce a non-empty result. 665 */ 666 std::vector<uint8_t> buff; 667 spanString->EncodeTlv(buff); 668 EXPECT_EQ(buff.size() > 0, true); 669 670 /** 671 * @tc.steps3: Convert the TLV format back to HTML and compare it with the direct HTML conversion 672 * of the SpanString. 673 * @tc.expected: The HTML results from both methods should be identical. 674 */ 675 SpanToHtml toHtml; 676 auto htmlFromU8 = toHtml.ToHtml(buff); 677 auto htmlFromSpan = toHtml.ToHtml(*spanString); 678 EXPECT_EQ(htmlFromU8, htmlFromSpan); 679 680 /** 681 * @tc.steps4: Convert the HTML back to SpanString and verify that the items match. 682 * @tc.expected: The SpanString items after conversion should be identical to the original. 683 */ 684 HtmlToSpan toSpan; 685 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 686 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 687 688 /** 689 * @tc.steps5: Convert the SpanString back to HTML and verify consistency. 690 * @tc.expected: The final HTML should match the HTML obtained earlier. 691 */ 692 SpanToHtml toHtml1; 693 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 694 EXPECT_EQ(hmtlString, htmlFromSpan); 695 } 696 697 /** 698 * @tc.name: HtmlConvert005 699 * @tc.desc: This test case checks the conversion of an empty SpanString. It ensures that an empty SpanString 700 * correctly converts to TLV and HTML formats without issues. 701 * @tc.level: 1 702 */ 703 HWTEST_F(HtmlConvertTestNg, HtmlConvert005, TestSize.Level1) 704 { 705 /** 706 * @tc.steps1: Create an empty SpanString. 707 * @tc.expected: The SpanString should remain empty and correctly handle conversion to TLV and HTML. 708 */ 709 auto spanString = AceType::MakeRefPtr<SpanString>(u""); 710 711 // Convert to TLV format 712 std::vector<uint8_t> buff; 713 spanString->EncodeTlv(buff); 714 EXPECT_EQ(buff.size(), 13); 715 716 /** 717 * @tc.steps2: Convert the TLV buffer to HTML. 718 * @tc.expected: The HTML string should match the result of directly converting the SpanString to HTML. 719 */ 720 SpanToHtml toHtml; 721 auto htmlFromU8 = toHtml.ToHtml(buff); 722 auto htmlFromSpan = toHtml.ToHtml(*spanString); 723 EXPECT_EQ(htmlFromU8, htmlFromSpan); 724 725 /** 726 * @tc.steps3: Convert the HTML back to SpanString and verify if it matches the original SpanString. 727 * @tc.expected: The SpanString obtained from the HTML should match the original SpanString. 728 */ 729 HtmlToSpan toSpan; 730 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 731 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 732 733 /** 734 * @tc.steps4: Convert back to HTML and verify that it matches the initial HTML result. 735 * @tc.expected: The final HTML should match the HTML obtained earlier. 736 */ 737 SpanToHtml toHtml1; 738 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 739 EXPECT_EQ(hmtlString, htmlFromSpan); 740 } 741 742 /** 743 * @tc.name: HtmlConvert006 744 * @tc.desc: This test case checks the conversion of a SpanString with overlapping styles applied in different ranges. 745 * It ensures that the overlapping styles are correctly maintained during the conversion process. 746 * @tc.level: 1 747 */ 748 HWTEST_F(HtmlConvertTestNg, HtmlConvert006, TestSize.Level1) 749 { 750 /** 751 * @tc.steps1: Create a SpanString with overlapping styles (FontSpans). 752 * @tc.expected: The SpanString should correctly handle overlapping styles and preserve them during conversion. 753 */ 754 auto spanString = AceType::MakeRefPtr<SpanString>(u"Hello World"); 755 spanString->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 5)); 756 spanString->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 3, 8)); 757 758 // Convert to TLV format 759 std::vector<uint8_t> buff; 760 spanString->EncodeTlv(buff); 761 EXPECT_EQ(buff.size() > 0, true); 762 763 /** 764 * @tc.steps2: Convert the TLV buffer to HTML and compare with direct SpanString to HTML conversion. 765 * @tc.expected: The converted HTML from TLV and direct SpanString should be identical. 766 */ 767 SpanToHtml toHtml; 768 auto htmlFromU8 = toHtml.ToHtml(buff); 769 auto htmlFromSpan = toHtml.ToHtml(*spanString); 770 EXPECT_EQ(htmlFromU8, htmlFromSpan); 771 772 /** 773 * @tc.steps3: Convert the HTML back to SpanString and validate that the spans are equivalent. 774 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 775 */ 776 HtmlToSpan toSpan; 777 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 778 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 779 780 /** 781 * @tc.steps4: Convert back to HTML and validate consistency with the initial HTML result. 782 * @tc.expected: The final HTML should match the initial HTML obtained from the SpanString. 783 */ 784 SpanToHtml toHtml1; 785 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 786 EXPECT_EQ(hmtlString, htmlFromSpan); 787 } 788 789 /** 790 * @tc.name: HtmlConvert007 791 * @tc.desc: This test case checks overlapping styles involving font spans and background color spans, 792 * ensuring that both styles are applied correctly and do not interfere with each other. 793 * @tc.level: 1 794 */ 795 HWTEST_F(HtmlConvertTestNg, HtmlConvert007, TestSize.Level1) 796 { 797 /** 798 * @tc.steps1: Add font spans and background color spans with overlapping ranges. 799 * @tc.expected: Both styles should be applied correctly without interference. 800 */ 801 auto spanString = AceType::MakeRefPtr<SpanString>(u"Overlapping styles test"); 802 spanString->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10)); 803 TextBackgroundStyle bgStyle; 804 bgStyle.backgroundColor = Color::GREEN; 805 spanString->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(bgStyle, 5, 15)); 806 807 /** 808 * @tc.steps2: Convert the SpanString to TLV format and check the result. 809 * @tc.expected: The TLV conversion should handle both font and background color styles correctly. 810 */ 811 std::vector<uint8_t> buff; 812 spanString->EncodeTlv(buff); 813 EXPECT_GT(buff.size(), 0); 814 815 /** 816 * @tc.steps3: Convert the SpanString to HTML format and validate the output. 817 * @tc.expected: The HTML conversion should match the expected HTML representation. 818 */ 819 SpanToHtml toHtml; 820 auto htmlFromSpan = toHtml.ToHtml(*spanString); 821 822 /** 823 * @tc.steps4: Convert the HTML back to SpanString and ensure consistency with the original SpanString. 824 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 825 */ 826 HtmlToSpan toSpan; 827 auto spanFromHtml = toSpan.ToSpanString(htmlFromSpan); 828 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 829 } 830 831 /** 832 * @tc.name: HtmlConvert008 833 * @tc.desc: This test case checks the behavior of overlapping letter spacing and text shadow spans in a `SpanString`. 834 * It ensures that both styles work together without interfering with each other. 835 * @tc.level: 1 836 */ 837 HWTEST_F(HtmlConvertTestNg, HtmlConvert008, TestSize.Level1) 838 { 839 /** 840 * @tc.steps1: Add letter spacing and text shadow spans with overlapping ranges. 841 * @tc.expected: Both letter spacing and shadow effects should be applied correctly and not interfere. 842 */ 843 auto spanString = AceType::MakeRefPtr<SpanString>(u"Letter spacing and shadow test"); 844 spanString->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(5), 0, 10)); 845 846 Shadow shadow1; 847 shadow1.SetBlurRadius(5.0); 848 shadow1.SetColor(Color::BLUE); 849 shadow1.SetOffsetX(2.0); 850 shadow1.SetOffsetY(2.0); 851 spanString->AddSpan(AceType::MakeRefPtr<TextShadowSpan>(std::vector<Shadow>{shadow1}, 5, 15)); 852 853 /** 854 * @tc.steps2: Convert the SpanString to TLV format and check the result. 855 * @tc.expected: The conversion should handle both letter spacing and shadow effects correctly. 856 */ 857 std::vector<uint8_t> buff; 858 spanString->EncodeTlv(buff); 859 EXPECT_GT(buff.size(), 0); 860 861 /** 862 * @tc.steps3: Convert the SpanString to HTML format and validate the result. 863 * @tc.expected: The HTML conversion should correctly apply both the letter spacing and text shadow. 864 */ 865 SpanToHtml toHtml; 866 auto htmlFromSpan = toHtml.ToHtml(*spanString); 867 868 /** 869 * @tc.steps4: Convert the HTML back to SpanString and validate if it matches the original SpanString. 870 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 871 */ 872 HtmlToSpan toSpan; 873 auto spanFromHtml = toSpan.ToSpanString(htmlFromSpan); 874 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 875 } 876 877 /** 878 * @tc.name: HtmlConvert009 879 * @tc.desc: This test case checks the overlapping of font spans and letter spacing spans in a `SpanString`. 880 * It ensures that both styles are applied correctly and do not interfere with each other. 881 * @tc.level: 1 882 */ 883 HWTEST_F(HtmlConvertTestNg, HtmlConvert009, TestSize.Level1) 884 { 885 /** 886 * @tc.steps1: Add font and letter spacing spans with overlapping ranges. 887 * @tc.expected: Both font and letter spacing should be applied correctly without any conflicts. 888 */ 889 auto spanString = AceType::MakeRefPtr<SpanString>(u"Overlapping font and letter spacing"); 890 spanString->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10)); 891 spanString->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(5), 5, 15)); 892 893 // Convert to TLV and HTML 894 std::vector<uint8_t> buff; 895 spanString->EncodeTlv(buff); 896 EXPECT_GT(buff.size(), 0); 897 898 /** 899 * @tc.steps2: Convert the SpanString to HTML format and check for correctness. 900 * @tc.expected: The HTML conversion should apply both the font and letter spacing styles correctly. 901 */ 902 SpanToHtml toHtml; 903 auto htmlFromSpan = toHtml.ToHtml(*spanString); 904 905 /** 906 * @tc.steps3: Convert the HTML back to SpanString and verify if the spans are correctly restored. 907 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 908 */ 909 HtmlToSpan toSpan; 910 auto spanFromHtml = toSpan.ToSpanString(htmlFromSpan); 911 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 912 } 913 914 /** 915 * @tc.name: HtmlConvert010 916 * @tc.desc: This test case checks the overlapping of background color spans and text shadow spans in a `SpanString`. 917 * It ensures that both styles are applied correctly without interference. 918 * @tc.level: 1 919 */ 920 HWTEST_F(HtmlConvertTestNg, HtmlConvert010, TestSize.Level1) 921 { 922 /** 923 * @tc.steps1: Add background color and text shadow spans with overlapping ranges. 924 * @tc.expected: Both background color and text shadow should be applied correctly without any conflicts. 925 */ 926 auto spanString = AceType::MakeRefPtr<SpanString>(u"Overlapping background and shadow styles"); 927 NG::BorderRadiusProperty borderRadius; 928 TextBackgroundStyle textBackgroundStyle; 929 textBackgroundStyle.backgroundColor = Color::WHITE; 930 textBackgroundStyle.backgroundRadius = borderRadius; 931 spanString->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle, 0, 10)); 932 933 Shadow shadow1; 934 shadow1.SetBlurRadius(3.0); 935 shadow1.SetColor(Color::BLACK); 936 shadow1.SetOffsetX(1.0); 937 shadow1.SetOffsetY(1.0); 938 spanString->AddSpan(AceType::MakeRefPtr<TextShadowSpan>(std::vector<Shadow>{shadow1}, 5, 15)); 939 940 // Convert to TLV and HTML 941 std::vector<uint8_t> buff; 942 spanString->EncodeTlv(buff); 943 EXPECT_GT(buff.size(), 0); 944 945 /** 946 * @tc.steps2: Convert the SpanString to HTML and check for correctness. 947 * @tc.expected: The HTML conversion should correctly reflect both background color and text shadow styles. 948 */ 949 SpanToHtml toHtml; 950 auto htmlFromSpan = toHtml.ToHtml(*spanString); 951 952 /** 953 * @tc.steps3: Convert the HTML back to SpanString and validate if it matches the original SpanString. 954 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 955 */ 956 HtmlToSpan toSpan; 957 auto spanFromHtml = toSpan.ToSpanString(htmlFromSpan); 958 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 959 } 960 961 /** 962 * @tc.name: HtmlConvert011 963 * @tc.desc: This test case checks the overlapping of font spans, letter spacing spans, and background color spans. 964 * It ensures that all styles are applied correctly without any conflicts between them. 965 * @tc.level: 1 966 */ 967 HWTEST_F(HtmlConvertTestNg, HtmlConvert011, TestSize.Level1) 968 { 969 /** 970 * @tc.steps1: Add font, letter spacing, and background color spans with overlapping ranges. 971 * @tc.expected: All three styles (font, letter spacing, and background color) should be applied correctly. 972 */ 973 auto spanString = AceType::MakeRefPtr<SpanString>(u"Overlapping font, letter spacing, and background styles"); 974 spanString->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10)); 975 spanString->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(4), 5, 15)); 976 977 NG::BorderRadiusProperty borderRadius; 978 TextBackgroundStyle textBackgroundStyle; 979 textBackgroundStyle.backgroundColor = Color::RED; 980 textBackgroundStyle.backgroundRadius = borderRadius; 981 spanString->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle, 0, 10)); 982 983 // Convert to TLV and HTML 984 std::vector<uint8_t> buff; 985 spanString->EncodeTlv(buff); 986 EXPECT_GT(buff.size(), 0); 987 988 /** 989 * @tc.steps2: Convert the SpanString to HTML and verify the result. 990 * @tc.expected: The HTML conversion should correctly reflect all three styles 991 * (font, letter spacing, and background). 992 */ 993 SpanToHtml toHtml; 994 auto htmlFromSpan = toHtml.ToHtml(*spanString); 995 996 /** 997 * @tc.steps3: Convert the HTML back to SpanString and validate if it matches the original SpanString. 998 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 999 */ 1000 HtmlToSpan toSpan; 1001 auto spanFromHtml = toSpan.ToSpanString(htmlFromSpan); 1002 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 1003 } 1004 1005 /** 1006 * @tc.name: HtmlConvert012 1007 * @tc.desc: This test case checks the conversion of a SpanString containing English and Chinese. 1008 * It ensures these characters are handled properly in both TLV and HTML conversions. 1009 * @tc.level: 1 1010 */ 1011 HWTEST_F(HtmlConvertTestNg, HtmlConvert012, TestSize.Level1) 1012 { 1013 /** 1014 * @tc.steps1: Create a SpanString with English and Chinese. 1015 * @tc.expected: The SpanString should correctly preserve and handle these special characters during conversion. 1016 */ 1017 auto spanString = AceType::MakeRefPtr<SpanString>(u"Hello World, Friends 你好,世界,朋友们。"); 1018 1019 // Convert to TLV format 1020 std::vector<uint8_t> buff; 1021 spanString->EncodeTlv(buff); 1022 EXPECT_EQ(buff.size() > 0, true); 1023 1024 /** 1025 * @tc.steps2: Convert the TLV buffer to HTML and compare it with the result from SpanString conversion. 1026 * @tc.expected: The HTML string should match the result of directly converting the SpanString with 1027 * English and Chinese. 1028 */ 1029 SpanToHtml toHtml; 1030 auto htmlFromU8 = toHtml.ToHtml(buff); 1031 auto htmlFromSpan = toHtml.ToHtml(*spanString); 1032 EXPECT_EQ(htmlFromU8, htmlFromSpan); 1033 1034 /** 1035 * @tc.steps3: Convert the HTML back to SpanString and check if it matches the original SpanString. 1036 * @tc.expected: The SpanString obtained from the HTML should match the original SpanString. 1037 */ 1038 HtmlToSpan toSpan; 1039 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 1040 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 1041 1042 /** 1043 * @tc.steps4: Convert the final SpanString back to HTML and ensure consistency with the initial HTML. 1044 * @tc.expected: The final HTML should match the HTML obtained before the conversion to SpanString. 1045 */ 1046 SpanToHtml toHtml1; 1047 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 1048 EXPECT_EQ(hmtlString, htmlFromSpan); 1049 } 1050 1051 /** 1052 * @tc.name: HtmlConvert013 1053 * @tc.desc: This test case ensures that containing English and Chinese in a `SpanString` 1054 * are properly encoded and decoded during the conversion process. 1055 * @tc.level: 1 1056 */ 1057 HWTEST_F(HtmlConvertTestNg, HtmlConvert013, TestSize.Level1) 1058 { 1059 /** 1060 * @tc.steps1: Create a SpanString with special characters and encode it. 1061 * @tc.expected: The special characters should be properly encoded and decoded in HTML. 1062 */ 1063 auto spanString = AceType::MakeRefPtr<SpanString>( 1064 u"Hello World Friends!\n你好,世界,朋友们。\nTest!测试!SpanString"); 1065 1066 // Convert to TLV and HTML 1067 std::vector<uint8_t> buff; 1068 spanString->EncodeTlv(buff); 1069 EXPECT_GT(buff.size(), 0); 1070 1071 /** 1072 * @tc.steps2: Convert the TLV buffer to HTML format and verify the result. 1073 * @tc.expected: The HTML conversion should correctly encode the special characters. 1074 */ 1075 SpanToHtml toHtml; 1076 auto htmlFromSpan = toHtml.ToHtml(*spanString); 1077 1078 /** 1079 * @tc.steps3: Convert the HTML back to SpanString and check for correctness. 1080 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 1081 */ 1082 HtmlToSpan toSpan; 1083 auto spanFromHtml = toSpan.ToSpanString(htmlFromSpan); 1084 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 1085 1086 /** 1087 * @tc.steps4: Convert the final SpanString back to HTML and ensure it matches the initial HTML. 1088 * @tc.expected: The final HTML should match the original HTML obtained from the SpanString. 1089 */ 1090 SpanToHtml toHtml1; 1091 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 1092 EXPECT_EQ(hmtlString, htmlFromSpan); 1093 } 1094 1095 /** 1096 * @tc.name: HtmlConvert014 1097 * @tc.desc: This test case ensures that newline and tab characters in a `SpanString` are 1098 * correctly processed during conversion. 1099 * @tc.level: 1 1100 */ 1101 HWTEST_F(HtmlConvertTestNg, HtmlConvert014, TestSize.Level1) 1102 { 1103 /** 1104 * @tc.steps1: Create a SpanString with newline and tab characters and verify proper conversion. 1105 * @tc.expected: Newline and tab characters should be retained correctly in the conversion process. 1106 */ 1107 auto spanString = AceType::MakeRefPtr<SpanString>(u"Hello\tWorld!\nThis is a test."); 1108 1109 // Convert to TLV and HTML 1110 std::vector<uint8_t> buff; 1111 spanString->EncodeTlv(buff); 1112 EXPECT_GT(buff.size(), 0); 1113 1114 /** 1115 * @tc.steps2: Convert the TLV buffer to HTML format and verify the result. 1116 * @tc.expected: The HTML conversion should correctly encode the newline and tab characters. 1117 */ 1118 SpanToHtml toHtml; 1119 auto htmlFromSpan = toHtml.ToHtml(*spanString); 1120 1121 /** 1122 * @tc.steps3: Convert the HTML back to SpanString and check for correctness. 1123 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 1124 */ 1125 HtmlToSpan toSpan; 1126 auto spanFromHtml = toSpan.ToSpanString(htmlFromSpan); 1127 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 1128 1129 /** 1130 * @tc.steps4: Convert the final SpanString back to HTML and ensure it matches the initial HTML. 1131 * @tc.expected: The final HTML should match the original HTML obtained from the SpanString. 1132 */ 1133 SpanToHtml toHtml1; 1134 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 1135 EXPECT_EQ(hmtlString, htmlFromSpan); 1136 } 1137 1138 /** 1139 * @tc.name: HtmlConvert015 1140 * @tc.desc: This test case checks the conversion of a SpanString with long text content. 1141 * It ensures that long text is correctly handled in both TLV and HTML conversions. 1142 * @tc.level: 1 1143 */ 1144 HWTEST_F(HtmlConvertTestNg, HtmlConvert015, TestSize.Level1) 1145 { 1146 /** 1147 * @tc.steps1: Create a SpanString with a long text (over 1000 characters). 1148 * @tc.expected: The SpanString with long text should be handled properly during 1149 * conversion to TLV and HTML formats. 1150 */ 1151 std::u16string longText = u"Hello World! " + std::u16string(1000, u'X') + u" Test with long text."; 1152 auto spanString = AceType::MakeRefPtr<SpanString>(longText); 1153 1154 // Convert to TLV format 1155 std::vector<uint8_t> buff; 1156 spanString->EncodeTlv(buff); 1157 EXPECT_EQ(buff.size() > 0, true); 1158 1159 /** 1160 * @tc.steps2: Convert the TLV buffer to HTML and compare with the direct SpanString to HTML conversion. 1161 * @tc.expected: The HTML obtained from TLV and from SpanString should be identical. 1162 */ 1163 SpanToHtml toHtml; 1164 auto htmlFromU8 = toHtml.ToHtml(buff); 1165 auto htmlFromSpan = toHtml.ToHtml(*spanString); 1166 EXPECT_EQ(htmlFromU8, htmlFromSpan); 1167 1168 /** 1169 * @tc.steps3: Convert the HTML back to SpanString and verify consistency with the original SpanString. 1170 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 1171 */ 1172 HtmlToSpan toSpan; 1173 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 1174 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 1175 1176 /** 1177 * @tc.steps4: Convert the final SpanString back to HTML and check for consistency with the original HTML. 1178 * @tc.expected: The final HTML should match the initial HTML result. 1179 */ 1180 SpanToHtml toHtml1; 1181 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 1182 EXPECT_EQ(hmtlString, htmlFromSpan); 1183 } 1184 1185 /** 1186 * @tc.name: HtmlConvert016 1187 * @tc.desc: This test case checks how `SpanString` handles very long text inputs and ensures that the conversion 1188 * process still works efficiently with long texts. 1189 * @tc.level: 1 1190 */ 1191 HWTEST_F(HtmlConvertTestNg, HtmlConvert016, TestSize.Level1) 1192 { 1193 /** 1194 * @tc.steps1: Create a long text string and verify its conversion to TLV and HTML formats. 1195 * @tc.expected: Long text should be handled without errors, and conversion should retain all data. 1196 */ 1197 std::u16string longText = u"Long text testing " + std::u16string(5000, u'X'); 1198 auto spanString = AceType::MakeRefPtr<SpanString>(longText); 1199 1200 // Convert to TLV and HTML 1201 std::vector<uint8_t> buff; 1202 spanString->EncodeTlv(buff); 1203 EXPECT_GT(buff.size(), 0); 1204 1205 /** 1206 * @tc.steps2: Convert the TLV buffer to HTML format and verify the result. 1207 * @tc.expected: The HTML conversion should correctly handle long text. 1208 */ 1209 SpanToHtml toHtml; 1210 auto htmlFromSpan = toHtml.ToHtml(*spanString); 1211 1212 /** 1213 * @tc.steps3: Convert the HTML back to SpanString and check for correctness. 1214 * @tc.expected: The SpanString obtained from HTML should match the original SpanString. 1215 */ 1216 HtmlToSpan toSpan; 1217 auto spanFromHtml = toSpan.ToSpanString(htmlFromSpan); 1218 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString->GetSpanItems()), true); 1219 1220 /** 1221 * @tc.steps4: Convert the final SpanString back to HTML and ensure it matches the initial HTML. 1222 * @tc.expected: The final HTML should match the original HTML obtained from the SpanString. 1223 */ 1224 SpanToHtml toHtml1; 1225 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 1226 EXPECT_EQ(hmtlString, htmlFromSpan); 1227 } 1228 1229 /** 1230 * @tc.name: HtmlConvert017 1231 * @tc.desc: This test case checks the conversion of a SpanString that includes nested SpanStrings. 1232 * It ensures that nested spans are correctly handled and converted between formats. 1233 * @tc.level: 1 1234 */ 1235 HWTEST_F(HtmlConvertTestNg, HtmlConvert017, TestSize.Level1) 1236 { 1237 /** 1238 * @tc.steps1: Create a SpanString that contains other SpanStrings (nested). 1239 * @tc.expected: The nested SpanStrings should be correctly handled and maintained during conversion. 1240 */ 1241 auto spanString1 = AceType::MakeRefPtr<SpanString>(u"Hello"); 1242 1243 // Convert to TLV format 1244 std::vector<uint8_t> buff; 1245 spanString1->EncodeTlv(buff); 1246 EXPECT_EQ(buff.size() > 0, true); 1247 1248 /** 1249 * @tc.steps2: Convert the TLV buffer to HTML and compare with the direct SpanString to HTML conversion. 1250 * @tc.expected: The HTML string should match the result of converting the nested SpanString directly to HTML. 1251 */ 1252 SpanToHtml toHtml; 1253 auto htmlFromU8 = toHtml.ToHtml(buff); 1254 auto htmlFromSpan = toHtml.ToHtml(*spanString1); 1255 EXPECT_EQ(htmlFromU8, htmlFromSpan); 1256 1257 /** 1258 * @tc.steps3: Convert the HTML back to SpanString and verify if it matches the original nested SpanString. 1259 * @tc.expected: The SpanString obtained from HTML should match the original nested SpanString. 1260 */ 1261 HtmlToSpan toSpan; 1262 auto spanFromHtml = toSpan.ToSpanString(htmlFromU8); 1263 EXPECT_EQ(IsSpanItemSame(spanFromHtml->GetSpanItems(), spanString1->GetSpanItems()), true); 1264 1265 /** 1266 * @tc.steps4: Convert the final SpanString back to HTML and check if it matches the initial HTML. 1267 * @tc.expected: The final HTML should match the initial HTML obtained from the nested SpanString. 1268 */ 1269 SpanToHtml toHtml1; 1270 auto hmtlString = toHtml1.ToHtml(*spanFromHtml); 1271 EXPECT_EQ(hmtlString, htmlFromSpan); 1272 } 1273 1274 /** 1275 * @tc.name: HtmlConverter001 1276 * @tc.desc: This test case checks the conversion of a span string with a font-family property applied. 1277 * It verifies that the font-family attribute is correctly parsed and converted into a SpanItem. 1278 * @tc.level: 1 1279 */ 1280 HWTEST_F(HtmlConvertTestNg, HtmlConverter001, TestSize.Level1) 1281 { 1282 /** 1283 * @tc.steps1: Create a multiHtml string containing a <p> tag with a font-family property. 1284 * Convert it to SpanString and retrieve the font-family from the span item. 1285 * @tc.expected: The font-family property should be correctly converted and retrieved. 1286 */ 1287 const std::string multiHtml = "<html>" 1288 "<body>" 1289 "<p style=\"font-family:'Times New Roman', serif;\">Times New Roman 字体</p>" 1290 "</body>" 1291 "</html>"; 1292 HtmlToSpan toSpan; 1293 auto dstSpan = toSpan.ToSpanString(multiHtml); 1294 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1295 EXPECT_EQ(spans.size(), 1); 1296 } 1297 1298 /** 1299 * @tc.name: HtmlConverter002 1300 * @tc.desc: This test case checks the conversion of a span string with font-size and letter-spacing applied. 1301 * It ensures that the font size and letter spacing are correctly converted into SpanItem properties. 1302 * @tc.level: 1 1303 */ 1304 HWTEST_F(HtmlConvertTestNg, HtmlConverter002, TestSize.Level1) 1305 { 1306 /** 1307 * @tc.steps1: Create a multiHtml string containing a <p> tag with font-size and letter-spacing properties. 1308 * Convert it to SpanString and retrieve the font size and letter spacing from the span item. 1309 * @tc.expected: The font-size and letter-spacing properties should be correctly converted and retrieved. 1310 */ 1311 const std::string multiHtml = "<html>" 1312 "<body>" 1313 "<p style=\"font-size:18px;letter-spacing:2px;\">带字间距的文字</p>" 1314 "</body>" 1315 "</html>"; 1316 HtmlToSpan toSpan; 1317 auto dstSpan = toSpan.ToSpanString(multiHtml); 1318 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1319 EXPECT_EQ(spans.size(), 1); 1320 auto it = spans.begin(); 1321 EXPECT_EQ((*it)->fontStyle->GetFontSize().value(), Dimension(18, DimensionUnit::VP)); 1322 EXPECT_EQ((*it)->fontStyle->GetLetterSpacing().value(), Dimension(2, DimensionUnit::VP)); 1323 } 1324 1325 /** 1326 * @tc.name: HtmlConverter003 1327 * @tc.desc: This test case checks the conversion of a span string with text-decoration and color applied. 1328 * It verifies that the text-decoration (line-through) and text color are correctly 1329 * converted into SpanItem properties. 1330 * @tc.level: 1 1331 */ 1332 HWTEST_F(HtmlConvertTestNg, HtmlConverter003, TestSize.Level1) 1333 { 1334 /** 1335 * @tc.steps1: Create a multiHtml string containing a <p> tag with text-decoration and color properties. 1336 * Convert it to SpanString and retrieve the text-decoration and text color from the span item. 1337 * @tc.expected: The text-decoration (line-through) and text color (blue) properties should be 1338 * correctly converted and retrieved. 1339 */ 1340 const std::string multiHtml = "<html>" 1341 "<body>" 1342 "<p style=\"text-decoration:line-through;color:blue;\">带删除线的蓝色文字</p>" 1343 "</body>" 1344 "</html>"; 1345 HtmlToSpan toSpan; 1346 auto dstSpan = toSpan.ToSpanString(multiHtml); 1347 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1348 EXPECT_EQ(spans.size(), 1); 1349 auto it = spans.begin(); 1350 EXPECT_EQ((*it)->fontStyle->GetTextDecoration().value(), TextDecoration::LINE_THROUGH); 1351 } 1352 1353 /** 1354 * @tc.name: HtmlConverter004 1355 * @tc.desc: This test case checks the conversion of a span string with font-size, color, 1356 * and text-decoration properties applied. 1357 * It ensures that all three properties are correctly converted into SpanItem properties. 1358 * @tc.level: 1 1359 */ 1360 HWTEST_F(HtmlConvertTestNg, HtmlConverter004, TestSize.Level1) 1361 { 1362 /** 1363 * @tc.steps1: Create a multiHtml string containing a <p> tag with font-size, color, 1364 * and text-decoration properties. Convert it to SpanString and retrieve 1365 * the font size, text color, and text-decoration from the span item. 1366 * @tc.expected: The font-size (16px), text color (red), and text-decoration (underline) 1367 * properties should be correctly converted and retrieved. 1368 */ 1369 const std::string multiHtml = "<html>" 1370 "<body>" 1371 "<p style=\"font-size:16px;color:red;text-decoration:underline;\">" 1372 "带下划线的红色小字体</p>" 1373 "</body>" 1374 "</html>"; 1375 HtmlToSpan toSpan; 1376 auto dstSpan = toSpan.ToSpanString(multiHtml); 1377 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1378 EXPECT_EQ(spans.size(), 1); 1379 auto it = spans.begin(); 1380 EXPECT_EQ((*it)->fontStyle->GetFontSize().value(), Dimension(16, DimensionUnit::VP)); 1381 EXPECT_EQ((*it)->fontStyle->GetTextColor().value(), OHOS::Ace::Color::RED); 1382 } 1383 1384 /** 1385 * @tc.name: HtmlConverter005 1386 * @tc.desc: This test case checks the conversion of a span string with font-weight and font-style properties applied. 1387 * It ensures that both the font-weight (bold) and font-style (italic) are correctly converted into SpanItem. 1388 * @tc.level: 1 1389 */ 1390 HWTEST_F(HtmlConvertTestNg, HtmlConverter005, TestSize.Level1) 1391 { 1392 /** 1393 * @tc.steps1: Create a multiHtml string containing a <p> tag with font-weight (bold) and font-style (italic). 1394 * Convert it to SpanString and retrieve the font-weight and font-style from the span item. 1395 * @tc.expected: The font-weight (bold) and font-style (italic) should be correctly converted and retrieved. 1396 */ 1397 const std::string multiHtml = "<html>" 1398 "<body>" 1399 "<p style=\"font-weight:bold;font-style:italic;\">加粗斜体文字</p>" 1400 "</body>" 1401 "</html>"; 1402 HtmlToSpan toSpan; 1403 auto dstSpan = toSpan.ToSpanString(multiHtml); 1404 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1405 EXPECT_EQ(spans.size(), 1); 1406 auto it = spans.begin(); 1407 EXPECT_TRUE((*it)->fontStyle->GetFontWeight().has_value()); 1408 EXPECT_EQ((*it)->fontStyle->GetFontWeight().value(), FontWeight::BOLD); 1409 EXPECT_TRUE((*it)->fontStyle->GetItalicFontStyle().has_value()); 1410 } 1411 1412 /** 1413 * @tc.name: HtmlConverter006 1414 * @tc.desc: This test case checks the conversion of a span string with nested font-size properties. 1415 * It ensures that the correct font-size is applied for each nested span element. 1416 * @tc.level: 1 1417 */ 1418 HWTEST_F(HtmlConvertTestNg, HtmlConverter006, TestSize.Level1) 1419 { 1420 /** 1421 * @tc.steps1: Create a multiHtml string containing a <p> tag with nested <span> tags have different font-size. 1422 * Convert it to SpanString and retrieve the font-size for each span item. 1423 * @tc.expected: The nested <span> tags should correctly apply the respective font-size (100px and 50px). 1424 */ 1425 const std::string multiHtml = "<html>" 1426 "<body>" 1427 "<p style=\"font-size:50px\"><span style=\"font-size:100px\">100px</span>50px</p>" 1428 "</body>" 1429 "</html>"; 1430 HtmlToSpan toSpan; 1431 auto dstSpan = toSpan.ToSpanString(multiHtml); 1432 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1433 EXPECT_EQ(spans.size(), 2); 1434 auto it = spans.begin(); 1435 EXPECT_EQ((*it)->fontStyle->GetFontSize().value(), Dimension(50, DimensionUnit::VP)); 1436 } 1437 1438 /** 1439 * @tc.name: HtmlConverter007 1440 * @tc.desc: This test case checks the conversion of a span string with font-size and text-shadow properties applied. 1441 * It ensures that both the font-size and text-shadow are correctly converted into SpanItem properties. 1442 * @tc.level: 1 1443 */ 1444 HWTEST_F(HtmlConvertTestNg, HtmlConverter007, TestSize.Level1) 1445 { 1446 /** 1447 * @tc.steps1: Create a multiHtml string containing a <p> tag with font-size and text-shadow properties. 1448 * Convert it to SpanString and retrieve the font-size and text-shadow from the span item. 1449 * @tc.expected: The font-size (50px) and text-shadow (red and green shadows) should be correctly converted. 1450 */ 1451 const std::string multiHtml = "<html>" 1452 "<body>" 1453 "<p style=\"font-size:50px;text-shadow:0 0 3px red, green 0 0;\">" 1454 "带有字体大小和阴影的文字</p>" 1455 "</body>" 1456 "</html>"; 1457 HtmlToSpan toSpan; 1458 auto dstSpan = toSpan.ToSpanString(multiHtml); 1459 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1460 EXPECT_EQ(spans.size(), 1); 1461 auto it = spans.begin(); 1462 EXPECT_EQ((*it)->fontStyle->GetFontSize().value(), Dimension(50, DimensionUnit::VP)); 1463 EXPECT_EQ((*it)->fontStyle->GetTextShadow().value()[0].GetColor(), OHOS::Ace::Color::RED); 1464 } 1465 1466 /** 1467 * @tc.name: HtmlConverter008 1468 * @tc.desc: This test case checks the conversion of HTML with multiple inline styles 1469 * applied to various elements like `span`, `strong`, and `em`, ensuring correct styling. 1470 * @tc.level: 1 1471 */ 1472 HWTEST_F(HtmlConvertTestNg, HtmlConverter008, TestSize.Level1) 1473 { 1474 /** 1475 * @tc.steps1: Create a complex HTML with multiple inline styles, such as `em`, and `span`, 1476 * to check the proper conversion to span strings. 1477 * @tc.expected: All inline styles such as font weight, font style, and color should be applied correctly. 1478 */ 1479 const std::string multiHtml = "<html>" 1480 "<body>" 1481 "<p><strong>Text</strong> and " 1482 "<em style=\"font-style:italic; color:blue;\">Italic Blue</em> and " 1483 "<span style=\"font-size:14px; color:green;\">Green 14px</span></p>" 1484 "</body>" 1485 "</html>"; 1486 HtmlToSpan toSpan; 1487 auto dstSpan = toSpan.ToSpanString(multiHtml); 1488 1489 /** 1490 * @tc.steps2: Verify the size of the converted `SpanString`. It should have 5 span items. 1491 * @tc.expected: The size of the spans list should be 5. 1492 */ 1493 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1494 EXPECT_EQ(spans.size(), 5); 1495 1496 /** 1497 * @tc.steps3: Verify the first span item, which should have a bold font weight. 1498 * @tc.expected: The first span item should have font weight set to bold. 1499 */ 1500 auto it = spans.begin(); 1501 EXPECT_TRUE((*it)->fontStyle->GetFontWeight().has_value()); 1502 EXPECT_EQ((*it)->fontStyle->GetFontWeight().value(), FontWeight::BOLD); 1503 1504 /** 1505 * @tc.steps4: Verify the third span item, which should have an italic font style and blue color. 1506 * @tc.expected: The third span item should have italic font style and color set to blue. 1507 */ 1508 ++it; 1509 ++it; 1510 EXPECT_TRUE((*it)->fontStyle->GetTextColor().has_value()); 1511 EXPECT_EQ((*it)->fontStyle->GetTextColor().value(), Color::BLUE); 1512 EXPECT_TRUE((*it)->fontStyle->GetItalicFontStyle().has_value()); 1513 EXPECT_EQ((*it)->fontStyle->GetItalicFontStyle().value(), Ace::FontStyle::ITALIC); 1514 1515 /** 1516 * @tc.steps5: Verify the fifth span item, which should have a font size of 14px and green color. 1517 * @tc.expected: The fifth span item should have font size set to 14px and color set to green. 1518 */ 1519 ++it; 1520 ++it; 1521 EXPECT_TRUE((*it)->fontStyle->GetTextColor().has_value()); 1522 EXPECT_EQ((*it)->fontStyle->GetTextColor().value(), Color::GREEN); 1523 EXPECT_TRUE((*it)->fontStyle->GetFontSize().has_value()); 1524 EXPECT_EQ((*it)->fontStyle->GetFontSize().value(), Dimension(14, DimensionUnit::VP)); 1525 } 1526 1527 /** 1528 * @tc.name: HtmlConverter009 1529 * @tc.desc: This test case checks the conversion of HTML with nested `span` tags with 1530 * varying `text-shadow` and `background-color`. 1531 * @tc.level: 1 1532 */ 1533 HWTEST_F(HtmlConvertTestNg, HtmlConverter009, TestSize.Level1) 1534 { 1535 /** 1536 * @tc.steps1: Create a nested HTML structure with `span` elements having `text-shadow` and `background-color`. 1537 * @tc.expected: Nested `span` tags should preserve both text-shadow and background color styles correctly. 1538 */ 1539 const std::string multiHtml = "<html>" 1540 "<body>" 1541 "<p>" 1542 "<span style=\"text-shadow:2px 2px 4px #000000; background-color:#ffcc00;\">" 1543 "Shadowed and Colored Text</span></p>" 1544 "</body>" 1545 "</html>"; 1546 HtmlToSpan toSpan; 1547 auto dstSpan = toSpan.ToSpanString(multiHtml); 1548 1549 /** 1550 * @tc.steps2: Verify the size of the converted `SpanString`. It have 1 span item for the single `span` tag. 1551 * @tc.expected: The size of the spans list should be 1. 1552 */ 1553 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1554 EXPECT_EQ(spans.size(), 1); 1555 1556 /** 1557 * @tc.steps3: Verify the text shadow applied to the span. It should have a shadow with the specified parameters. 1558 * @tc.expected: The text-shadow should have a 2px offset, 4px blur, and black color. 1559 */ 1560 auto it = spans.begin(); 1561 EXPECT_EQ((*it)->fontStyle->GetTextShadow().value()[0].GetColor(), OHOS::Ace::Color::BLACK); 1562 } 1563 1564 /** 1565 * @tc.name: HtmlConverter010 1566 * @tc.desc: This test case checks the conversion of HTML with `div` elements having 1567 * padding, margin, and background color. 1568 * @tc.level: 1 1569 */ 1570 HWTEST_F(HtmlConvertTestNg, HtmlConverter010, TestSize.Level1) 1571 { 1572 /** 1573 * @tc.steps1: Create a `div` element with padding, margin, and background color. 1574 * @tc.expected: Padding, margin, and background color styles should be correctly applied to the div content. 1575 */ 1576 const std::string multiHtml = "<html>" 1577 "<body>" 1578 "<div style=\"padding:10px; margin:5px; background-color:#f4f4f4;\">" 1579 "Styled Div Content</div>" 1580 "</body>" 1581 "</html>"; 1582 HtmlToSpan toSpan; 1583 auto dstSpan = toSpan.ToSpanString(multiHtml); 1584 1585 /** 1586 * @tc.steps2: Verify the size of the converted `SpanString`. It should have 1 span item for the `div` tag. 1587 * @tc.expected: The size of the spans list should be 1. 1588 */ 1589 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1590 EXPECT_EQ(spans.size(), 1); 1591 } 1592 1593 1594 /** 1595 * @tc.name: HtmlConverter011 1596 * @tc.desc: This test case checks the conversion of HTML containing a paragraph with a red text color. 1597 * It ensures that the correct color is applied to the text in the resulting SpanItem. 1598 * @tc.level: 1 1599 */ 1600 HWTEST_F(HtmlConvertTestNg, HtmlConverter011, TestSize.Level1) 1601 { 1602 /** 1603 * @tc.steps1: Initialize a multi-line HTML string with a paragraph containing red text color. 1604 * Convert the HTML to SpanString and verify the text color. 1605 * @tc.expected: The text color of the span should be red. 1606 */ 1607 const std::string multiHtml = "<html>" 1608 "<body>" 1609 "<p style=\"color:red;\">dddd当地经的123456</p>" 1610 "</body>" 1611 "</html>"; 1612 HtmlToSpan toSpan; 1613 auto dstSpan = toSpan.ToSpanString(multiHtml); 1614 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1615 EXPECT_EQ(spans.size(), 1); 1616 auto it = spans.begin(); 1617 EXPECT_EQ((*it)->fontStyle->GetTextColor().value(), OHOS::Ace::Color::RED); 1618 } 1619 1620 1621 /** 1622 * @tc.name: HtmlConverter012 1623 * @tc.desc: This test case checks the conversion of a SpanString with basic spanParagraphStyle. 1624 * It ensures that the span string is correctly converted to HTML with the specified styles. 1625 * @tc.level: 1 1626 */ 1627 HWTEST_F(HtmlConvertTestNg, HtmlConverter012, TestSize.Level1) 1628 { 1629 auto spanString = AceType::MakeRefPtr<SpanString>(u"段落标题\n正文第一段开始"); 1630 SpanParagraphStyle spanParagraphStyle; 1631 spanParagraphStyle.align = TextAlign::CENTER; 1632 1633 // default max lines 4 1634 spanParagraphStyle.maxLines = 4; 1635 spanParagraphStyle.wordBreak = WordBreak::BREAK_ALL; 1636 spanParagraphStyle.textOverflow = TextOverflow::ELLIPSIS; 1637 1638 // defalut textIndent 23 1639 spanParagraphStyle.textIndent = Dimension(23.0_vp); 1640 spanParagraphStyle.leadingMargin = LeadingMargin(); 1641 1642 // default width 25.0 height 26.0 1643 spanParagraphStyle.leadingMargin->size = LeadingMarginSize(Dimension(25.0_vp), Dimension(26.0)); 1644 auto paragraphStyle = AceType::MakeRefPtr<ParagraphStyleSpan>(spanParagraphStyle, 0, 5); 1645 spanString->AddSpan(paragraphStyle); 1646 1647 SpanToHtml convert; 1648 auto out = convert.ToHtml(*spanString); 1649 std::string result = 1650 "<div ><p style=\"text-align: center;text-indent: 23.00px;word-break: break_all;text-overflow: ellipsis;\">" 1651 "<span style=\"font-size: 16.00px;font-style: normal;font-weight: normal;color: #000000FF;font-family: " 1652 "HarmonyOS Sans;\">段落标题</span></p><span style=\"font-size: 16.00px;font-style: normal;font-weight: " 1653 "normal;color: #000000FF;font-family: HarmonyOS Sans;\">正文第一段开始</span></div>"; 1654 EXPECT_EQ(out, result); 1655 } 1656 1657 /** 1658 * @tc.name: HtmlConverter013 1659 * @tc.desc: This test case checks the conversion of a SpanString with spanString. 1660 * It ensures that the span string is correctly converted to HTML with the specified styles. 1661 * @tc.level: 1 1662 */ 1663 HWTEST_F(HtmlConvertTestNg, HtmlConverter013, TestSize.Level1) 1664 { 1665 auto spanString = AceType::MakeRefPtr<SpanString>(u"向上到顶适中向下到底"); 1666 spanString->AddSpan(AceType::MakeRefPtr<BaselineOffsetSpan>(Dimension(20.0_vp), 0, 4)); 1667 spanString->AddSpan(AceType::MakeRefPtr<BaselineOffsetSpan>(Dimension(10.0_vp), 4, 6)); 1668 1669 SpanToHtml convert; 1670 auto out = convert.ToHtml(*spanString); 1671 std::string result = 1672 "<div ><span style=\"font-size: 16.00px;font-style: normal;font-weight: normal;color: #000000FF;font-family: " 1673 "HarmonyOS Sans;vertical-align: 20.00px;\">向上到顶</span><span style=\"font-size: 16.00px;font-style: " 1674 "normal;font-weight: normal;color: #000000FF;font-family: HarmonyOS Sans;vertical-align: " 1675 "10.00px;\">适中</span><span style=\"font-size: 16.00px;font-style: normal;font-weight: normal;color: " 1676 "#000000FF;font-family: HarmonyOS Sans;\">向下到底</span></div>"; 1677 EXPECT_EQ(out, result); 1678 } 1679 1680 /** 1681 * @tc.name: HtmlConverter014 1682 * @tc.desc: This test case checks the conversion of a string that includes some style. 1683 * @tc.level: 1 1684 */ 1685 HWTEST_F(HtmlConvertTestNg, HtmlConverter014, TestSize.Level1) 1686 { 1687 const std::string multiHtml = 1688 "<html>" 1689 "<body>" 1690 "<p style=\"font-size:50px\"><span style=\"font-size:100px\">100fontsize</span>dddd当地经的123456<span " 1691 "style=\"font-size:30px\">30fontsize</span>1232132</p>" 1692 "</body>" 1693 "</html>"; 1694 HtmlToSpan toSpan; 1695 auto dstSpan = toSpan.ToSpanString(multiHtml); 1696 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1697 EXPECT_EQ(spans.size(), 4); 1698 1699 SpanToHtml convert; 1700 auto dstHtml = convert.ToHtml(*dstSpan); 1701 HtmlToSpan toSpan1; 1702 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 1703 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 1704 auto secondHtml = convert.ToHtml(*dstSpan1); 1705 EXPECT_EQ(secondHtml, dstHtml); 1706 } 1707 1708 /** 1709 * @tc.name: HtmlConverter015 1710 * @tc.desc: This test case checks the conversion of a string that includes more style. 1711 * @tc.level: 1 1712 */ 1713 HWTEST_F(HtmlConvertTestNg, HtmlConverter015, TestSize.Level1) 1714 { 1715 const std::string multiHtml = 1716 "<html>" 1717 "<head>" 1718 "</head>" 1719 "<body>" 1720 "<p style=\"font-size:50px;text-shadow: 0 0 3px red, green 0 0;\">" 1721 "<span style=\"font-size:100px\">100fontsize</span>dddd当地经的123456<span " 1722 "style=\"font-size:30px\">30fontsize</span>1232132</p>" 1723 "</body>" 1724 "</html>"; 1725 HtmlToSpan toSpan; 1726 auto dstSpan = toSpan.ToSpanString(multiHtml); 1727 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 1728 EXPECT_EQ(spans.size(), 4); 1729 auto it = spans.begin(); 1730 EXPECT_EQ((*it)->fontStyle->GetTextShadow().value()[0].GetColor(), OHOS::Ace::Color::RED); 1731 EXPECT_EQ((*it)->fontStyle->GetTextShadow().value()[1].GetColor(), OHOS::Ace::Color::GREEN); 1732 } 1733 1734 /** 1735 * @tc.name: HtmlConvertExam001 1736 * @tc.desc: This test case checks the conversion of a SpanString with basic text color and font size. 1737 * It ensures that the HTML conversion correctly applies the font size and text color. 1738 * @tc.level: 1 1739 */ 1740 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam001, TestSize.Level1) 1741 { 1742 /** 1743 * @tc.steps1: Create a SpanString with the text and apply the basic paragraph style with 1744 * font-size (50px) and text color (red). 1745 * @tc.expected: The generated HTML should include basic font size and text color styles. 1746 */ 1747 const std::string multiHtml = 1748 "<html>" 1749 "<head>" 1750 "</head>" 1751 "<body>" 1752 "<p style=\"font-weight:bold;letter-spacing:5px;\">" 1753 "这是一个测试,检查字体为粗体,字符间距为5px的文本。" 1754 "</p>" 1755 "</body>" 1756 "</html>"; 1757 1758 /** 1759 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 1760 * The result should match the original input HTML. 1761 * @tc.expected: The generated SpanString should match the original input HTML. 1762 */ 1763 HtmlToSpan toSpan; 1764 auto dstSpan = toSpan.ToSpanString(multiHtml); 1765 EXPECT_EQ(dstSpan->GetSpanItems().size(), 1); 1766 SpanToHtml convert; 1767 1768 auto dstHtml = convert.ToHtml(*dstSpan); 1769 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 1770 HtmlToSpan toSpan1; 1771 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 1772 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 1773 auto secondHtml = convert.ToHtml(*dstSpan1); 1774 EXPECT_EQ(secondHtml, dstHtml); 1775 } 1776 1777 /** 1778 * @tc.name: HtmlConvertExam002 1779 * @tc.desc: This test case checks the conversion of a SpanString with basic text color and font size. 1780 * It ensures that the HTML conversion correctly applies the font size and text color. 1781 * @tc.level: 1 1782 */ 1783 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam002, TestSize.Level1) 1784 { 1785 /** 1786 * @tc.steps1: Create a SpanString with the text and apply the basic paragraph style with 1787 * font-size (50px) and text color (red). 1788 * @tc.expected: The generated SpanString should include basic font size and text color styles. 1789 */ 1790 const std::string multiHtml = 1791 "<html>" 1792 "<head>" 1793 "</head>" 1794 "<body>" 1795 "<p style=\"font-size:50px;color:red;\">" 1796 "这是一个测试,检查字体颜色为红色,字体大小为50px的文本转换。" 1797 "</p>" 1798 "</body>" 1799 "</html>"; 1800 1801 /** 1802 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 1803 * The result should match the original input HTML. 1804 * @tc.expected: The generated SpanString should match the original input HTML. 1805 */ 1806 HtmlToSpan toSpan; 1807 auto dstSpan = toSpan.ToSpanString(multiHtml); 1808 EXPECT_EQ(dstSpan->GetSpanItems().size(), 1); 1809 SpanToHtml convert; 1810 1811 auto dstHtml = convert.ToHtml(*dstSpan); 1812 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 1813 HtmlToSpan toSpan1; 1814 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 1815 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 1816 auto secondHtml = convert.ToHtml(*dstSpan1); 1817 EXPECT_EQ(secondHtml, dstHtml); 1818 } 1819 1820 /** 1821 * @tc.name: HtmlConvertExam003 1822 * @tc.desc: This test case checks the conversion of a SpanString with mixed font sizes and text shadow. 1823 * It ensures that the HTML correctly applies different font sizes and text shadow effects. 1824 * @tc.level: 1 1825 */ 1826 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam003, TestSize.Level1) 1827 { 1828 /** 1829 * @tc.steps1: Create a SpanString and apply the paragraph style with font-size (50px) and text shadow (red). 1830 * @tc.expected: The generated HTML should include the correct font size and shadow effect. 1831 */ 1832 const std::string multiHtml = 1833 "<html>" 1834 "<head>" 1835 "</head>" 1836 "<body>" 1837 "<p style=\"font-size:50px;text-shadow:0px 0px 5px red;\">" 1838 "这段文本具有红色阴影效果,字体大小为50px,用于测试阴影和字体大小的转换。" 1839 "</p>" 1840 "</body>" 1841 "</html>"; 1842 1843 /** 1844 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 1845 * The result should match the original input HTML. 1846 * @tc.expected: The generated SpanString should match the original input HTML. 1847 */ 1848 HtmlToSpan toSpan; 1849 auto dstSpan = toSpan.ToSpanString(multiHtml); 1850 EXPECT_EQ(dstSpan->GetSpanItems().size(), 1); 1851 SpanToHtml convert; 1852 auto dstHtml = convert.ToHtml(*dstSpan); 1853 1854 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 1855 HtmlToSpan toSpan1; 1856 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 1857 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 1858 auto secondHtml = convert.ToHtml(*dstSpan1); 1859 EXPECT_EQ(secondHtml, dstHtml); 1860 } 1861 1862 /** 1863 * @tc.name: HtmlConvertExam004 1864 * @tc.desc: This test case checks the conversion of a SpanString with bold and italic font styles. 1865 * It ensures that the HTML applies the font styles correctly. 1866 * @tc.level: 1 1867 */ 1868 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam004, TestSize.Level1) 1869 { 1870 /** 1871 * @tc.steps1: Create a SpanString with the text and apply the paragraph style with font-weight (bold) 1872 * and font-style (italic). 1873 * @tc.expected: The generated HTML should include bold and italic font styles. 1874 */ 1875 const std::string multiHtml = 1876 "<html>" 1877 "<head>" 1878 "</head>" 1879 "<body>" 1880 "<p style=\"font-size:50px;font-weight:bold;font-style:italic;\">" 1881 "这是加粗且斜体的文本,用于测试段落中的加粗和斜体字体样式。" 1882 "</p>" 1883 "</body>" 1884 "</html>"; 1885 1886 /** 1887 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 1888 * The result should match the original input HTML. 1889 * @tc.expected: The generated SpanString should match the original input HTML. 1890 */ 1891 HtmlToSpan toSpan; 1892 auto dstSpan = toSpan.ToSpanString(multiHtml); 1893 EXPECT_EQ(dstSpan->GetSpanItems().size(), 1); 1894 1895 SpanToHtml convert; 1896 auto dstHtml = convert.ToHtml(*dstSpan); 1897 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 1898 HtmlToSpan toSpan1; 1899 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 1900 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 1901 auto secondHtml = convert.ToHtml(*dstSpan1); 1902 EXPECT_EQ(secondHtml, dstHtml); 1903 } 1904 1905 /** 1906 * @tc.name: HtmlConvertExam005 1907 * @tc.desc: This test case checks the conversion of a SpanString with mixed font sizes and colors. 1908 * It ensures that different parts of the text have the correct font size and color. 1909 * @tc.level: 1 1910 */ 1911 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam005, TestSize.Level1) 1912 { 1913 /** 1914 * @tc.steps1: Create a SpanString with the text and apply the paragraph style with font-size (50px) and 1915 * color (red), and a span with font-size (30px) and color (blue). 1916 */ 1917 const std::string multiHtml = 1918 "<html>" 1919 "<head>" 1920 "</head>" 1921 "<body>" 1922 "<p style=\"font-size:50px;color:red;\">" 1923 "这一部分为红色,字体大小50px,后面是<span style=\"font-size:30px;color:blue;\">蓝色30px字体</span>。" 1924 "</p>" 1925 "</body>" 1926 "</html>"; 1927 1928 /** 1929 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 1930 * The result should match the original input HTML. 1931 * @tc.expected: The generated SpanString should match the original input HTML. 1932 */ 1933 HtmlToSpan toSpan; 1934 auto dstSpan = toSpan.ToSpanString(multiHtml); 1935 EXPECT_EQ(dstSpan->GetSpanItems().size(), 3); 1936 1937 SpanToHtml convert; 1938 auto dstHtml = convert.ToHtml(*dstSpan); 1939 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 1940 HtmlToSpan toSpan1; 1941 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 1942 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 1943 auto secondHtml = convert.ToHtml(*dstSpan1); 1944 EXPECT_EQ(secondHtml, dstHtml); 1945 } 1946 1947 /** 1948 * @tc.name: HtmlConvertExam006 1949 * @tc.desc: This test case checks the conversion of a SpanString with underline and strikethrough. 1950 * It ensures the HTML conversion applies the text decoration correctly. 1951 * @tc.level: 1 1952 */ 1953 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam006, TestSize.Level1) 1954 { 1955 /** 1956 * @tc.steps1: Create a SpanString with the text and apply text-decoration (underline). 1957 * Add a span with text-decoration (line-through). 1958 * @tc.expected: The generated SpanString should include the underline and strikethrough decoration. 1959 */ 1960 const std::string multiHtml = 1961 "<html>" 1962 "<head>" 1963 "</head>" 1964 "<body>" 1965 "<p style=\"font-size:50px;text-decoration:underline;\">" 1966 "这段文本带有下划线效果,用于测试下划线装饰功能。" 1967 "<span style=\"text-decoration:line-through;\"> 这一部分有删除线效果。</span>" 1968 "</p>" 1969 "</body>" 1970 "</html>"; 1971 1972 /** 1973 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 1974 * The result should match the original input HTML. 1975 * @tc.expected: The generated SpanString should match the original input HTML. 1976 */ 1977 HtmlToSpan toSpan; 1978 auto dstSpan = toSpan.ToSpanString(multiHtml); 1979 EXPECT_EQ(dstSpan->GetSpanItems().size(), 2); 1980 SpanToHtml convert; 1981 1982 auto dstHtml = convert.ToHtml(*dstSpan); 1983 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 1984 HtmlToSpan toSpan1; 1985 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 1986 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 1987 auto secondHtml = convert.ToHtml(*dstSpan1); 1988 EXPECT_EQ(secondHtml, dstHtml); 1989 } 1990 1991 /** 1992 * @tc.name: HtmlConvertExam007 1993 * @tc.desc: This test case checks the conversion of a SpanString with font size and background color. 1994 * It ensures the HTML correctly applies the background color and font size. 1995 * @tc.level: 1 1996 */ 1997 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam007, TestSize.Level1) 1998 { 1999 /** 2000 * @tc.steps1: Create a SpanString with the text "这段文本有黄色背景,字体大小为50px,用于测试背景颜色转换。" 2001 * and apply paragraph styles with font-size (50px) and background-color (yellow). 2002 * @tc.expected: The generated HTML should include the font size and background color styles. 2003 */ 2004 const std::string multiHtml = 2005 "<html>" 2006 "<head>" 2007 "</head>" 2008 "<body>" 2009 "<p style=\"font-size:50px;background-color:yellow;\">" 2010 "这段文本有黄色背景,字体大小为50px,用于测试背景颜色转换。" 2011 "</p>" 2012 "</body>" 2013 "</html>"; 2014 2015 /** 2016 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 2017 * The result should match the original input HTML. 2018 * @tc.expected: The generated SpanString should match the original input HTML. 2019 */ 2020 HtmlToSpan toSpan; 2021 auto dstSpan = toSpan.ToSpanString(multiHtml); 2022 EXPECT_EQ(dstSpan->GetSpanItems().size(), 1); 2023 SpanToHtml convert; 2024 2025 auto dstHtml = convert.ToHtml(*dstSpan); 2026 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 2027 HtmlToSpan toSpan1; 2028 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 2029 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 2030 auto secondHtml = convert.ToHtml(*dstSpan1); 2031 EXPECT_EQ(secondHtml, dstHtml); 2032 } 2033 2034 /** 2035 * @tc.name: HtmlConvertExam008 2036 * @tc.desc: This test case checks the conversion of a SpanString with different font sizes and letter spacing. 2037 * It ensures the HTML correctly reflects the application of font size and letter spacing. 2038 * @tc.level: 1 2039 */ 2040 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam008, TestSize.Level1) 2041 { 2042 /** 2043 * @tc.steps1: Create a SpanString with the text "这段文字有5px的字母间距,字体大小为50px。" 2044 * and apply paragraph styles with font-size (50px) and letter-spacing (5px). 2045 * Add a span with font-size (30px) and letter-spacing (2px). 2046 * @tc.expected: The generated HTML should include correct font sizes and letter spacing. 2047 */ 2048 const std::string multiHtml = 2049 "<html>" 2050 "<head>" 2051 "</head>" 2052 "<body>" 2053 "<p style=\"font-size:50px;letter-spacing:5px;\">" 2054 "这段文字有5px的字母间距,字体大小为50px。<span style=\"font-size:30px;letter-spacing:2px;\">" 2055 "这一部分有2px字母间距和30px字体大小。</span>" 2056 "</p>" 2057 "</body>" 2058 "</html>"; 2059 2060 /** 2061 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 2062 * The result should match the original input HTML. 2063 * @tc.expected: The generated SpanString should match the original input HTML. 2064 */ 2065 HtmlToSpan toSpan; 2066 auto dstSpan = toSpan.ToSpanString(multiHtml); 2067 EXPECT_EQ(dstSpan->GetSpanItems().size(), 2); 2068 SpanToHtml convert; 2069 2070 auto dstHtml = convert.ToHtml(*dstSpan); 2071 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 2072 HtmlToSpan toSpan1; 2073 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 2074 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 2075 auto secondHtml = convert.ToHtml(*dstSpan1); 2076 EXPECT_EQ(secondHtml, dstHtml); 2077 } 2078 2079 /** 2080 * @tc.name: HtmlConvertExam009 2081 * @tc.desc: This test case checks the conversion of a SpanString with text color and multiple shadows. 2082 * It ensures the HTML correctly applies text color and shadow effects. 2083 * @tc.level: 1 2084 */ 2085 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam009, TestSize.Level1) 2086 { 2087 /** 2088 * @tc.steps1: Create a SpanString with the text "这段文本具有红色文本颜色和两个阴影效果(蓝色和绿色)。" 2089 * and apply the paragraph styles with text color (red) and multiple text shadows (blue and green). 2090 * @tc.expected: The generated HTML should include the correct text color and shadow effects. 2091 */ 2092 const std::string multiHtml = 2093 "<html>" 2094 "<head>" 2095 "</head>" 2096 "<body>" 2097 "<p style=\"font-size:50px;color:red;text-shadow:0px 0px 5px blue, 2px 2px 3px green;\">" 2098 "这段文本具有红色文本颜色和两个阴影效果(蓝色和绿色)。" 2099 "</p>" 2100 "</body>" 2101 "</html>"; 2102 2103 /** 2104 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 2105 * The result should match the original input HTML. 2106 * @tc.expected: The generated HTML should match the original input HTML. 2107 */ 2108 HtmlToSpan toSpan; 2109 auto dstSpan = toSpan.ToSpanString(multiHtml); 2110 EXPECT_EQ(dstSpan->GetSpanItems().size(), 1); 2111 SpanToHtml convert; 2112 2113 auto dstHtml = convert.ToHtml(*dstSpan); 2114 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 2115 HtmlToSpan toSpan1; 2116 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 2117 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 2118 auto secondHtml = convert.ToHtml(*dstSpan1); 2119 EXPECT_EQ(secondHtml, dstHtml); 2120 } 2121 2122 /** 2123 * @tc.name: HtmlConvertExam010 2124 * @tc.desc: This test case checks the conversion of a SpanString with font size, text color, 2125 * text decoration, and text shadow. It ensures all styles are correctly applied in the generated SpanString. 2126 * @tc.level: 1 2127 */ 2128 HWTEST_F(HtmlConvertTestNg, HtmlConvertExam010, TestSize.Level1) 2129 { 2130 /** 2131 * @tc.steps1: Create a SpanString with the text and apply the paragraph styles with font-size (50px), 2132 color (red), text-decoration (underline), and text-shadow (blue). 2133 * @tc.expected: The generated HTML should include all styles: font size, color, text decoration, and shadow. 2134 */ 2135 const std::string multiHtml = 2136 "<html>" 2137 "<head>" 2138 "</head>" 2139 "<body>" 2140 "<p style=\"font-size:50px;color:red;text-decoration:underline;text-shadow:0px 0px 5px blue;\">" 2141 "这段文本为红色,有下划线,并且有一个蓝色阴影效果,字体大小为50px。" 2142 "</p>" 2143 "</body>" 2144 "</html>"; 2145 2146 /** 2147 * @tc.steps2: Convert the HTML string to SpanString and then back to HTML. 2148 * The result should match the original input HTML. 2149 * @tc.expected: The generated HTML should match the original input HTML. 2150 */ 2151 HtmlToSpan toSpan; 2152 auto dstSpan = toSpan.ToSpanString(multiHtml); 2153 EXPECT_EQ(dstSpan->GetSpanItems().size(), 1); 2154 SpanToHtml convert; 2155 2156 auto dstHtml = convert.ToHtml(*dstSpan); 2157 EXPECT_GT(dstHtml.size(), 0); // dstHtml is converted 2158 HtmlToSpan toSpan1; 2159 auto dstSpan1 = toSpan1.ToSpanString(dstHtml); 2160 EXPECT_EQ(IsSpanItemSame(dstSpan->GetSpanItems(), dstSpan1->GetSpanItems()), true); 2161 auto secondHtml = convert.ToHtml(*dstSpan1); 2162 EXPECT_EQ(secondHtml, dstHtml); 2163 } 2164 2165 /** 2166 * @tc.name: MultiHtmlConvert 2167 * @tc.desc: This test case checks the conversion of HTML with different text-decoration styles 2168 * (underline, strike-through, overline). 2169 * @tc.level: 1 2170 */ 2171 HWTEST_F(HtmlConvertTestNg, MultiHtmlConvert, TestSize.Level1) 2172 { 2173 /** 2174 * @tc.steps1: Test conversion with `text-decoration` values: `underline`, `line-through`, and `overline`. 2175 * @tc.expected: Each text-decoration style should be correctly applied to the resulting `SpanString`. 2176 */ 2177 const std::string multiHtml = "<html>" 2178 "<body>" 2179 "<p style=\"text-decoration:underline;\">Underlined Text</p>" 2180 "<p style=\"text-decoration:line-through;\">Strikethrough Text</p>" 2181 "<p style=\"text-decoration:overline;\">Overlined Text</p>" 2182 "</body>" 2183 "</html>"; 2184 HtmlToSpan toSpan; 2185 auto dstSpan = toSpan.ToSpanString(multiHtml); 2186 2187 /** 2188 * @tc.steps2: Verify the size of the converted `SpanString`. 2189 * It should have 3 span items for the 3 different text-decoration styles. 2190 * @tc.expected: The size of the spans list should be 3. 2191 */ 2192 std::list<RefPtr<NG::SpanItem>> spans = dstSpan->GetSpanItems(); 2193 EXPECT_EQ(spans.size(), 3); 2194 2195 /** 2196 * @tc.steps3: Verify that the first span item has the `underline` text-decoration. 2197 * @tc.expected: The first span item should have the `underline` text-decoration. 2198 */ 2199 auto it = spans.begin(); 2200 EXPECT_EQ((*it)->fontStyle->GetTextDecoration().value(), TextDecoration::UNDERLINE); 2201 2202 /** 2203 * @tc.steps4: Verify that the second span item has the `line-through` text-decoration. 2204 * @tc.expected: The second span item should have the `line-through` text-decoration. 2205 */ 2206 ++it; 2207 EXPECT_EQ((*it)->fontStyle->GetTextDecoration().value(), TextDecoration::LINE_THROUGH); 2208 2209 /** 2210 * @tc.steps5: Verify that the third span item has the `overline` text-decoration. 2211 * @tc.expected: The third span item should have the `overline` text-decoration. 2212 */ 2213 ++it; 2214 EXPECT_EQ((*it)->fontStyle->GetTextDecoration().value(), TextDecoration::OVERLINE); 2215 } 2216 2217 } // namespace OHOS::Ace::NG