1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <functional>
17 #include <memory>
18 #include <optional>
19 #include <vector>
20 
21 #include "gtest/gtest.h"
22 #define private public
23 #define protected public
24 
25 #include "test/mock/core/common/mock_theme_manager.h"
26 #include "test/mock/core/pipeline/mock_pipeline_context.h"
27 #include "test/mock/core/render/mock_paragraph.h"
28 
29 #include "base/geometry/dimension.h"
30 #include "base/memory/ace_type.h"
31 #include "base/memory/referenced.h"
32 #include "core/components/common/properties/color.h"
33 #include "core/components/common/properties/text_style.h"
34 #include "core/components/text/text_theme.h"
35 #include "core/components_ng/pattern/text/span/mutable_span_string.h"
36 #include "core/components_ng/pattern/text/span/span_object.h"
37 #include "core/components_ng/pattern/text/span/span_string.h"
38 #include "core/components_ng/pattern/text/text_pattern.h"
39 #include "core/components_ng/pattern/text/text_styles.h"
40 #include "core/components_ng/property/measure_property.h"
41 
42 using namespace testing;
43 using namespace testing::ext;
44 namespace OHOS::Ace::NG {
45 namespace {
46 const uint32_t GESTURE_INDEX1 = 1;
ConstructGestureStyle(GestureStyle & gestureInfo)47 void ConstructGestureStyle(GestureStyle& gestureInfo)
48 {
49     auto onClick = [](const BaseEventInfo* info) {};
50     auto tmpClickFunc = [func = std::move(onClick)](GestureEvent& info) { func(&info); };
51     gestureInfo.onClick = std::move(tmpClickFunc);
52 
53     auto onLongPress = [](const BaseEventInfo* info) {};
54     auto tmpLongPressFunc = [func = std::move(onLongPress)](GestureEvent& info) { func(&info); };
55     gestureInfo.onLongPress = std::move(tmpLongPressFunc);
56 }
57 } // namespace
58 
59 class SpanStringTestNg : public testing::Test {
60 public:
61     static void SetUpTestSuite();
62     static void TearDownTestSuite();
63     static ImageSpanOptions GetImageOption(const std::string& src);
64 };
65 
SetUpTestSuite()66 void SpanStringTestNg::SetUpTestSuite()
67 {
68     MockPipelineContext::SetUp();
69     auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
70     MockPipelineContext::GetCurrent()->SetThemeManager(themeManager);
71     EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<TextTheme>()));
72     RefPtr<FrameNode> stageNode = AceType::MakeRefPtr<FrameNode>("STAGE", -1, AceType::MakeRefPtr<Pattern>());
73     auto stageManager = AceType::MakeRefPtr<StageManager>(stageNode);
74     MockPipelineContext::GetCurrent()->stageManager_ = stageManager;
75 }
76 
TearDownTestSuite()77 void SpanStringTestNg::TearDownTestSuite()
78 {
79     MockPipelineContext::TearDown();
80 }
81 
GetImageOption(const std::string & src)82 ImageSpanOptions SpanStringTestNg::GetImageOption(const std::string& src)
83 {
84     ImageSpanSize size { .width = 50.0_vp, .height = 50.0_vp };
85     BorderRadiusProperty borderRadius;
86     borderRadius.SetRadius(2.0_vp);
87     MarginProperty margins;
88     margins.SetEdges(CalcLength(10.0));
89     PaddingProperty paddings;
90     paddings.SetEdges(CalcLength(5.0));
91     ImageSpanAttribute attr { .paddingProp = paddings,
92         .marginProp = margins,
93         .borderRadius = borderRadius,
94         .objectFit = ImageFit::COVER,
95         .verticalAlign = VerticalAlign::BOTTOM };
96     ImageSpanOptions option { .image = src, .imageAttribute = attr };
97     return option;
98 }
99 
100 class TestNode : public UINode {
101     DECLARE_ACE_TYPE(TestNode, UINode);
102 
103 public:
CreateTestNode(int32_t nodeId)104     static RefPtr<TestNode> CreateTestNode(int32_t nodeId)
105     {
106         auto spanNode = MakeRefPtr<TestNode>(nodeId);
107         return spanNode;
108     };
109 
IsAtomicNode() const110     bool IsAtomicNode() const override
111     {
112         return true;
113     }
114 
TestNode(int32_t nodeId)115     explicit TestNode(int32_t nodeId) : UINode("TestNode", nodeId) {}
116     ~TestNode() override = default;
117 };
118 std::string test_str[] = { "hello", "world", "this", "find", "gank", "pink", "that", "when", "how", "cpp" };
119 Font testFont1 { OHOS::Ace::FontWeight::BOLD, Dimension(29.0, DimensionUnit::PX), OHOS::Ace::FontStyle::ITALIC,
120     std::vector<std::string>(test_str, test_str + 10), OHOS::Ace::Color::RED };
121 Font testFont2 { OHOS::Ace::FontWeight::LIGHTER, Dimension(19.0, DimensionUnit::PX), OHOS::Ace::FontStyle::ITALIC,
122     std::vector<std::string>(test_str, test_str + 10), OHOS::Ace::Color::WHITE };
123 Font testEmptyFont {};
124 /**
125  * @tc.name: SpanStringTest001
126  * @tc.desc: Test basic function of GetString/GetLength/GetIndex
127  * @tc.type: FUNC
128  */
129 HWTEST_F(SpanStringTestNg, SpanString001, TestSize.Level1)
130 {
131     auto spanString = AceType::MakeRefPtr<SpanString>("0123456789");
132     EXPECT_EQ(spanString->GetString(), "0123456789");
133     EXPECT_EQ(spanString->GetLength(), 10);
134 
135     auto spanString1 = AceType::MakeRefPtr<SpanString>("中0123456789");
136     EXPECT_EQ(spanString1->GetString(), "中0123456789");
137     EXPECT_EQ(spanString1->GetLength(), 11);
138 
139     auto spanString2 = AceType::MakeRefPtr<SpanString>("0123456");
140     EXPECT_EQ(spanString2->GetString(), "0123456");
141     EXPECT_EQ(spanString2->GetLength(), 7);
142 
143     auto spanString3 = AceType::MakeRefPtr<SpanString>("你好");
144     EXPECT_EQ(spanString3->GetString(), "你好");
145     EXPECT_EQ(spanString3->GetLength(), 2);
146 }
147 /**
148  * @tc.name: SpanStringTest002
149  * @tc.desc: Test basic function of IsEqualToSpanString/GetSubSpanString
150  * @tc.type: FUNC
151  */
152 HWTEST_F(SpanStringTestNg, SpanString002, TestSize.Level1)
153 {
154     auto spanString1 = AceType::MakeRefPtr<SpanString>("01234中56789");
155     auto spanString2 = AceType::MakeRefPtr<SpanString>("01234中56789");
156     auto spanString3 = AceType::MakeRefPtr<SpanString>("01234567891");
157     EXPECT_TRUE(spanString1->IsEqualToSpanString(spanString2));
158     EXPECT_FALSE(spanString1->IsEqualToSpanString(spanString3));
159     std::vector<RefPtr<SpanBase>> spans;
160     spans.push_back(AceType::MakeRefPtr<FontSpan>(testFont2, 0, 3));
161     spans.push_back(AceType::MakeRefPtr<FontSpan>(testEmptyFont, 5, 8));
162     auto spanStringWithSpans1 = AceType::MakeRefPtr<SpanString>("01234567891");
163     spanStringWithSpans1->BindWithSpans(spans);
164     auto spanStringWithSpans2 = AceType::MakeRefPtr<SpanString>("01234567891");
165     spanStringWithSpans2->BindWithSpans(spans);
166     EXPECT_TRUE(spanStringWithSpans1->IsEqualToSpanString(spanStringWithSpans2));
167     std::vector<RefPtr<SpanBase>> spans1;
168     spans1.push_back(AceType::MakeRefPtr<FontSpan>(testFont2, 0, 3));
169     spans1.push_back(AceType::MakeRefPtr<FontSpan>(testEmptyFont, 5, 7));
170     auto spanStringWithSpans3 = AceType::MakeRefPtr<SpanString>("01234567891");
171     spanStringWithSpans3->BindWithSpans(spans1);
172     EXPECT_FALSE(spanStringWithSpans3->IsEqualToSpanString(spanStringWithSpans2));
173     auto subSpanStringWithSpans2 = spanStringWithSpans2->GetSubSpanString(0, 7);
174     auto subSpanStringWithSpans3 = spanStringWithSpans3->GetSubSpanString(0, 7);
175     auto map2 = subSpanStringWithSpans2->GetSpansMap();
176     EXPECT_TRUE(subSpanStringWithSpans2->IsEqualToSpanString(subSpanStringWithSpans3));
177     auto emptySpanString = spanStringWithSpans2->GetSubSpanString(1, 0);
178     EXPECT_TRUE(emptySpanString->IsEqualToSpanString(AceType::MakeRefPtr<SpanString>("")));
179 }
180 
181 /**
182  * @tc.name: SpanStringTest003
183  * @tc.desc: Test basic function of GetSpans/GetFontColor/GetFontSize/GetFontWeight/GetFontFamily/GetFontStyle
184  * @tc.type: FUNC
185  */
186 HWTEST_F(SpanStringTestNg, SpanString003, TestSize.Level1)
187 {
188     auto spanString3 = AceType::MakeRefPtr<MutableSpanString>("0123456789");
189     spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 3));
190     spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 3, 5));
191     spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testEmptyFont, 5, 8));
192     auto firstSpans = spanString3->GetSpans(5, 2);
193     EXPECT_EQ(firstSpans.size(), 1);
194     auto firstFontSpan = AceType::DynamicCast<FontSpan>(firstSpans[0]);
195     EXPECT_NE(firstFontSpan, nullptr);
196     EXPECT_EQ(firstFontSpan->GetStartIndex(), 5);
197     EXPECT_EQ(firstFontSpan->GetEndIndex(), 7);
198     EXPECT_EQ(firstFontSpan->GetFont().GetFontColor(), "");
199 
200     auto secondString3 = spanString3->GetSpans(0, 3);
201     EXPECT_EQ(secondString3.size(), 1);
202     auto secondFontSpan = AceType::DynamicCast<FontSpan>(secondString3[0]);
203     EXPECT_NE(secondFontSpan, nullptr);
204     EXPECT_EQ(secondFontSpan->GetStartIndex(), 0);
205     EXPECT_EQ(secondFontSpan->GetEndIndex(), 3);
206     EXPECT_EQ(secondFontSpan->GetFont().GetFontColor(), OHOS::Ace::Color::RED.ColorToString());
207 
208     auto thirdString3 = spanString3->GetSpans(3, 1);
209     EXPECT_EQ(thirdString3.size(), 1);
210     auto thirdFontSpan = AceType::DynamicCast<FontSpan>(thirdString3[0]);
211     EXPECT_NE(thirdFontSpan, nullptr);
212     EXPECT_EQ(thirdFontSpan->GetStartIndex(), 3);
213     EXPECT_EQ(thirdFontSpan->GetEndIndex(), 4);
214     EXPECT_EQ(thirdFontSpan->GetFont().GetFontColor(), OHOS::Ace::Color::WHITE.ColorToString());
215 }
216 /**
217  * @tc.name: SpanStringTest004
218  * @tc.desc: Test basic function of GetString/GetLength/GetIndex
219  * @tc.type: FUNC
220  */
221 HWTEST_F(SpanStringTestNg, SpanString004, TestSize.Level1)
222 {
223     auto spanString3 = AceType::MakeRefPtr<MutableSpanString>("0123456789");
224     spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testEmptyFont, 0, 3));
225     spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 3, 5));
226     spanString3->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 5, 8));
227     auto spans = spanString3->GetSpans(1, 7);
228     EXPECT_EQ(spans.size(), 3);
229     auto firstFontSpan = AceType::DynamicCast<FontSpan>(spans[0]);
230     EXPECT_NE(firstFontSpan, nullptr);
231     EXPECT_EQ(firstFontSpan->GetStartIndex(), 1);
232     EXPECT_EQ(firstFontSpan->GetEndIndex(), 3);
233     EXPECT_EQ(firstFontSpan->GetFont().GetFontColor(), "");
234 
235     auto secondFontSpan = AceType::DynamicCast<FontSpan>(spans[1]);
236     EXPECT_NE(secondFontSpan, nullptr);
237     EXPECT_EQ(secondFontSpan->GetStartIndex(), 3);
238     EXPECT_EQ(secondFontSpan->GetEndIndex(), 5);
239     EXPECT_EQ(secondFontSpan->GetFont().GetFontColor(), OHOS::Ace::Color::RED.ColorToString());
240 
241     auto thirdFontSpan = AceType::DynamicCast<FontSpan>(spans[2]);
242     EXPECT_NE(thirdFontSpan, nullptr);
243     EXPECT_EQ(thirdFontSpan->GetStartIndex(), 5);
244     EXPECT_EQ(thirdFontSpan->GetEndIndex(), 8);
245     EXPECT_EQ(thirdFontSpan->GetFont().GetFontColor(), OHOS::Ace::Color::WHITE.ColorToString());
246 }
247 
248 /**
249  * @tc.name: SpanStringTest005
250  * @tc.desc: Test basic function of DecorationSpan/BaselineOffsetSpan/LetterSpacingSpan/TextShadowSpan
251  * @tc.type: FUNC
252  */
253 HWTEST_F(SpanStringTestNg, SpanString005, TestSize.Level1)
254 {
255     auto spanString3 = AceType::MakeRefPtr<MutableSpanString>("0123456789");
256     spanString3->AddSpan(
257         AceType::MakeRefPtr<DecorationSpan>(TextDecoration::OVERLINE, Color::RED, TextDecorationStyle::WAVY, 0, 1));
258     spanString3->AddSpan(AceType::MakeRefPtr<BaselineOffsetSpan>(Dimension(4), 0, 2));
259     spanString3->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(5), 0, 3));
260     Shadow textShadow;
261     textShadow.SetBlurRadius(0.0);
262     textShadow.SetColor(Color::BLUE);
263     textShadow.SetOffsetX(5.0);
264     textShadow.SetOffsetY(5.0);
265     vector<Shadow> textShadows { textShadow };
266     spanString3->AddSpan(AceType::MakeRefPtr<TextShadowSpan>(textShadows, 7, 9));
267 
268     auto firstSpans = spanString3->GetSpans(2, 1);
269     EXPECT_EQ(firstSpans.size(), 1);
270     auto letterSpacingSpan = AceType::DynamicCast<LetterSpacingSpan>(firstSpans[0]);
271     EXPECT_NE(letterSpacingSpan, nullptr);
272     EXPECT_EQ(letterSpacingSpan->GetStartIndex(), 2);
273     EXPECT_EQ(letterSpacingSpan->GetEndIndex(), 3);
274     EXPECT_TRUE(letterSpacingSpan->GetLetterSpacing() == Dimension(5));
275 
276     auto secondSpans = spanString3->GetSpans(1, 1);
277     EXPECT_EQ(secondSpans.size(), 2);
278 
279     auto thirdSpans = spanString3->GetSpans(0, 1);
280     EXPECT_EQ(thirdSpans.size(), 3);
281 
282     auto fourthSpans = spanString3->GetSpans(3, 1);
283     EXPECT_EQ(fourthSpans.size(), 0);
284 
285     auto fifthSpans = spanString3->GetSpans(0, 9);
286     EXPECT_EQ(fifthSpans.size(), 4);
287 }
288 
289 /**
290  * @tc.name: SpanStringTest006
291  * @tc.desc: Test basic function of DecorationSpan/BaselineOffsetSpan/LetterSpacingSpan/TextShadowSpan
292  * @tc.type: FUNC
293  */
294 HWTEST_F(SpanStringTestNg, SpanString006, TestSize.Level1)
295 {
296     auto spanString3 = AceType::MakeRefPtr<MutableSpanString>("0123456789");
297     spanString3->AddSpan(
298         AceType::MakeRefPtr<DecorationSpan>(TextDecoration::OVERLINE, Color::RED, TextDecorationStyle::WAVY, 0, 1));
299     spanString3->AddSpan(AceType::MakeRefPtr<BaselineOffsetSpan>(Dimension(4), 0, 2));
300     spanString3->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(5), 5, 8));
301     Shadow textShadow;
302     textShadow.SetBlurRadius(0.0);
303     textShadow.SetColor(Color::BLUE);
304     textShadow.SetOffsetX(5.0);
305     textShadow.SetOffsetY(5.0);
306     vector<Shadow> textShadows { textShadow };
307     spanString3->AddSpan(AceType::MakeRefPtr<TextShadowSpan>(textShadows, 8, 10));
308     auto subSpanString = spanString3->GetSubSpanString(0, 10);
309     EXPECT_TRUE(subSpanString->IsEqualToSpanString(spanString3));
310     auto firstSpans = spanString3->GetSpans(5, 1);
311     auto letterSpacingSpan = AceType::DynamicCast<LetterSpacingSpan>(firstSpans[0]);
312     EXPECT_NE(letterSpacingSpan, nullptr);
313     EXPECT_EQ(letterSpacingSpan->GetStartIndex(), 5);
314     EXPECT_EQ(letterSpacingSpan->GetEndIndex(), 6);
315     EXPECT_TRUE(letterSpacingSpan->GetLetterSpacing() == Dimension(5));
316 
317     auto secondSpans = spanString3->GetSpans(1, 2);
318     EXPECT_EQ(secondSpans.size(), 1);
319     auto baselineOffsetSpan = AceType::DynamicCast<BaselineOffsetSpan>(secondSpans[0]);
320     EXPECT_NE(baselineOffsetSpan, nullptr);
321     EXPECT_EQ(baselineOffsetSpan->GetStartIndex(), 1);
322     EXPECT_EQ(baselineOffsetSpan->GetEndIndex(), 2);
323     EXPECT_TRUE(baselineOffsetSpan->GetBaselineOffset() == Dimension(4));
324 
325     auto thirdSpans = spanString3->GetSpans(8, 1);
326     EXPECT_EQ(thirdSpans.size(), 1);
327     auto textShadowSpan = AceType::DynamicCast<TextShadowSpan>(thirdSpans[0]);
328     EXPECT_NE(textShadowSpan, nullptr);
329     EXPECT_EQ(textShadowSpan->GetStartIndex(), 8);
330     EXPECT_EQ(textShadowSpan->GetEndIndex(), 9);
331     EXPECT_TRUE(textShadowSpan->GetTextShadow()[0] == textShadow);
332 }
333 
334 /**
335  * @tc.name: SpanStringTest007
336  * @tc.desc: Test basic function of CustomSpan
337  * @tc.type: FUNC
338  */
339 HWTEST_F(SpanStringTestNg, SpanString007, TestSize.Level1)
340 {
341     auto customSpan = AceType::MakeRefPtr<CustomSpan>();
342     auto spanString = AceType::MakeRefPtr<MutableSpanString>(customSpan);
343     auto spans = spanString->GetSpans(0, spanString->GetLength());
344     EXPECT_EQ(spans.size(), 1);
345     auto span = AceType::DynamicCast<CustomSpan>(spans[0]);
346     EXPECT_EQ(span->GetStartIndex(), 0);
347     EXPECT_EQ(span->GetEndIndex(), 1);
348     EXPECT_EQ(span->GetOnMeasure(), std::nullopt);
349     EXPECT_EQ(span->GetOnDraw(), std::nullopt);
350 
351     spanString->AppendSpanString(spanString);
352     spans = spanString->GetSpans(0, spanString->GetLength());
353     EXPECT_EQ(spans.size(), 2);
354     auto span0 = AceType::DynamicCast<CustomSpan>(spans[0]);
355     EXPECT_EQ(span0->GetStartIndex(), 0);
356     EXPECT_EQ(span0->GetEndIndex(), 1);
357     EXPECT_EQ(span0->GetOnMeasure(), std::nullopt);
358     EXPECT_EQ(span0->GetOnDraw(), std::nullopt);
359     auto span1 = AceType::DynamicCast<CustomSpan>(spans[1]);
360     EXPECT_EQ(span1->GetStartIndex(), 1);
361     EXPECT_EQ(span1->GetEndIndex(), 2);
362     EXPECT_EQ(span1->GetOnMeasure(), std::nullopt);
363     EXPECT_EQ(span1->GetOnDraw(), std::nullopt);
364     EXPECT_FALSE(span0->IsAttributesEqual(span1));
365     spanString->RemoveSpans(0, spanString->GetLength());
366     EXPECT_EQ(spanString->GetLength(), 0);
367 }
368 
369 /**
370  * @tc.name: SpanStringTest008
371  * @tc.desc: Test basic function of CustomSpan/Image
372  * @tc.type: FUNC
373  */
374 HWTEST_F(SpanStringTestNg, SpanString008, TestSize.Level1)
375 {
376     auto imageOption = SpanStringTestNg::GetImageOption("src/icon-1.png");
377     auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption);
378     mutableStr->InsertString(0, "123");
379     mutableStr->InsertString(4, "456");
380     auto imageOption1 = SpanStringTestNg::GetImageOption("src/icon-2.png");
381     auto imageSpan1 = AceType::MakeRefPtr<SpanString>(imageOption1);
382     mutableStr->AppendSpanString(imageSpan1);
383     auto customSpan = AceType::MakeRefPtr<CustomSpan>();
384     auto spanString = AceType::MakeRefPtr<MutableSpanString>(customSpan);
385     spanString->AppendSpanString(mutableStr);
386     auto spans = spanString->GetSpans(0, spanString->GetLength());
387     EXPECT_EQ(spans.size(), 3);
388     spanString->AppendSpanString(spanString);
389     spans = spanString->GetSpans(0, spanString->GetLength());
390     EXPECT_EQ(spans.size(), 6);
391 }
392 
393 /**
394  * @tc.name: SpanStringTest001
395  * @tc.desc: Test basic function of ReplaceString/InsertString/RemoveString
396  * @tc.type: FUNC
397  */
398 HWTEST_F(SpanStringTestNg, MutableSpanString001, TestSize.Level1)
399 {
400     auto a = AceType::MakeRefPtr<MutableSpanString>("0123456789");
401     EXPECT_EQ(a->GetString(), "0123456789");
402     a->ReplaceString(0, 1, "abcd");
403     EXPECT_EQ(a->GetString(), "abcd123456789");
404     a->InsertString(0, "abcd");
405     EXPECT_EQ(a->GetString(), "abcdabcd123456789");
406     a->RemoveString(3, 3);
407     EXPECT_EQ(a->GetString(), "abccd123456789");
408     a->InsertString(4, "中文插入测试");
409     EXPECT_EQ(a->GetString(), "abcc中文插入测试d123456789");
410     a->RemoveString(4, 6);
411     EXPECT_EQ(a->GetString(), "abccd123456789");
412     a->ReplaceString(5, 9, "中文替换测试");
413     EXPECT_EQ(a->GetString(), "abccd中文替换测试");
414 }
415 
CompareSpanList(const std::list<RefPtr<SpanBase>> & a,const std::list<RefPtr<SpanBase>> & b)416 bool CompareSpanList(const std::list<RefPtr<SpanBase>>& a, const std::list<RefPtr<SpanBase>>& b)
417 {
418     if (a.size() != b.size()) {
419         std::cout << "size not match" << a.size() << " " << b.size() << std::endl;
420         return false;
421     }
422     auto ita = a.begin();
423     auto itb = b.begin();
424     while (ita != a.end()) {
425         if (!(*ita)->IsAttributesEqual(*itb)) {
426             std::cout << (*ita)->ToString() << " " << (*itb)->ToString() << std::endl;
427             return false;
428         }
429         ++ita;
430         ++itb;
431     }
432     return true;
433 }
434 
435 /**
436  * @tc.name: SpanStringTest001
437  * @tc.desc: Test if span is right after addSpan/replaceSpan/removeSpan
438  * @tc.type: FUNC
439  */
440 HWTEST_F(SpanStringTestNg, MutableSpanString002, TestSize.Level1)
441 {
442     auto a = AceType::MakeRefPtr<MutableSpanString>("0123456789");
443 
444     std::string test_str[] = { "hello", "world", "this", "find", "gank", "pink", "that", "when", "how", "cpp" };
445     a->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10));
446     EXPECT_EQ(a->GetString(), "0123456789");
447     std::list<RefPtr<SpanBase>> resultList1 = { AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10) };
448     auto aSpansMap = a->GetSpansMap();
449     EXPECT_TRUE(CompareSpanList(aSpansMap[SpanType::Font], resultList1));
450     std::list<RefPtr<SpanBase>> resultList2 = { AceType::MakeRefPtr<FontSpan>(testFont2, 0, 3),
451         AceType::MakeRefPtr<FontSpan>(testFont1, 3, 10) };
452     a->ReplaceSpan(0, 3, AceType::MakeRefPtr<FontSpan>(testFont2, 0, 10));
453     aSpansMap = a->GetSpansMap();
454     EXPECT_EQ(a->GetString(), "0123456789");
455     EXPECT_TRUE(CompareSpanList(aSpansMap[SpanType::Font], resultList2));
456     std::list<RefPtr<SpanBase>> resultList3 = { AceType::MakeRefPtr<FontSpan>(testFont1, 3, 10) };
457     a->RemoveSpan(0, 3, SpanType::Font);
458     aSpansMap = a->GetSpansMap();
459     EXPECT_EQ(a->GetString(), "0123456789");
460     EXPECT_TRUE(CompareSpanList(aSpansMap[SpanType::Font], resultList3));
461 
462     // 用中文再测一次
463     auto b = MutableSpanString("零一二三四五六七八九");
464     b.AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10));
465     EXPECT_EQ(b.GetString(), "零一二三四五六七八九");
466     std::list<RefPtr<SpanBase>> resultList4 = { AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10) };
467     auto bSpansMap = b.GetSpansMap();
468     EXPECT_TRUE(CompareSpanList(bSpansMap[SpanType::Font], resultList4));
469     std::list<RefPtr<SpanBase>> resultList5 = { AceType::MakeRefPtr<FontSpan>(testFont2, 0, 3),
470         AceType::MakeRefPtr<FontSpan>(testFont1, 3, 10) };
471     b.ReplaceSpan(0, 3, AceType::MakeRefPtr<FontSpan>(testFont2, 0, 10));
472     bSpansMap = b.GetSpansMap();
473     EXPECT_EQ(b.GetString(), "零一二三四五六七八九");
474     EXPECT_TRUE(CompareSpanList(bSpansMap[SpanType::Font], resultList5));
475     std::list<RefPtr<SpanBase>> resultList6 = { AceType::MakeRefPtr<FontSpan>(testFont1, 3, 10) };
476     b.RemoveSpan(0, 3, SpanType::Font);
477     bSpansMap = b.GetSpansMap();
478     EXPECT_EQ(b.GetString(), "零一二三四五六七八九");
479     EXPECT_TRUE(CompareSpanList(bSpansMap[SpanType::Font], resultList6));
480 }
481 
482 /**
483  * @tc.name: MutableSpanString003
484  * @tc.desc: Test if span is right after addSpan
485  * @tc.type: FUNC
486  */
487 HWTEST_F(SpanStringTestNg, MutableSpanString003, TestSize.Level1)
488 {
489     auto mutableSpan = AceType::MakeRefPtr<MutableSpanString>("0123456789");
490 
491     Font fontOne { .fontColor = OHOS::Ace::Color::RED };
492     Font fontTwo { .fontColor = OHOS::Ace::Color::WHITE };
493     Font fontThree { .fontColor = OHOS::Ace::Color::BLACK };
494 
495     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontOne, 0, 10));
496     auto spansMap = mutableSpan->GetSpansMap();
497     auto spanItems = mutableSpan->GetSpanItems();
498     EXPECT_EQ(spansMap.size(), 1);
499     EXPECT_EQ(spanItems.size(), 1);
500 
501     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontTwo, 3, 7));
502     spansMap = mutableSpan->GetSpansMap();
503     auto fontSpans = spansMap[SpanType::Font];
504     spanItems = mutableSpan->GetSpanItems();
505     EXPECT_EQ(fontSpans.size(), 3);
506     EXPECT_EQ(spanItems.size(), 3);
507     std::list<RefPtr<SpanBase>> resultList = { AceType::MakeRefPtr<FontSpan>(fontOne, 0, 3),
508         AceType::MakeRefPtr<FontSpan>(fontTwo, 3, 7), AceType::MakeRefPtr<FontSpan>(fontOne, 7, 10) };
509     EXPECT_EQ(CompareSpanList(fontSpans, resultList), true);
510 
511     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontThree, 2, 5));
512     spansMap = mutableSpan->GetSpansMap();
513     fontSpans = spansMap[SpanType::Font];
514     spanItems = mutableSpan->GetSpanItems();
515     EXPECT_EQ(fontSpans.size(), 4);
516     EXPECT_EQ(spanItems.size(), 5);
517     resultList = { AceType::MakeRefPtr<FontSpan>(fontOne, 0, 2), AceType::MakeRefPtr<FontSpan>(fontThree, 2, 5),
518         AceType::MakeRefPtr<FontSpan>(fontTwo, 5, 7), AceType::MakeRefPtr<FontSpan>(fontOne, 7, 10) };
519     EXPECT_EQ(CompareSpanList(fontSpans, resultList), true);
520 }
521 
522 /**
523  * @tc.name: MutableSpanString004
524  * @tc.desc: Test if span is right after removeSpan
525  * @tc.type: FUNC
526  */
527 HWTEST_F(SpanStringTestNg, MutableSpanString004, TestSize.Level1)
528 {
529     auto mutableSpan = AceType::MakeRefPtr<MutableSpanString>("0123456789");
530 
531     Font fontOne { .fontColor = OHOS::Ace::Color::RED };
532     Font fontTwo { .fontColor = OHOS::Ace::Color::WHITE };
533     Font fontThree { .fontColor = OHOS::Ace::Color::BLACK };
534 
535     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontOne, 0, 10));
536     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontTwo, 3, 7));
537     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontThree, 2, 5));
538 
539     mutableSpan->RemoveSpan(2, 3, SpanType::Font);
540     auto spansMap = mutableSpan->GetSpansMap();
541     auto fontSpans = spansMap[SpanType::Font];
542     EXPECT_EQ(fontSpans.size(), 3);
543     std::list<RefPtr<SpanBase>> resultList = { AceType::MakeRefPtr<FontSpan>(fontOne, 0, 2),
544         AceType::MakeRefPtr<FontSpan>(fontTwo, 5, 7), AceType::MakeRefPtr<FontSpan>(fontOne, 7, 10) };
545     EXPECT_EQ(CompareSpanList(fontSpans, resultList), true);
546 
547     mutableSpan->RemoveSpan(2, 5, SpanType::Font);
548     spansMap = mutableSpan->GetSpansMap();
549     fontSpans = spansMap[SpanType::Font];
550     EXPECT_EQ(fontSpans.size(), 2);
551     resultList = { AceType::MakeRefPtr<FontSpan>(fontOne, 0, 2), AceType::MakeRefPtr<FontSpan>(fontOne, 7, 10) };
552     EXPECT_EQ(CompareSpanList(fontSpans, resultList), true);
553 
554     mutableSpan->RemoveSpan(0, 10, SpanType::Font);
555     spansMap = mutableSpan->GetSpansMap();
556     fontSpans = spansMap[SpanType::Font];
557     EXPECT_EQ(fontSpans.size(), 0);
558 }
559 
560 /**
561  * @tc.name: MutableSpanString005
562  * @tc.desc: Test for outliers
563  * @tc.type: FUNC
564  */
565 HWTEST_F(SpanStringTestNg, MutableSpanString005, TestSize.Level1)
566 {
567     auto mutableSpan = AceType::MakeRefPtr<MutableSpanString>("0123456789");
568 
569     Font fontOne { .fontColor = OHOS::Ace::Color::RED };
570     Font fontTwo { .fontColor = OHOS::Ace::Color::WHITE };
571     Font fontThree { .fontColor = OHOS::Ace::Color::BLACK };
572 
573     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontOne, -1, 10));
574     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontTwo, 3, 100));
575     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontThree, -100, 100));
576 
577     auto spansMap = mutableSpan->GetSpansMap();
578     EXPECT_EQ(spansMap.size(), 0);
579 
580     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontOne, 0, 10));
581     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontTwo, 3, 7));
582     mutableSpan->AddSpan(AceType::MakeRefPtr<FontSpan>(fontThree, 2, 5));
583     spansMap = mutableSpan->GetSpansMap();
584     auto fontSpans = spansMap[SpanType::Font];
585     EXPECT_EQ(fontSpans.size(), 4);
586     mutableSpan->RemoveSpan(-1, 10, SpanType::Font);
587     mutableSpan->RemoveSpan(3, 100, SpanType::Font);
588     mutableSpan->RemoveSpan(-100, 100, SpanType::Font);
589     spansMap = mutableSpan->GetSpansMap();
590     fontSpans = spansMap[SpanType::Font];
591     EXPECT_EQ(fontSpans.size(), 4);
592 }
593 
594 /**
595  * @tc.name: MutableSpanString006
596  * @tc.desc: Test combination of IsEqualToSpanString/GetSubSpanString/GetSpans/GetSpan
597  * @tc.type: FUNC
598  */
599 HWTEST_F(SpanStringTestNg, MutableSpanString006, TestSize.Level1)
600 {
601     Font fontOne { .fontColor = OHOS::Ace::Color::RED };
602     Font fontTwo { .fontColor = OHOS::Ace::Color::WHITE };
603     auto spanString1 = AceType::MakeRefPtr<SpanString>("0123456789");
604     spanString1->AddSpan(AceType::MakeRefPtr<FontSpan>(fontOne, 0, 3));
605     auto mutableSpanString1 = AceType::MakeRefPtr<MutableSpanString>("0123456789");
606     EXPECT_FALSE(spanString1->IsEqualToSpanString(mutableSpanString1));
607     mutableSpanString1->AddSpan(AceType::MakeRefPtr<FontSpan>(fontOne, 0, 3));
608     EXPECT_TRUE(spanString1->IsEqualToSpanString(mutableSpanString1));
609     mutableSpanString1->AddSpan(AceType::MakeRefPtr<FontSpan>(fontOne, 3, 7));
610     auto mutableSpanString2 = AceType::MakeRefPtr<MutableSpanString>("0123456789");
611     mutableSpanString2->AddSpan(AceType::MakeRefPtr<FontSpan>(fontOne, 0, 7));
612     EXPECT_TRUE(mutableSpanString2->IsEqualToSpanString(mutableSpanString1));
613     EXPECT_TRUE(spanString1->GetSubSpanString(0, 3)->IsEqualToSpanString(mutableSpanString2->GetSubSpanString(0, 3)));
614     mutableSpanString2->AddSpan(AceType::MakeRefPtr<FontSpan>(fontTwo, 7, 8));
615     mutableSpanString2->AddSpan(AceType::MakeRefPtr<FontSpan>(fontOne, 8, 9));
616     auto spanArr = mutableSpanString2->GetSpans(0, 9);
617     EXPECT_EQ(spanArr.size(), 3);
618 }
619 
620 /**
621  * @tc.name: MutableSpanString007
622  * @tc.desc: Test some edge case of InsertString/ReplaceString/RemoveString
623  * @tc.type: FUNC
624  */
625 HWTEST_F(SpanStringTestNg, MutableSpanString007, TestSize.Level1)
626 {
627     vector<OHOS::Ace::Color> colors = { Color::RED, Color::BLACK, Color::GREEN, Color::GRAY, Color::BLUE };
628     vector<Font> fonts;
629     auto spanString1 = AceType::MakeRefPtr<MutableSpanString>("01234");
630     for (int i = 0; i < colors.size(); i++) {
631         Font f;
632         f.fontColor = colors[i];
633         fonts.emplace_back(f);
634         spanString1->AddSpan(AceType::MakeRefPtr<FontSpan>(f, i, i + 1));
635     }
636     auto spanArr = spanString1->GetSpans(0, spanString1->GetLength());
637     EXPECT_EQ(spanArr.size(), colors.size());
638 
639     auto spanString2 = spanString1->GetSubSpanString(0, spanString1->GetLength());
640     EXPECT_TRUE(spanString2->IsEqualToSpanString(spanString1));
641     std::list<RefPtr<SpanBase>> resultList1 = { AceType::MakeRefPtr<FontSpan>(fonts[0], 0, 6),
642         AceType::MakeRefPtr<FontSpan>(fonts[1], 6, 7), AceType::MakeRefPtr<FontSpan>(fonts[2], 7, 8),
643         AceType::MakeRefPtr<FontSpan>(fonts[3], 8, 9), AceType::MakeRefPtr<FontSpan>(fonts[4], 9, 10) };
644     spanString1->InsertString(0, "一二三四五");
645     auto spanMap = spanString1->GetSpansMap();
646     EXPECT_TRUE(CompareSpanList(spanMap[SpanType::Font], resultList1));
647     spanString1->InsertString(6, "红红火火");
648     std::list<RefPtr<SpanBase>> resultList2 = { AceType::MakeRefPtr<FontSpan>(fonts[0], 0, 10),
649         AceType::MakeRefPtr<FontSpan>(fonts[1], 10, 11), AceType::MakeRefPtr<FontSpan>(fonts[2], 11, 12),
650         AceType::MakeRefPtr<FontSpan>(fonts[3], 12, 13), AceType::MakeRefPtr<FontSpan>(fonts[4], 13, 14) };
651     spanMap = spanString1->GetSpansMap();
652     EXPECT_TRUE(CompareSpanList(spanMap[SpanType::Font], resultList2));
653     spanString1->InsertString(11, "abcdefg");
654     spanString1->ReplaceString(8, 8, "A");
655     std::list<RefPtr<SpanBase>> resultList3 = { AceType::MakeRefPtr<FontSpan>(fonts[0], 0, 9),
656         AceType::MakeRefPtr<FontSpan>(fonts[1], 9, 11), AceType::MakeRefPtr<FontSpan>(fonts[2], 11, 12),
657         AceType::MakeRefPtr<FontSpan>(fonts[3], 12, 13), AceType::MakeRefPtr<FontSpan>(fonts[4], 13, 14) };
658     spanMap = spanString1->GetSpansMap();
659     EXPECT_TRUE(CompareSpanList(spanMap[SpanType::Font], resultList3));
660     EXPECT_EQ(spanString1->GetString(), "一二三四五0红红Afg234");
661     spanString1->RemoveString(1, 10);
662     std::list<RefPtr<SpanBase>> resultList4 = { AceType::MakeRefPtr<FontSpan>(fonts[0], 0, 1),
663         AceType::MakeRefPtr<FontSpan>(fonts[2], 1, 2), AceType::MakeRefPtr<FontSpan>(fonts[3], 2, 3),
664         AceType::MakeRefPtr<FontSpan>(fonts[4], 3, 4) };
665     spanMap = spanString1->GetSpansMap();
666     EXPECT_TRUE(CompareSpanList(spanMap[SpanType::Font], resultList4));
667 }
668 
669 /**
670  * @tc.name: MutableSpanString008
671  * @tc.desc: Test some edge case of InsertSpanString/ReplaceSpanString/AppendSpanString
672  * @tc.type: FUNC
673  */
674 HWTEST_F(SpanStringTestNg, MutableSpanString008, TestSize.Level1)
675 {
676     vector<OHOS::Ace::Color> colors = { Color::RED, Color::BLACK, Color::GREEN, Color::GRAY, Color::BLUE };
677     vector<Font> fonts;
678     auto spanString1 = AceType::MakeRefPtr<MutableSpanString>("0123");
679     for (int i = 0; i < 5; i++) {
680         Font f;
681         f.fontColor = colors[i];
682         fonts.emplace_back(f);
683         if (i != 4) {
684             spanString1->AddSpan(AceType::MakeRefPtr<FontSpan>(f, i, i + 1));
685         }
686     }
687     auto spanArr = spanString1->GetSpans(0, spanString1->GetLength());
688     auto spanString2 = AceType::MakeRefPtr<MutableSpanString>("abc");
689     Font f;
690     f.fontColor = colors[4];
691     spanString2->AddSpan(AceType::MakeRefPtr<FontSpan>(f, 0, 3));
692     spanString1->InsertSpanString(1, spanString2);
693     std::list<RefPtr<SpanBase>> resultList1 = { AceType::MakeRefPtr<FontSpan>(fonts[0], 0, 1),
694         AceType::MakeRefPtr<FontSpan>(fonts[4], 1, 4), AceType::MakeRefPtr<FontSpan>(fonts[1], 4, 5),
695         AceType::MakeRefPtr<FontSpan>(fonts[2], 5, 6), AceType::MakeRefPtr<FontSpan>(fonts[3], 6, 7) };
696     auto spanMap = spanString1->GetSpansMap();
697     EXPECT_TRUE(CompareSpanList(spanMap[SpanType::Font], resultList1));
698 
699     spanString1->InsertSpanString(0, spanString2);
700     std::list<RefPtr<SpanBase>> resultList2 = { AceType::MakeRefPtr<FontSpan>(fonts[4], 0, 3),
701         AceType::MakeRefPtr<FontSpan>(fonts[0], 3, 4), AceType::MakeRefPtr<FontSpan>(fonts[4], 4, 7),
702         AceType::MakeRefPtr<FontSpan>(fonts[1], 7, 8), AceType::MakeRefPtr<FontSpan>(fonts[2], 8, 9),
703         AceType::MakeRefPtr<FontSpan>(fonts[3], 9, 10) };
704     spanMap = spanString1->GetSpansMap();
705     EXPECT_TRUE(CompareSpanList(spanMap[SpanType::Font], resultList2));
706 
707     spanString1->ReplaceSpanString(0, 7, spanString2);
708     std::list<RefPtr<SpanBase>> resultList3 = { AceType::MakeRefPtr<FontSpan>(fonts[4], 0, 3),
709         AceType::MakeRefPtr<FontSpan>(fonts[1], 3, 4), AceType::MakeRefPtr<FontSpan>(fonts[2], 4, 5),
710         AceType::MakeRefPtr<FontSpan>(fonts[3], 5, 6) };
711     spanMap = spanString1->GetSpansMap();
712     EXPECT_TRUE(CompareSpanList(spanMap[SpanType::Font], resultList3));
713 
714     spanString1->AppendSpanString(spanString2);
715     std::list<RefPtr<SpanBase>> resultList4 = { AceType::MakeRefPtr<FontSpan>(fonts[4], 0, 3),
716         AceType::MakeRefPtr<FontSpan>(fonts[1], 3, 4), AceType::MakeRefPtr<FontSpan>(fonts[2], 4, 5),
717         AceType::MakeRefPtr<FontSpan>(fonts[3], 5, 6), AceType::MakeRefPtr<FontSpan>(fonts[4], 6, 9) };
718     spanMap = spanString1->GetSpansMap();
719     EXPECT_TRUE(CompareSpanList(spanMap[SpanType::Font], resultList4));
720 }
721 
722 /**
723  * @tc.name: GestureSpanString001
724  * @tc.desc: Test the construction of the gesture type in spanString
725  * @tc.type: FUNC
726  */
727 HWTEST_F(SpanStringTestNg, GestureSpanString001, TestSize.Level1)
728 {
729     /**
730      * @tc.steps: step1. Create spanBases and gestureInfo
731      */
732     std::vector<RefPtr<SpanBase>> spanBases;
733     GestureStyle gestureInfo;
734     ConstructGestureStyle(gestureInfo);
735     spanBases.emplace_back(AceType::MakeRefPtr<GestureSpan>(gestureInfo, 0, 3));
736     auto spanStringWithSpans = AceType::MakeRefPtr<SpanString>("01234567891");
737     spanStringWithSpans->BindWithSpans(spanBases);
738 
739     /**
740      * @tc.steps: step2. compare SpansMap and gestureInfo
741      * @tc.expect: The number of spanItems in the spanString is 2
742      */
743     auto spanMap = spanStringWithSpans->GetSpansMap();
744     std::list<RefPtr<SpanBase>> resultList = { AceType::MakeRefPtr<GestureSpan>(gestureInfo, 0, 3) };
745     EXPECT_FALSE(CompareSpanList(spanMap[SpanType::Gesture], resultList));
746     EXPECT_EQ(spanStringWithSpans->GetSpanItems().size(), 2);
747 }
748 
749 /**
750  * @tc.name: GestureSpanString002
751  * @tc.desc: Test the manifestations of the gesture type in the textPattern after it is constructed in spanString
752  * @tc.type: FUNC
753  */
754 HWTEST_F(SpanStringTestNg, GestureSpanString002, TestSize.Level1)
755 {
756     /**
757      * @tc.steps: step1. Create spanBases and gestureInfo
758      */
759     std::vector<RefPtr<SpanBase>> spanBases;
760     GestureStyle gestureInfo;
761     ConstructGestureStyle(gestureInfo);
762     spanBases.emplace_back(AceType::MakeRefPtr<GestureSpan>(gestureInfo, 0, 3));
763     spanBases.emplace_back(AceType::MakeRefPtr<GestureSpan>(gestureInfo, 8, 11));
764     auto spanStringWithSpans = AceType::MakeRefPtr<SpanString>("01234567891");
765     spanStringWithSpans->BindWithSpans(spanBases);
766 
767     std::list<RefPtr<SpanBase>> resultList = { AceType::MakeRefPtr<GestureSpan>(gestureInfo, 0, 3),
768         AceType::MakeRefPtr<GestureSpan>(gestureInfo, 8, 3) };
769     auto spanMap = spanStringWithSpans->GetSpansMap();
770 
771     EXPECT_FALSE(CompareSpanList(spanMap[SpanType::Gesture], resultList));
772 
773     /**
774      * @tc.steps: step2. Create textPattern and construct property string scene for textPattern
775      */
776     auto textPattern = AceType::MakeRefPtr<TextPattern>();
777     auto frameNode = FrameNode::CreateFrameNode("Test", 1, textPattern);
778     textPattern->SetTextController(AceType::MakeRefPtr<TextController>());
779     textPattern->GetTextController()->SetPattern(AceType::WeakClaim(AceType::RawPtr(textPattern)));
780     auto textController = textPattern->GetTextController();
781     textController->SetStyledString(spanStringWithSpans);
782 
783     auto spans = spanStringWithSpans->GetSpanItems();
784     textPattern->SetSpanItemChildren(spans);
785     textPattern->SetSpanStringMode(true);
786 
787     /**
788      * @tc.steps: step2. Call the BeforeCreateLayoutWrapper function
789      * @tc.expect: The click and long press event of the textPattern is initialized
790      *             and the number of spanItems in the spanString is 2
791      */
792     textPattern->BeforeCreateLayoutWrapper();
793     EXPECT_EQ(textPattern->GetSpanItemChildren().size(), 3);
794     EXPECT_TRUE(textPattern->clickEventInitialized_);
795     EXPECT_NE(textPattern->longPressEvent_, nullptr);
796 }
797 
798 /**
799  * @tc.name: GestureSpanString003
800  * @tc.desc: Test some edge case of AddSpan
801  * @tc.type: FUNC
802  */
803 HWTEST_F(SpanStringTestNg, GestureSpanString03, TestSize.Level1)
804 {
805     /**
806      * @tc.steps: step1. Create spanString and textPattern
807      */
808     auto spanStringWithSpans = AceType::MakeRefPtr<SpanString>("01234567891");
809     auto textPattern = AceType::MakeRefPtr<TextPattern>();
810     auto frameNode = FrameNode::CreateFrameNode("Test", 1, textPattern);
811 
812     /**
813      * @tc.steps: step2. Call the AddSpan function
814      * @tc.expect: The number of spanBases for gesture types is 1
815      */
816     GestureStyle gestureInfo;
817     ConstructGestureStyle(gestureInfo);
818     auto spanBase = AceType::MakeRefPtr<GestureSpan>(gestureInfo, 8, 10);
819     spanStringWithSpans->AddSpan(spanBase);
820     auto spanMap = spanStringWithSpans->GetSpansMap();
821     EXPECT_EQ(spanMap[SpanType::Gesture].size(), 1);
822 
823     /**
824      * @tc.steps: step3. Call the BeforeCreateLayoutWrapper function of textPattern
825      * @tc.expect: The number of spans for text is 3 and second span has event
826      */
827     textPattern->SetTextController(AceType::MakeRefPtr<TextController>());
828     textPattern->GetTextController()->SetPattern(AceType::WeakClaim(AceType::RawPtr(textPattern)));
829     auto textController = textPattern->GetTextController();
830     textController->SetStyledString(spanStringWithSpans);
831     textPattern->SetSpanStringMode(true);
832     textPattern->BeforeCreateLayoutWrapper();
833 
834     auto spanItems = textPattern->GetSpanItemChildren();
835     EXPECT_EQ(spanItems.size(), 3);
836     EXPECT_TRUE(textPattern->clickEventInitialized_);
837     EXPECT_NE(textPattern->longPressEvent_, nullptr);
838     auto iter = spanItems.begin();
839     std::advance(iter, GESTURE_INDEX1);
840     EXPECT_NE((*iter)->onClick, nullptr);
841     EXPECT_NE((*iter)->onLongPress, nullptr);
842 }
843 
844 /**
845  * @tc.name: GestureSpanString004
846  * @tc.desc: Test some edge case of ReplaceString/RemoveString
847  * @tc.type: FUNC
848  */
849 HWTEST_F(SpanStringTestNg, GestureSpanString004, TestSize.Level1)
850 {
851     /**
852      * @tc.steps: step1. Create MutableSpanString and textPattern
853      */
854     GestureStyle gestureInfo;
855     ConstructGestureStyle(gestureInfo);
856     std::vector<RefPtr<SpanBase>> spanBases;
857     spanBases.emplace_back(AceType::MakeRefPtr<GestureSpan>(gestureInfo, 0, 3));
858     spanBases.emplace_back(AceType::MakeRefPtr<GestureSpan>(gestureInfo, 8, 11));
859     auto spanString = AceType::MakeRefPtr<MutableSpanString>("01234567891");
860     spanString->BindWithSpans(spanBases);
861     auto textPattern = AceType::MakeRefPtr<TextPattern>();
862     auto frameNode = FrameNode::CreateFrameNode("Test", 1, textPattern);
863     textPattern->SetTextController(AceType::MakeRefPtr<TextController>());
864     textPattern->GetTextController()->SetPattern(AceType::WeakClaim(AceType::RawPtr(textPattern)));
865     auto textController = textPattern->GetTextController();
866     textController->SetStyledString(spanString);
867 
868     auto spans = spanString->GetSpanItems();
869     textPattern->SetSpanItemChildren(spans);
870 
871     auto spanItems = textPattern->GetSpanItemChildren();
872     EXPECT_EQ(spanItems.size(), 3);
873     EXPECT_NE(spanItems.front()->onClick, nullptr);
874     EXPECT_NE(spanItems.front()->onLongPress, nullptr);
875 
876     /**
877      * @tc.steps: step2. Call the ReplaceString function
878      * @tc.expect: The number of spanItems for textPattern is 4 and the events for each span were as expected
879      */
880     spanString->ReplaceString(0, 2, "a");
881     spanItems = textPattern->GetSpanItemChildren();
882     EXPECT_EQ(spanItems.size(), 3);
883 
884     auto iter = spanItems.begin();
885     EXPECT_NE((*iter)->onClick, nullptr);
886     EXPECT_NE((*iter)->onLongPress, nullptr);
887     iter++;
888     EXPECT_EQ((*iter)->onClick, nullptr);
889     EXPECT_EQ((*iter)->onLongPress, nullptr);
890     iter++;
891     EXPECT_NE((*iter)->onClick, nullptr);
892     EXPECT_NE((*iter)->onLongPress, nullptr);
893 
894     /**
895      * @tc.steps: step3. Call the RemoveString function
896      * @tc.expect: The number of spanItems for textPattern is 3
897      */
898     spanString->RemoveString(7, 3);
899     textController->SetStyledString(spanString);
900     spanItems = textPattern->GetSpanItemChildren();
901     EXPECT_EQ(spanItems.size(), 2);
902     iter = spanItems.begin();
903     EXPECT_NE((*iter)->onClick, nullptr);
904     EXPECT_NE((*iter)->onLongPress, nullptr);
905     iter++;
906     EXPECT_EQ((*iter)->onClick, nullptr);
907     EXPECT_EQ((*iter)->onLongPress, nullptr);
908 }
909 
910 /**
911  * @tc.name: MutableSpanString009
912  * @tc.desc: Test imageSpan
913  * @tc.type: FUNC
914  */
915 HWTEST_F(SpanStringTestNg, MutableSpanString009, TestSize.Level1)
916 {
917     auto imageOption = SpanStringTestNg::GetImageOption("src/icon.png");
918     auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption);
919 
920     auto spans = mutableStr->GetSpans(0, 2);
921     EXPECT_TRUE(spans.size() == 0);
922 
923     spans = mutableStr->GetSpans(-1, 1);
924     EXPECT_TRUE(spans.size() == 0);
925 
926     spans = mutableStr->GetSpans(0, 1);
927     EXPECT_TRUE(spans.size() == 1);
928     auto imageSpan = AceType::MakeRefPtr<ImageSpan>(imageOption);
929     EXPECT_TRUE(spans[0]->IsAttributesEqual(imageSpan));
930 }
931 
932 /**
933  * @tc.name: MutableSpanString010
934  * @tc.desc: Test the insertString in the case of imageSpan
935  * @tc.type: FUNC
936  */
937 HWTEST_F(SpanStringTestNg, MutableSpanString010, TestSize.Level1)
938 {
939     auto imageOption = SpanStringTestNg::GetImageOption("src/icon.png");
940     auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption);
941 
942     mutableStr->InsertString(0, "123");
943     auto text = mutableStr->GetString();
944     EXPECT_TRUE(text == "123 ");
945     auto length = mutableStr->GetLength();
946     EXPECT_TRUE(length == 4);
947 
948     mutableStr->InsertString(4, "456");
949     text = mutableStr->GetString();
950     EXPECT_TRUE(text == "123 456");
951     length = mutableStr->GetLength();
952     EXPECT_TRUE(length == 7);
953 
954     auto spans = mutableStr->GetSpans(0, 7);
955     EXPECT_TRUE(spans.size() == 1);
956 
957     auto imageSpan = AceType::MakeRefPtr<ImageSpan>(imageOption);
958     EXPECT_TRUE(spans[0]->IsAttributesEqual(imageSpan));
959 }
960 
961 /**
962  * @tc.name: MutableSpanString011
963  * @tc.desc: Test the insertSpanString in the case of imageSpan
964  * @tc.type: FUNC
965  */
966 HWTEST_F(SpanStringTestNg, MutableSpanString011, TestSize.Level1)
967 {
968     auto imageOption = SpanStringTestNg::GetImageOption("src/icon.png");
969     auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption);
970 
971     auto spanStr = AceType::MakeRefPtr<SpanString>("123");
972     spanStr->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 3));
973     mutableStr->InsertSpanString(0, spanStr);
974     auto text = mutableStr->GetString();
975     EXPECT_EQ(text, "123 ");
976     auto length = mutableStr->GetLength();
977     EXPECT_EQ(length, 4);
978 
979     spanStr = AceType::MakeRefPtr<SpanString>("456");
980     spanStr->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont1, 0, 3));
981     mutableStr->InsertSpanString(4, spanStr);
982     text = mutableStr->GetString();
983     EXPECT_EQ(text, "123 456");
984     length = mutableStr->GetLength();
985     EXPECT_EQ(length, 7);
986     auto spans = mutableStr->GetSpans(0, 7);
987     EXPECT_EQ(spans.size(), 3);
988 }
989 
990 /**
991  * @tc.name: MutableSpanString012
992  * @tc.desc: Test the replaceSpan/addSpan in the case of imageSpan
993  * @tc.type: FUNC
994  */
995 HWTEST_F(SpanStringTestNg, MutableSpanString012, TestSize.Level1)
996 {
997     auto imageOption = SpanStringTestNg::GetImageOption("src/icon-1.png");
998     auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption);
999     mutableStr->InsertString(0, "123");
1000     mutableStr->InsertString(4, "456");
1001 
1002     auto imageOption1 = SpanStringTestNg::GetImageOption("src/icon-2.png");
1003     auto imageSpan1 = AceType::MakeRefPtr<ImageSpan>(imageOption1);
1004     mutableStr->ReplaceSpan(3, 1, imageSpan1);
1005     auto length = mutableStr->GetLength();
1006     EXPECT_TRUE(length == 7);
1007     auto spans = mutableStr->GetSpans(0, 7);
1008     EXPECT_TRUE(spans[0]->IsAttributesEqual(imageSpan1));
1009 
1010     auto imageOption2 = SpanStringTestNg::GetImageOption("src/icon-3.png");
1011     auto imageSpan2 = AceType::MakeRefPtr<ImageSpan>(imageOption2);
1012     imageSpan2->UpdateStartIndex(3);
1013     imageSpan2->UpdateEndIndex(4);
1014     mutableStr->AddSpan(imageSpan2);
1015     spans = mutableStr->GetSpans(0, 7);
1016     EXPECT_TRUE(spans[0]->IsAttributesEqual(imageSpan2));
1017 }
1018 
1019 /**
1020  * @tc.name: MutableSpanString013
1021  * @tc.desc: Test the appendSpan/removeSpan in the case of imageSpan
1022  * @tc.type: FUNC
1023  */
1024 HWTEST_F(SpanStringTestNg, MutableSpanString013, TestSize.Level1)
1025 {
1026     auto imageOption = SpanStringTestNg::GetImageOption("src/icon-1.png");
1027     auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption);
1028     mutableStr->InsertString(0, "123");
1029     mutableStr->InsertString(4, "456");
1030     auto imageOption1 = SpanStringTestNg::GetImageOption("src/icon-2.png");
1031     auto imageSpan1 = AceType::MakeRefPtr<SpanString>(imageOption1);
1032     mutableStr->AppendSpanString(imageSpan1);
1033     auto spans = mutableStr->GetSpans(0, 7);
1034     EXPECT_EQ(spans.size(), 1);
1035     mutableStr->RemoveSpan(0, 7, SpanType::Image);
1036     spans = mutableStr->GetSpans(0, 6);
1037     EXPECT_EQ(spans.size(), 0);
1038     spans = mutableStr->GetSpans(0, 7);
1039     EXPECT_EQ(spans.size(), 1);
1040     mutableStr->RemoveSpans(0, 7);
1041     spans = mutableStr->GetSpans(0, 7);
1042     EXPECT_EQ(spans.size(), 0);
1043 }
1044 
1045 /**
1046  * @tc.name: MutableSpanString014
1047  * @tc.desc: Test basic function of LineHeightSpan/ParagraphStyleSpan
1048  * @tc.type: FUNC
1049  */
1050 HWTEST_F(SpanStringTestNg, MutableSpanString014, TestSize.Level1)
1051 {
1052     auto spanString = AceType::MakeRefPtr<MutableSpanString>("0123456789");
1053     SpanParagraphStyle spanParagraphStyle;
1054     spanParagraphStyle.align = TextAlign::END;
1055     spanParagraphStyle.maxLines = 4;
1056     spanParagraphStyle.wordBreak = WordBreak::BREAK_ALL;
1057     spanParagraphStyle.textOverflow = TextOverflow::ELLIPSIS;
1058     spanParagraphStyle.textIndent = Dimension(23);
1059     spanParagraphStyle.leadingMargin = LeadingMargin();
1060     spanParagraphStyle.leadingMargin->size = LeadingMarginSize(Dimension(25.0), Dimension(26.0));
1061     spanString->AddSpan(AceType::MakeRefPtr<ParagraphStyleSpan>(spanParagraphStyle, 0, 1));
1062     spanString->AddSpan(AceType::MakeRefPtr<LineHeightSpan>(Dimension(30), 0, 3));
1063     spanString->AddSpan(AceType::MakeRefPtr<LineHeightSpan>(Dimension(10), 0, 2));
1064 
1065     auto firstSpans = spanString->GetSpans(2, 1);
1066     EXPECT_EQ(firstSpans.size(), 1);
1067     auto lineHeightSpan = AceType::DynamicCast<LineHeightSpan>(firstSpans[0]);
1068     EXPECT_NE(lineHeightSpan, nullptr);
1069     EXPECT_EQ(lineHeightSpan->GetStartIndex(), 2);
1070     EXPECT_EQ(lineHeightSpan->GetEndIndex(), 3);
1071     EXPECT_EQ(lineHeightSpan->GetLineHeight(), Dimension(30));
1072 
1073     auto paraSpans = spanString->GetSpans(0, 2, SpanType::ParagraphStyle);
1074     EXPECT_EQ(paraSpans.size(), 1);
1075     auto paraSpan = AceType::DynamicCast<ParagraphStyleSpan>(paraSpans[0]);
1076     EXPECT_NE(paraSpan, nullptr);
1077     EXPECT_EQ(paraSpan->GetStartIndex(), 0);
1078     EXPECT_EQ(paraSpan->GetEndIndex(), 1);
1079     EXPECT_EQ(paraSpan->GetParagraphStyle().align, TextAlign::END);
1080     EXPECT_EQ(paraSpan->GetParagraphStyle().maxLines, 4);
1081     EXPECT_EQ(paraSpan->GetParagraphStyle().wordBreak, WordBreak::BREAK_ALL);
1082     EXPECT_EQ(paraSpan->GetParagraphStyle().textOverflow, TextOverflow::ELLIPSIS);
1083     EXPECT_EQ(paraSpan->GetParagraphStyle().textIndent, Dimension(23));
1084     EXPECT_EQ(paraSpan->GetParagraphStyle().leadingMargin.value().size.Width().ConvertToVp(), 25);
1085     EXPECT_EQ(paraSpan->GetParagraphStyle().leadingMargin.value().size.Height().ConvertToVp(), 26);
1086     auto secondSpans = spanString->GetSpans(0, 3);
1087     EXPECT_EQ(secondSpans.size(), 3);
1088     auto thirdSpans = spanString->GetSpans(0, 1);
1089     EXPECT_EQ(thirdSpans.size(), 2);
1090     auto fourthSpans = spanString->GetSpans(3, 1);
1091     EXPECT_EQ(fourthSpans.size(), 0);
1092     auto fifthSpans = spanString->GetSpans(0, 9);
1093     EXPECT_EQ(fifthSpans.size(), 3);
1094 }
1095 
1096 /**
1097  * @tc.name: MutableSpanString015
1098  * @tc.desc: Test isAttributesEqual of LineHeightSpan/ParagraphStyleSpan
1099  * @tc.type: FUNC
1100  */
1101 HWTEST_F(SpanStringTestNg, MutableSpanString015, TestSize.Level1)
1102 {
1103     SpanParagraphStyle spanParagraphStyle;
1104     spanParagraphStyle.align = TextAlign::END;
1105     spanParagraphStyle.maxLines = 4;
1106     spanParagraphStyle.wordBreak = WordBreak::BREAK_ALL;
1107     spanParagraphStyle.textOverflow = TextOverflow::ELLIPSIS;
1108     spanParagraphStyle.textIndent = Dimension(23);
1109     spanParagraphStyle.leadingMargin = LeadingMargin();
1110     spanParagraphStyle.leadingMargin->size = LeadingMarginSize(Dimension(25.0), Dimension(26.0));
1111     auto paraSpan = AceType::MakeRefPtr<ParagraphStyleSpan>(spanParagraphStyle, 0, 1);
1112     auto paraSpan2 = AceType::MakeRefPtr<ParagraphStyleSpan>(spanParagraphStyle, 0, 1);
1113     EXPECT_TRUE(paraSpan->IsAttributesEqual(paraSpan2));
1114 
1115     auto lineHeightSpan = AceType::MakeRefPtr<LineHeightSpan>(Dimension(30), 0, 3);
1116     auto lineHeightSpan2 = AceType::MakeRefPtr<LineHeightSpan>(Dimension(30), 0, 3);
1117     auto lineHeightSpan3 = AceType::MakeRefPtr<LineHeightSpan>(Dimension(25), 0, 3);
1118     EXPECT_TRUE(lineHeightSpan->IsAttributesEqual(lineHeightSpan2));
1119     EXPECT_FALSE(lineHeightSpan->IsAttributesEqual(lineHeightSpan3));
1120 }
1121 
1122 /**
1123  * @tc.name: MutableSpanString016
1124  * @tc.desc: Test AppendSpanString/ReplaceSpanString of LineHeightSpan/ParagraphStyleSpan
1125  * @tc.type: FUNC
1126  */
1127 HWTEST_F(SpanStringTestNg, MutableSpanString016, TestSize.Level1)
1128 {
1129     SpanParagraphStyle spanParagraphStyle;
1130     spanParagraphStyle.align = TextAlign::END;
1131     spanParagraphStyle.maxLines = 4;
1132     spanParagraphStyle.wordBreak = WordBreak::BREAK_ALL;
1133     spanParagraphStyle.textOverflow = TextOverflow::ELLIPSIS;
1134     spanParagraphStyle.textIndent = Dimension(23);
1135     spanParagraphStyle.leadingMargin = LeadingMargin();
1136     spanParagraphStyle.leadingMargin->size = LeadingMarginSize(Dimension(25.0), Dimension(26.0));
1137     auto paraSpan = AceType::MakeRefPtr<ParagraphStyleSpan>(spanParagraphStyle, 0, 1);
1138     auto lineHeightSpan = AceType::MakeRefPtr<LineHeightSpan>(Dimension(30), 0, 3);
1139 
1140     auto imageOption = SpanStringTestNg::GetImageOption("src/icon-1.png");
1141     auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption);
1142     auto mutableStr2 = AceType::MakeRefPtr<MutableSpanString>("123456");
1143     mutableStr->AddSpan(paraSpan);
1144     mutableStr2->AddSpan(lineHeightSpan);
1145     mutableStr->AppendSpanString(mutableStr2);
1146     EXPECT_EQ(mutableStr->GetString(), " 123456");
1147     auto spans = mutableStr->GetSpans(0, 7);
1148     EXPECT_EQ(spans.size(), 3);
1149     mutableStr->ReplaceSpanString(1, 1, mutableStr2);
1150     EXPECT_EQ(mutableStr->GetString(), " 12345623456");
1151 }
1152 
1153 /**
1154  * @tc.name: MutableSpanString017
1155  * @tc.desc: Test InsertSpanString of LineHeightSpan/ParagraphStyleSpan
1156  * @tc.type: FUNC
1157  */
1158 HWTEST_F(SpanStringTestNg, MutableSpanString017, TestSize.Level1)
1159 {
1160     SpanParagraphStyle spanParagraphStyle;
1161     spanParagraphStyle.align = TextAlign::END;
1162     spanParagraphStyle.maxLines = 4;
1163     spanParagraphStyle.wordBreak = WordBreak::BREAK_ALL;
1164     spanParagraphStyle.textOverflow = TextOverflow::ELLIPSIS;
1165     spanParagraphStyle.textIndent = Dimension(23);
1166     spanParagraphStyle.leadingMargin = LeadingMargin();
1167     spanParagraphStyle.leadingMargin->size = LeadingMarginSize(Dimension(25.0), Dimension(26.0));
1168     auto paraSpan = AceType::MakeRefPtr<ParagraphStyleSpan>(spanParagraphStyle, 0, 1);
1169 
1170     auto imageOption = SpanStringTestNg::GetImageOption("src/icon.png");
1171     auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption);
1172 
1173     auto spanStr = AceType::MakeRefPtr<SpanString>("123");
1174     spanStr->AddSpan(paraSpan);
1175     mutableStr->InsertSpanString(0, spanStr);
1176     auto text = mutableStr->GetString();
1177     EXPECT_EQ(text, "123 ");
1178     auto length = mutableStr->GetLength();
1179     EXPECT_EQ(length, 4);
1180 
1181     spanStr = AceType::MakeRefPtr<SpanString>("456");
1182     spanStr->AddSpan(AceType::MakeRefPtr<LineHeightSpan>(Dimension(30), 0, 3));
1183     mutableStr->InsertSpanString(4, spanStr);
1184     text = mutableStr->GetString();
1185     EXPECT_EQ(text, "123 456");
1186     length = mutableStr->GetLength();
1187     EXPECT_EQ(length, 7);
1188     auto spans = mutableStr->GetSpans(0, 7);
1189     EXPECT_EQ(spans.size(), 3);
1190 }
1191 
1192 /**
1193  * @tc.name: MutableSpanString018
1194  * @tc.desc: Test serialization and unserialization of SpanString
1195  * @tc.type: FUNC
1196  */
1197 HWTEST_F(SpanStringTestNg, MutableSpanString018, TestSize.Level1)
1198 {
1199     std::vector<uint8_t> buff;
1200     Font testFont { OHOS::Ace::FontWeight::BOLD, Dimension(29.0, DimensionUnit::PX), OHOS::Ace::FontStyle::ITALIC,
1201     std::vector<std::string>(test_str, test_str + 10), OHOS::Ace::Color::RED };
1202     Font testFont2 { OHOS::Ace::FontWeight::W300, Dimension(49.0, DimensionUnit::VP), OHOS::Ace::FontStyle::ITALIC,
1203     std::vector<std::string>(test_str, test_str + 5), OHOS::Ace::Color::BLUE };
1204     auto spanStr = AceType::MakeRefPtr<SpanString>("dddd当地经的123456");
1205     spanStr->AddSpan(AceType::MakeRefPtr<LineHeightSpan>(Dimension(30), 0, 3));
1206     spanStr->AddSpan(AceType::MakeRefPtr<LineHeightSpan>(Dimension(10), 0, 2));
1207     spanStr->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont, 1, 2));
1208     spanStr->AddSpan(AceType::MakeRefPtr<FontSpan>(testFont2, 4, 5));
1209     spanStr->AddSpan(AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(15), 8, 9));
1210     spanStr->AddSpan(AceType::MakeRefPtr<BaselineOffsetSpan>(Dimension(16), 9, 10));
1211     SpanParagraphStyle spanParagraphStyle;
1212     spanParagraphStyle.align = TextAlign::END;
1213     spanParagraphStyle.maxLines = 4;
1214     spanParagraphStyle.wordBreak = WordBreak::BREAK_ALL;
1215     spanParagraphStyle.textOverflow = TextOverflow::ELLIPSIS;
1216     spanParagraphStyle.textIndent = Dimension(23);
1217     spanParagraphStyle.leadingMargin = LeadingMargin();
1218     spanParagraphStyle.leadingMargin->size = LeadingMarginSize(Dimension(25.0), Dimension(26.0));
1219     spanStr->AddSpan(AceType::MakeRefPtr<ParagraphStyleSpan>(spanParagraphStyle, 10, 11));
1220     spanStr->EncodeTlv(buff);
1221     auto spanString2 = SpanString::DecodeTlv(buff);
1222     std::list<RefPtr<NG::SpanItem>> spans = spanString2->GetSpanItems();
1223 
1224     EXPECT_EQ(spans.size(), 10);
1225     EXPECT_EQ(spanStr->GetString(), "dddd当地经的123456");
1226     auto it = spans.begin();
1227     EXPECT_EQ((*it)->content, "d");
1228     EXPECT_EQ((*it)->interval.first, 0);
1229     EXPECT_EQ((*it)->interval.second, 1);
1230     EXPECT_EQ((*it)->textLineStyle->GetLineHeight().value(), Dimension(10));
1231     ++it;
1232     EXPECT_EQ((*it)->content, "d");
1233     EXPECT_EQ((*it)->interval.first, 1);
1234     EXPECT_EQ((*it)->interval.second, 2);
1235     EXPECT_EQ((*it)->fontStyle->GetFontSize().value(), Dimension(29));
1236     EXPECT_EQ((*it)->fontStyle->GetTextColor().value(), OHOS::Ace::Color::RED);
1237     EXPECT_EQ((*it)->fontStyle->GetItalicFontStyle().value(), OHOS::Ace::FontStyle::ITALIC);
1238     EXPECT_EQ((*it)->fontStyle->GetFontWeight().value(), OHOS::Ace::FontWeight::BOLD);
1239     EXPECT_EQ((*it)->textLineStyle->GetLineHeight().value(), Dimension(10));
1240     ++it;
1241     EXPECT_EQ((*it)->content, "d");
1242     EXPECT_EQ((*it)->interval.first, 2);
1243     EXPECT_EQ((*it)->interval.second, 3);
1244     EXPECT_EQ((*it)->textLineStyle->GetLineHeight().value(), Dimension(30));
1245     ++it;
1246     EXPECT_EQ((*it)->content, "d");
1247     EXPECT_EQ((*it)->interval.first, 3);
1248     EXPECT_EQ((*it)->interval.second, 4);
1249     ++it;
1250     EXPECT_EQ((*it)->content, "当");
1251     EXPECT_EQ((*it)->interval.first, 4);
1252     EXPECT_EQ((*it)->interval.second, 5);
1253     EXPECT_EQ((*it)->fontStyle->GetFontSize().value(), Dimension(49, OHOS::Ace::DimensionUnit::VP));
1254     EXPECT_EQ((*it)->fontStyle->GetTextColor().value(), OHOS::Ace::Color::BLUE);
1255     EXPECT_EQ((*it)->fontStyle->GetItalicFontStyle().value(), OHOS::Ace::FontStyle::ITALIC);
1256     EXPECT_EQ((*it)->fontStyle->GetFontWeight().value(), OHOS::Ace::FontWeight::W300);
1257     ++it;
1258     EXPECT_EQ((*it)->content, "地经的");
1259     EXPECT_EQ((*it)->interval.first, 5);
1260     EXPECT_EQ((*it)->interval.second, 8);
1261     ++it;
1262     EXPECT_EQ((*it)->content, "1");
1263     EXPECT_EQ((*it)->interval.first, 8);
1264     EXPECT_EQ((*it)->interval.second, 9);
1265     EXPECT_EQ((*it)->fontStyle->GetLetterSpacing().value(), Dimension(15));
1266     ++it;
1267     EXPECT_EQ((*it)->content, "2");
1268     EXPECT_EQ((*it)->interval.first, 9);
1269     EXPECT_EQ((*it)->interval.second, 10);
1270     EXPECT_EQ((*it)->textLineStyle->GetBaselineOffset().value(), Dimension(16));
1271     ++it;
1272     EXPECT_EQ((*it)->content, "3");
1273     EXPECT_EQ((*it)->interval.first, 10);
1274     EXPECT_EQ((*it)->interval.second, 11);
1275     EXPECT_EQ((*it)->textLineStyle->GetTextOverflow().value(), TextOverflow::ELLIPSIS);
1276     EXPECT_EQ((*it)->textLineStyle->GetTextAlign().value(), TextAlign::END);
1277     EXPECT_EQ((*it)->textLineStyle->GetMaxLines().value(), 4);
1278     EXPECT_EQ((*it)->textLineStyle->GetTextIndent().value(), Dimension(23));
1279     EXPECT_EQ((*it)->textLineStyle->GetWordBreak().value(), WordBreak::BREAK_ALL);
1280     ++it;
1281     EXPECT_EQ((*it)->content, "456");
1282     EXPECT_EQ((*it)->interval.first, 11);
1283     EXPECT_EQ((*it)->interval.second, 14);
1284 }
1285 
1286 /**
1287  * @tc.name: MutableSpanString019
1288  * @tc.desc: Test InsertSpanString of ExtSpan
1289  * @tc.type: FUNC
1290  */
1291 HWTEST_F(SpanStringTestNg, MutableSpanString019, TestSize.Level1)
1292 {
1293     auto extSpan = AceType::MakeRefPtr<ExtSpan>(1, 2);
1294     auto imageOption = SpanStringTestNg::GetImageOption("src/icon.png");
1295     auto mutableStr = AceType::MakeRefPtr<MutableSpanString>(imageOption);
1296     auto spanStr = AceType::MakeRefPtr<SpanString>("12345");
1297     spanStr->AddSpan(extSpan);
1298     mutableStr->InsertSpanString(0, spanStr);
1299     auto text = mutableStr->GetString();
1300     EXPECT_EQ(text, "12345 ");
1301     auto length = mutableStr->GetLength();
1302     EXPECT_EQ(length, 6);
1303     auto spans = mutableStr->GetSpans(0, 6);
1304     EXPECT_EQ(spans.size(), 2);
1305     spans = mutableStr->GetSpans(1, 3);
1306     EXPECT_EQ(spans.size(), 1);
1307     for (auto span : spans) {
1308         EXPECT_EQ(span->GetStartIndex(), 1);
1309         EXPECT_EQ(span->GetEndIndex(), 2);
1310     }
1311 }
1312 
1313 /**
1314  * @tc.name: SpanStringTest009
1315  * @tc.desc: Test basic function of span object
1316  * @tc.type: FUNC
1317  */
1318 HWTEST_F(SpanStringTestNg, SpanString009, TestSize.Level1)
1319 {
1320     std::string buffer;
1321     RefPtr<FontSpan> fontSpan = AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10);
1322     buffer = fontSpan->ToString();
1323     EXPECT_FALSE(buffer.empty());
1324     EXPECT_EQ(buffer.find("FontSpan"), 0);
1325 
1326     auto spanItem = AceType::MakeRefPtr<NG::SpanItem>();
1327     auto decorationSpan =
1328         AceType::MakeRefPtr<DecorationSpan>(TextDecoration::OVERLINE, Color::RED, TextDecorationStyle::WAVY, 0, 1);
1329     EXPECT_FALSE(fontSpan->IsAttributesEqual(decorationSpan));
1330     decorationSpan->ApplyToSpanItem(spanItem, SpanOperation::REMOVE);
1331     buffer.clear();
1332     buffer = decorationSpan->ToString();
1333     EXPECT_FALSE(buffer.empty());
1334     EXPECT_EQ(buffer.find("DecorationSpan"), 0);
1335     EXPECT_FALSE(decorationSpan->IsAttributesEqual(fontSpan));
1336 
1337     auto baselineOffsetSpan = AceType::MakeRefPtr<BaselineOffsetSpan>(Dimension(4), 0, 2);
1338     EXPECT_FALSE(baselineOffsetSpan->IsAttributesEqual(decorationSpan));
1339     baselineOffsetSpan->ApplyToSpanItem(spanItem, SpanOperation::REMOVE);
1340     buffer.clear();
1341     buffer = baselineOffsetSpan->ToString();
1342     EXPECT_FALSE(buffer.empty());
1343     EXPECT_EQ(buffer.find("BaselineOffsetSpan"), 0);
1344 
1345     auto letterSpacingSpan = AceType::MakeRefPtr<LetterSpacingSpan>(Dimension(5), 0, 3);
1346     EXPECT_FALSE(letterSpacingSpan->IsAttributesEqual(decorationSpan));
1347     letterSpacingSpan->ApplyToSpanItem(spanItem, SpanOperation::REMOVE);
1348     buffer.clear();
1349     buffer = letterSpacingSpan->ToString();
1350     EXPECT_FALSE(buffer.empty());
1351     EXPECT_EQ(buffer.find("LetterSpacingSpan"), 0);
1352 
1353     Shadow textShadow;
1354     textShadow.SetBlurRadius(1.0);
1355     textShadow.SetColor(Color::BLACK);
1356     textShadow.SetOffsetX(6.0);
1357     textShadow.SetOffsetY(6.0);
1358     vector<Shadow> textShadows { textShadow };
1359     vector<Shadow> textShadows2;
1360     textShadow.SetColor(Color::RED);
1361     vector<Shadow> textShadows3 {textShadow};
1362     auto textShadowSpan = AceType::MakeRefPtr<TextShadowSpan>(textShadows, 7, 9);
1363     auto textShadowSpan2 = AceType::MakeRefPtr<TextShadowSpan>(textShadows2, 7, 9);
1364     auto textShadowSpan3 = AceType::MakeRefPtr<TextShadowSpan>(textShadows3, 7, 9);
1365     EXPECT_FALSE(textShadowSpan->IsAttributesEqual(decorationSpan));
1366     EXPECT_FALSE(textShadowSpan->IsAttributesEqual(textShadowSpan2));
1367     EXPECT_FALSE(textShadowSpan->IsAttributesEqual(textShadowSpan3));
1368     textShadowSpan->ApplyToSpanItem(spanItem, SpanOperation::REMOVE);
1369     buffer = textShadowSpan->ToString();
1370     EXPECT_FALSE(buffer.empty());
1371     EXPECT_EQ(buffer.find("TextShadowSpan"), 0);
1372 }
1373 
1374 /**
1375  * @tc.name: SpanStringTest010
1376  * @tc.desc: Test basic function of span object
1377  * @tc.type: FUNC
1378  */
1379 HWTEST_F(SpanStringTestNg, SpanString010, TestSize.Level1)
1380 {
1381     std::string buffer;
1382     auto spanItem = AceType::MakeRefPtr<NG::SpanItem>();
1383     auto imageSpanItem = AceType::MakeRefPtr<NG::ImageSpanItem>();
1384     auto imageOption = SpanStringTestNg::GetImageOption("src/icon.png");
1385     auto imageSpan = AceType::MakeRefPtr<ImageSpan>(imageOption);
1386     imageSpan->ApplyToSpanItem(spanItem, SpanOperation::ADD);
1387     imageSpan->ApplyToSpanItem(imageSpanItem, SpanOperation::ADD);
1388     imageSpan->ApplyToSpanItem(imageSpanItem, SpanOperation::REMOVE);
1389     imageSpan->GetSubSpan(0, 3);
1390     buffer = imageSpan->ToString();
1391     EXPECT_TRUE(buffer.empty());
1392 
1393     auto customSpan = AceType::MakeRefPtr<CustomSpan>();
1394     auto customSpanItem = AceType::MakeRefPtr<NG::CustomSpanItem>();
1395     customSpan->ApplyToSpanItem(spanItem, SpanOperation::ADD);
1396     customSpan->ApplyToSpanItem(customSpanItem, SpanOperation::ADD);
1397     customSpan->ApplyToSpanItem(customSpanItem, SpanOperation::REMOVE);
1398     buffer = customSpan->ToString();
1399     EXPECT_FALSE(buffer.empty());
1400     EXPECT_EQ(buffer.find("CustomSpan"), 0);
1401 
1402     RefPtr<FontSpan> fontSpan = AceType::MakeRefPtr<FontSpan>(testFont1, 0, 10);
1403     auto paragraphStyleSpan = AceType::MakeRefPtr<ParagraphStyleSpan>();
1404     paragraphStyleSpan->ApplyToSpanItem(spanItem, SpanOperation::REMOVE);
1405     EXPECT_FALSE(paragraphStyleSpan->IsAttributesEqual(fontSpan));
1406     buffer = paragraphStyleSpan->ToString();
1407     EXPECT_TRUE(buffer.empty());
1408 
1409     auto lineHeightSpan = AceType::MakeRefPtr<LineHeightSpan>();
1410     EXPECT_FALSE(lineHeightSpan->IsAttributesEqual(fontSpan));
1411     buffer = lineHeightSpan->ToString();
1412     EXPECT_FALSE(buffer.empty());
1413     EXPECT_EQ(buffer.find("LineHeightSpan"), 0);
1414 
1415     GestureStyle gestureInfo;
1416     auto gestureSpan = AceType::MakeRefPtr<GestureSpan>(gestureInfo, 0, 3);
1417     EXPECT_FALSE(gestureSpan->IsAttributesEqual(lineHeightSpan));
1418     gestureSpan->AddSpanStyle(spanItem);
__anon317ed4c20602(const BaseEventInfo* info) 1419     auto onClick = [](const BaseEventInfo* info) {};
__anon317ed4c20702(GestureEvent& info) 1420     auto tmpClickFunc = [func = std::move(onClick)](GestureEvent& info) { func(&info); };
1421     gestureInfo.onClick = std::move(tmpClickFunc);
1422     gestureSpan->AddSpanStyle(spanItem);
1423 }
1424 
1425 /**
1426  * @tc.name: SpanStringTest011
1427  * @tc.desc: Test basic function of BackgroundColorSpan
1428  * @tc.type: FUNC
1429  */
1430 HWTEST_F(SpanStringTestNg, SpanString011, TestSize.Level1)
1431 {
1432     auto spanString = AceType::MakeRefPtr<MutableSpanString>("0123456789");
1433 
1434     TextBackgroundStyle textBackgroundStyle;
1435     NG::BorderRadiusProperty borderRadius;
1436     borderRadius.radiusTopLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
1437     borderRadius.radiusTopRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
1438     borderRadius.radiusBottomLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
1439     borderRadius.radiusBottomRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
1440     textBackgroundStyle.backgroundColor = Color::RED;;
1441     textBackgroundStyle.backgroundRadius = borderRadius;
1442 
1443     spanString->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle, 7, 9));
1444     auto firstSpans = spanString->GetSpans(2, 1);
1445     EXPECT_EQ(firstSpans.size(), 0);
1446     auto backgroundColorSpan = AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle);
1447     EXPECT_NE(backgroundColorSpan, nullptr);
1448     EXPECT_EQ(backgroundColorSpan->GetStartIndex(), 0);
1449     EXPECT_EQ(backgroundColorSpan->GetEndIndex(), 0);
1450 
1451     auto secondSpans = spanString->GetSpans(1, 1);
1452     EXPECT_EQ(secondSpans.size(), 0);
1453 
1454     auto thirdSpans = spanString->GetSpans(0, 1);
1455     EXPECT_EQ(thirdSpans.size(), 0);
1456 
1457     auto fourthSpans = spanString->GetSpans(3, 1);
1458     EXPECT_EQ(fourthSpans.size(), 0);
1459 
1460     auto fifthSpans = spanString->GetSpans(0, 9);
1461     EXPECT_EQ(fifthSpans.size(), 1);
1462 }
1463 
1464 /**
1465  * @tc.name: SpanStringTest012
1466  * @tc.desc: Test basic function of BackgroundColorSpan
1467  * @tc.type: FUNC
1468  */
1469 HWTEST_F(SpanStringTestNg, SpanString012, TestSize.Level1)
1470 {
1471     auto spanString = AceType::MakeRefPtr<MutableSpanString>("0123456789");
1472 
1473     TextBackgroundStyle textBackgroundStyle;
1474     NG::BorderRadiusProperty borderRadius;
1475     borderRadius.radiusTopLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
1476     borderRadius.radiusTopRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
1477     borderRadius.radiusBottomLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
1478     borderRadius.radiusBottomRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
1479 
1480     textBackgroundStyle.backgroundColor = Color::RED;;
1481     textBackgroundStyle.backgroundRadius = borderRadius;
1482 
1483     spanString->AddSpan(AceType::MakeRefPtr<BackgroundColorSpan>(textBackgroundStyle, 8, 10));
1484     auto subSpanString = spanString->GetSubSpanString(0, 10);
1485     EXPECT_TRUE(subSpanString->IsEqualToSpanString(spanString));
1486 
1487     auto firstSpans = spanString->GetSpans(8, 1);
1488     EXPECT_EQ(firstSpans.size(), 1);
1489     auto backgroundColorSpan = AceType::DynamicCast<BackgroundColorSpan>(firstSpans[0]);
1490     EXPECT_NE(backgroundColorSpan, nullptr);
1491     EXPECT_EQ(backgroundColorSpan->GetStartIndex(), 8);
1492     EXPECT_EQ(backgroundColorSpan->GetEndIndex(), 9);
1493     EXPECT_TRUE(backgroundColorSpan->GetBackgroundColor() == textBackgroundStyle);
1494 }
1495 
1496 /**
1497  * @tc.name: SpanStringTest013
1498  * @tc.desc: Test basic function of UrlSpan
1499  * @tc.type: FUNC
1500  */
1501 HWTEST_F(SpanStringTestNg, SpanString013, TestSize.Level1)
1502 {
1503     auto spanString = AceType::MakeRefPtr<MutableSpanString>("1234567890");
1504 
1505     std::string address = "https://www.example.com";
1506 
1507     spanString->AddSpan(AceType::MakeRefPtr<UrlSpan>(address, 7, 9));
1508     auto firstSpans = spanString->GetSpans(2, 1);
1509     EXPECT_EQ(firstSpans.size(), 0);
1510     auto urlSpan = AceType::MakeRefPtr<UrlSpan>(address);
1511     EXPECT_NE(urlSpan, nullptr);
1512     EXPECT_EQ(urlSpan->GetStartIndex(), 0);
1513     EXPECT_EQ(urlSpan->GetEndIndex(), 0);
1514 
1515     auto secondUrlSpans = spanString->GetSpans(1, 1);
1516     EXPECT_EQ(secondUrlSpans.size(), 0);
1517 
1518     auto thirdUrlSpans = spanString->GetSpans(0, 1);
1519     EXPECT_EQ(thirdUrlSpans.size(), 0);
1520 
1521     auto fourthUrlSpans = spanString->GetSpans(3, 1);
1522     EXPECT_EQ(fourthUrlSpans.size(), 0);
1523 
1524     auto fifthUrlSpans = spanString->GetSpans(0, 9);
1525     EXPECT_EQ(fifthUrlSpans.size(), 1);
1526 }
1527 
1528 /**
1529  * @tc.name: SpanStringTest014
1530  * @tc.desc: Test basic function of UrlSpan
1531  * @tc.type: FUNC
1532  */
1533 HWTEST_F(SpanStringTestNg, SpanString014, TestSize.Level1)
1534 {
1535     auto spanString = AceType::MakeRefPtr<MutableSpanString>("0123456789");
1536     std::string address = "https://www.example.com";
1537     spanString->AddSpan(AceType::MakeRefPtr<UrlSpan>(address, 8, 10));
1538     auto subSpanString  = spanString->GetSubSpanString(0, 10);
1539     EXPECT_TRUE(subSpanString->IsEqualToSpanString(spanString));
1540     auto firstSpans = spanString->GetSpans(8, 1);
1541     EXPECT_EQ(firstSpans.size(), 1);
1542     auto urlSpan = AceType::DynamicCast<UrlSpan>(firstSpans[0]);
1543     EXPECT_NE(urlSpan, nullptr);
1544     EXPECT_EQ(urlSpan->GetStartIndex(), 8);
1545     EXPECT_EQ(urlSpan->GetEndIndex(), 9);
1546     EXPECT_TRUE(urlSpan->GetUrlSpanAddress() == address);
1547 }
1548 
1549 /**
1550  * @tc.name: SpanStringTest015
1551  * @tc.desc: Test basic function of UrlSpan
1552  * @tc.type: FUNC
1553  */
1554 HWTEST_F(SpanStringTestNg, SpanString015, TestSize.Level1)
1555 {
1556     auto spanString = AceType::MakeRefPtr<MutableSpanString>("0123456789");
1557     std::string address = "https://www.example.com";
1558     spanString->AddSpan(AceType::MakeRefPtr<UrlSpan>(address, 0, 10));
1559     auto subSpanString  = spanString->GetSubSpanString(0, 10);
1560     EXPECT_TRUE(subSpanString->IsEqualToSpanString(spanString));
1561     auto firstSpans = spanString->GetSpans(0, 10);
1562     EXPECT_EQ(firstSpans.size(), 1);
1563     auto urlSpan = AceType::DynamicCast<UrlSpan>(firstSpans[0]);
1564     EXPECT_NE(urlSpan, nullptr);
1565     EXPECT_EQ(urlSpan->GetStartIndex(), 0);
1566     EXPECT_EQ(urlSpan->GetEndIndex(), 10);
1567     EXPECT_TRUE(urlSpan->GetUrlSpanAddress() == address);
1568 }
1569 
1570 /**
1571  * @tc.name: Tlv001
1572  * @tc.desc: Test basic function of TLV
1573  * @tc.type: FUNC
1574  */
1575 HWTEST_F(SpanStringTestNg, Tlv001, TestSize.Level1)
1576 {
1577     std::vector<uint8_t> buffer;
1578     std::vector<std::string> writeFontFamily = { "f1", "f2" };
1579     std::vector<uint8_t> result = { 0x25, 0x2, 0x20, 0x2, 0x66, 0x31, 0x20, 0x2, 0x66, 0x32 };
1580     TLVUtil::WriteFontFamily(buffer, writeFontFamily);
1581     EXPECT_TRUE(buffer == result);
1582 
1583     int32_t cursor = 0;
1584     std::vector<std::string> readFontFamily = TLVUtil::ReadFontFamily(buffer, cursor);
1585     EXPECT_TRUE(writeFontFamily == readFontFamily);
1586     buffer.clear();
1587     readFontFamily.clear();
1588     cursor = 0;
1589     readFontFamily = TLVUtil::ReadFontFamily(buffer, cursor);
1590     EXPECT_TRUE(readFontFamily.empty());
1591 }
1592 
1593 /**
1594  * @tc.name: Tlv002
1595  * @tc.desc: Test basic function of TLV
1596  * @tc.type: FUNC
1597  */
1598 HWTEST_F(SpanStringTestNg, Tlv002, TestSize.Level1)
1599 {
1600     std::vector<uint8_t> buffer;
1601     Shadow textShadow1;
1602     textShadow1.SetBlurRadius(2.0);
1603     textShadow1.SetColor(Color::BLACK);
1604     textShadow1.SetOffsetX(8.0);
1605     textShadow1.SetOffsetY(8.0);
1606     std::vector<uint8_t> result = { 0x23, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x22, 0xff, 0x0, 0x0, 0x0, 0x0,
1607         0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x40, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x40 };
1608     TLVUtil::WriteTextShadow(buffer, textShadow1);
1609     EXPECT_TRUE(buffer == result);
1610 
1611     int32_t cursor = 0;
1612     Shadow readShadow = TLVUtil::ReadTextShadow(buffer, cursor);
1613     EXPECT_TRUE(textShadow1 == readShadow);
1614     buffer.clear();
1615     Shadow errShadow = TLVUtil::ReadTextShadow(buffer, cursor);
1616     EXPECT_FALSE(textShadow1 == errShadow);
1617 
1618     std::vector<Shadow> writeShadows = { textShadow1 };
1619     std::vector<uint8_t> result2 = { 0x26, 0x1, 0x23, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x22, 0xff, 0x0,
1620         0x0, 0x0, 0x0, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x40, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x40 };
1621     buffer.clear();
1622     TLVUtil::WriteTextShadows(buffer, writeShadows);
1623     EXPECT_TRUE(buffer == result2);
1624 
1625     cursor = 0;
1626     std::vector<Shadow> readShadows = TLVUtil::ReadTextShadows(buffer, cursor);
1627     EXPECT_TRUE(writeShadows == readShadows);
1628     buffer.clear();
1629     cursor = 0;
1630     std::vector<Shadow> errShadows = TLVUtil::ReadTextShadows(buffer, cursor);
1631     EXPECT_TRUE(errShadows.empty());
1632 }
1633 
1634 /**
1635  * @tc.name: Tlv003
1636  * @tc.desc: Test basic function of TLV
1637  * @tc.type: FUNC
1638  */
1639 HWTEST_F(SpanStringTestNg, Tlv003, TestSize.Level1)
1640 {
1641     std::vector<uint8_t> buffer;
1642     std::list<std::pair<std::string, int32_t>> writeFontFeature = { { "f1", 1 }, { "f2", 2 } };
1643     std::vector<uint8_t> result = { 0x29, 0x2, 0x20, 0x2, 0x66, 0x31, 0x1, 0x20, 0x2, 0x66, 0x32, 0x2 };
1644     TLVUtil::WriteFontFeature(buffer, writeFontFeature);
1645     EXPECT_TRUE(buffer == result);
1646 
1647     int32_t cursor = 0;
1648     std::list<std::pair<std::string, int32_t>> readFontFeature = TLVUtil::ReadFontFeature(buffer, cursor);
1649     EXPECT_TRUE(writeFontFeature == readFontFeature);
1650     buffer.clear();
1651     readFontFeature.clear();
1652     cursor = 0;
1653     readFontFeature = TLVUtil::ReadFontFeature(buffer, cursor);
1654     EXPECT_TRUE(readFontFeature.empty());
1655 }
1656 
1657 /**
1658  * @tc.name: Tlv004
1659  * @tc.desc: Test basic function of TLV
1660  * @tc.type: FUNC
1661  */
1662 HWTEST_F(SpanStringTestNg, Tlv004, TestSize.Level1)
1663 {
1664     std::vector<uint8_t> buffer;
1665     NG::BorderRadiusProperty writeBorderRadiusProperty;
1666     writeBorderRadiusProperty.SetRadius(2.0_vp);
1667     std::vector<uint8_t> result = { 0x27, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x1, 0x24, 0x21, 0x0,
1668         0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x1, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x1, 0x24, 0x21,
1669         0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x1 };
1670     TLVUtil::WriteBorderRadiusProperty(buffer, writeBorderRadiusProperty);
1671     EXPECT_TRUE(buffer == result);
1672 
1673     int32_t cursor = 0;
1674     NG::BorderRadiusProperty readBorderRadiusProperty = TLVUtil::ReadBorderRadiusProperty(buffer, cursor);
1675     EXPECT_TRUE(writeBorderRadiusProperty == readBorderRadiusProperty);
1676     buffer.clear();
1677     cursor = 0;
1678     readBorderRadiusProperty = TLVUtil::ReadBorderRadiusProperty(buffer, cursor);
1679     EXPECT_FALSE(writeBorderRadiusProperty == readBorderRadiusProperty);
1680 }
1681 
1682 /**
1683  * @tc.name: Tlv005
1684  * @tc.desc: Test basic function of TLV
1685  * @tc.type: FUNC
1686  */
1687 HWTEST_F(SpanStringTestNg, Tlv005, TestSize.Level1)
1688 {
1689     std::vector<uint8_t> buffer;
1690     RefPtr<Ace::PixelMap> writePixelMap = Ace::PixelMap::CreatePixelMap(nullptr);
1691     std::vector<uint8_t> result = { 0x28, 0x0 };
1692     TLVUtil::WritePixelMap(buffer, writePixelMap);
1693     EXPECT_TRUE(buffer == result);
1694 
1695     int32_t cursor = 0;
1696     RefPtr<Ace::PixelMap> readPixelMap = TLVUtil::ReadPixelMap(buffer, cursor);
1697     EXPECT_FALSE(writePixelMap == readPixelMap);
1698     buffer.clear();
1699     cursor = 0;
1700     readPixelMap = TLVUtil::ReadPixelMap(buffer, cursor);
1701     EXPECT_FALSE(writePixelMap == readPixelMap);
1702 }
1703 
1704 /**
1705  * @tc.name: Tlv006
1706  * @tc.desc: Test basic function of TLV
1707  * @tc.type: FUNC
1708  */
1709 HWTEST_F(SpanStringTestNg, Tlv006, TestSize.Level1)
1710 {
1711     std::vector<uint8_t> buffer;
1712     Dimension dim(8);
1713     CalcDimension writeCalcDimension = CalcDimension(dim);
1714     std::vector<uint8_t> result = { 0x2a, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x40, 0x0 };
1715     TLVUtil::WriteCalcDimension(buffer, writeCalcDimension);
1716     EXPECT_TRUE(buffer == result);
1717 
1718     int32_t cursor = 0;
1719     CalcDimension readCalcDimension = TLVUtil::ReadCalcDimension(buffer, cursor);
1720     EXPECT_TRUE(writeCalcDimension == readCalcDimension);
1721     buffer.clear();
1722     cursor = 0;
1723     readCalcDimension = TLVUtil::ReadCalcDimension(buffer, cursor);
1724     EXPECT_FALSE(writeCalcDimension == readCalcDimension);
1725 }
1726 
1727 /**
1728  * @tc.name: Tlv007
1729  * @tc.desc: Test basic function of TLV
1730  * @tc.type: FUNC
1731  */
1732 HWTEST_F(SpanStringTestNg, Tlv007, TestSize.Level1)
1733 {
1734     std::vector<uint8_t> buffer;
1735     NG::CalcLength writeCalcLength(8);
1736     std::vector<uint8_t> result = { 0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x40, 0x0 };
1737     TLVUtil::WriteCalcLength(buffer, writeCalcLength);
1738     EXPECT_TRUE(buffer == result);
1739 
1740     int32_t cursor = 0;
1741     NG::CalcLength readCalcLength = TLVUtil::ReadCalcLength(buffer, cursor);
1742     EXPECT_TRUE(writeCalcLength == readCalcLength);
1743     buffer.clear();
1744     cursor = 0;
1745     readCalcLength = TLVUtil::ReadCalcLength(buffer, cursor);
1746     EXPECT_FALSE(writeCalcLength == readCalcLength);
1747 }
1748 
1749 /**
1750  * @tc.name: Tlv008
1751  * @tc.desc: Test basic function of TLV
1752  * @tc.type: FUNC
1753  */
1754 HWTEST_F(SpanStringTestNg, Tlv008, TestSize.Level1)
1755 {
1756     std::vector<uint8_t> buffer;
1757     ImageSpanSize writeImageSpanSize { .width = 60.0_vp, .height = 60.0_vp };
1758     std::vector<uint8_t> result = { 0x42, 0x43, 0x2a, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4e, 0x40, 0x1, 0x44,
1759         0x2a, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4e, 0x40, 0x1, 0x45 };
1760     TLVUtil::WriteImageSpanSize(buffer, writeImageSpanSize);
1761     EXPECT_TRUE(buffer == result);
1762 
1763     int32_t cursor = 0;
1764     ImageSpanSize readImageSpanSize = TLVUtil::ReadImageSpanSize(buffer, cursor);
1765     EXPECT_TRUE(writeImageSpanSize == readImageSpanSize);
1766     buffer.clear();
1767     cursor = 0;
1768     readImageSpanSize = TLVUtil::ReadImageSpanSize(buffer, cursor);
1769     EXPECT_FALSE(writeImageSpanSize == readImageSpanSize);
1770 }
1771 
1772 /**
1773  * @tc.name: Tlv009
1774  * @tc.desc: Test basic function of TLV
1775  * @tc.type: FUNC
1776  */
1777 HWTEST_F(SpanStringTestNg, Tlv009, TestSize.Level1)
1778 {
1779     std::vector<uint8_t> buffer;
1780     NG::PaddingProperty writePaddingProperty;
1781     writePaddingProperty.left = CalcLength(5);
1782     writePaddingProperty.right = CalcLength(5);
1783     writePaddingProperty.top = CalcLength(8);
1784     writePaddingProperty.bottom = CalcLength(8);
1785     std::vector<uint8_t> result = { 0x46, 0x49, 0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0x40,
1786         0x0, 0x4a, 0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0x40, 0x0, 0x47, 0x2b, 0x20, 0x0,
1787         0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x40, 0x0, 0x48, 0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0,
1788         0x0, 0x0, 0x0, 0x20, 0x40, 0x0, 0x4b };
1789     TLVUtil::WritePaddingProperty(buffer, writePaddingProperty);
1790     EXPECT_TRUE(buffer == result);
1791 
1792     int32_t cursor = 0;
1793     NG::PaddingProperty readPaddingProperty = TLVUtil::ReadPaddingProperty(buffer, cursor);
1794     EXPECT_TRUE(writePaddingProperty == readPaddingProperty);
1795     buffer.clear();
1796     cursor = 0;
1797     readPaddingProperty = TLVUtil::ReadPaddingProperty(buffer, cursor);
1798     EXPECT_FALSE(writePaddingProperty == readPaddingProperty);
1799 }
1800 
1801 /**
1802  * @tc.name: Tlv010
1803  * @tc.desc: Test basic function of TLV
1804  * @tc.type: FUNC
1805  */
1806 HWTEST_F(SpanStringTestNg, Tlv010, TestSize.Level1)
1807 {
1808     std::vector<uint8_t> buffer;
1809     BorderRadiusProperty borderRadius;
1810     borderRadius.SetRadius(2.0_vp);
1811     MarginProperty margins;
1812     margins.SetEdges(CalcLength(10.0));
1813     PaddingProperty paddings;
1814     paddings.SetEdges(CalcLength(5.0));
1815     ImageSpanAttribute writeImageSpanAttribute { .paddingProp = paddings,
1816         .marginProp = margins,
1817         .borderRadius = borderRadius,
1818         .objectFit = ImageFit::COVER,
1819         .verticalAlign = VerticalAlign::BOTTOM };
1820     std::vector<uint8_t> result = { 0x3a, 0x3c, 0x2c, 0x3, 0x3d, 0x2d, 0x2, 0x3e, 0x46, 0x49, 0x2b, 0x20, 0x0, 0x24,
1821         0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x24, 0x40, 0x0, 0x4a, 0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0,
1822         0x0, 0x24, 0x40, 0x0, 0x47, 0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x24, 0x40, 0x0, 0x48,
1823         0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x24, 0x40, 0x0, 0x4b, 0x3f, 0x27, 0x24, 0x21, 0x0,
1824         0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x1, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x1, 0x24, 0x21,
1825         0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x1, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x1, 0x40,
1826         0x46, 0x49, 0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0x40, 0x0, 0x4a, 0x2b, 0x20, 0x0,
1827         0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0x40, 0x0, 0x47, 0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0,
1828         0x0, 0x0, 0x0, 0x14, 0x40, 0x0, 0x48, 0x2b, 0x20, 0x0, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0x40,
1829         0x0, 0x4b, 0x41 };
1830     TLVUtil::WriteImageSpanAttribute(buffer, writeImageSpanAttribute);
1831     EXPECT_TRUE(buffer == result);
1832 
1833     int32_t cursor = 0;
1834     ImageSpanAttribute readImageSpanAttribute = TLVUtil::ReadImageSpanAttribute(buffer, cursor);
1835     EXPECT_TRUE(writeImageSpanAttribute == readImageSpanAttribute);
1836     buffer.clear();
1837     cursor = 0;
1838     readImageSpanAttribute = TLVUtil::ReadImageSpanAttribute(buffer, cursor);
1839     EXPECT_FALSE(writeImageSpanAttribute == readImageSpanAttribute);
1840 }
1841 
1842 /**
1843  * @tc.name: Tlv011
1844  * @tc.desc: Test basic function of TLV
1845  * @tc.type: FUNC
1846  */
1847 HWTEST_F(SpanStringTestNg, Tlv011, TestSize.Level1)
1848 {
1849     std::vector<uint8_t> buffer;
1850     NG::LeadingMargin writeLeadingMargin;
1851     writeLeadingMargin.size = LeadingMarginSize(Dimension(12.0), Dimension(48.0));
1852     std::vector<uint8_t> result = { 0x4c, 0x24, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x28, 0x40, 0x0, 0x24, 0x21, 0x0,
1853         0x0, 0x0, 0x0, 0x0, 0x0, 0x48, 0x40, 0x0, 0x4e };
1854     TLVUtil::WriteLeadingMargin(buffer, writeLeadingMargin);
1855     EXPECT_TRUE(buffer == result);
1856 
1857     int32_t cursor = 0;
1858     NG::LeadingMargin readLeadingMargin = TLVUtil::ReadLeadingMargin(buffer, cursor);
1859     EXPECT_TRUE(writeLeadingMargin == readLeadingMargin);
1860     buffer.clear();
1861     cursor = 0;
1862     readLeadingMargin = TLVUtil::ReadLeadingMargin(buffer, cursor);
1863     EXPECT_FALSE(writeLeadingMargin == readLeadingMargin);
1864 }
1865 } // namespace OHOS::Ace::NG