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 "text_base.h"
17 
18 namespace OHOS::Ace::NG {
19 
onClickFunc(const BaseEventInfo * info)20 void TextBases::onClickFunc(const BaseEventInfo* info) {};
21 
onRemoteMessage()22 void TextBases::onRemoteMessage() {};
23 
OnDragDropFunction(const RefPtr<OHOS::Ace::DragEvent> &,const std::string &)24 void TextBases::OnDragDropFunction(const RefPtr<OHOS::Ace::DragEvent>&, const std::string&) {};
25 
OnDragStartFunction(const RefPtr<OHOS::Ace::DragEvent> &,const std::string &)26 DragDropBaseInfo TextBases::OnDragStartFunction(const RefPtr<OHOS::Ace::DragEvent>&, const std::string&)
27 {
28     DragDropBaseInfo temp;
29     return temp;
30 }
31 
SuppressMockParagraph()32 void TextBases::SuppressMockParagraph()
33 {
34     auto paragraph = MockParagraph::GetOrCreateMockParagraph();
35     EXPECT_CALL(*paragraph, PushStyle(_)).Times(AnyNumber());
36     EXPECT_CALL(*paragraph, AddText(_)).Times(AnyNumber());
37     EXPECT_CALL(*paragraph, PopStyle()).Times(AnyNumber());
38     EXPECT_CALL(*paragraph, Build()).Times(AnyNumber());
39     EXPECT_CALL(*paragraph, Layout(_)).Times(AnyNumber());
40     EXPECT_CALL(*paragraph, GetAlphabeticBaseline()).WillRepeatedly(Return(0.f));
41     EXPECT_CALL(*paragraph, GetHeight()).WillRepeatedly(Return(0.f));
42     EXPECT_CALL(*paragraph, GetLongestLine()).WillRepeatedly(Return(0.f));
43     EXPECT_CALL(*paragraph, GetMaxWidth()).WillRepeatedly(Return(0.f));
44     EXPECT_CALL(*paragraph, GetLineCount()).WillRepeatedly(Return(0.f));
45     EXPECT_CALL(*paragraph, GetTextWidth()).WillRepeatedly(Return(0.f));
46     std::vector<RectF> rects;
47     EXPECT_CALL(*paragraph, GetRectsForRange(_, _, _)).WillRepeatedly(SetArgReferee<2>(rects)); // 2 means second paras
48 }
49 
Init()50 std::pair<RefPtr<FrameNode>, RefPtr<TextPattern>> TextBases::Init()
51 {
52     auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
53     MockPipelineContext::GetCurrent()->SetThemeManager(themeManager);
54     EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<TextOverlayTheme>()));
55     TextModelNG textModelNG;
56     textModelNG.Create(CREATE_VALUE);
57     auto pattern = AceType::MakeRefPtr<TextPattern>();
58     auto frameNode = FrameNode::CreateFrameNode("Test", 1, pattern);
59     frameNode->geometryNode_ = AceType::MakeRefPtr<GeometryNode>();
60     pattern->AttachToFrameNode(frameNode);
61     auto pipeline = PipelineContext::GetCurrentContext();
62     pipeline->rootNode_ =
63         FrameNode::CreateFrameNodeWithTree(V2::ROOT_ETS_TAG, ROOT_NODE_ID, Referenced::MakeRefPtr<RootPattern>());
64 
65     auto clipboard = ClipboardProxy::GetInstance()->GetClipboard(pipeline->GetTaskExecutor());
66     pattern->clipboard_ = clipboard;
67     return { frameNode, pattern };
68 }
69 
TestUpdateScenario(const RefPtr<TextPattern> & pattern)70 void TextBases::TestUpdateScenario(const RefPtr<TextPattern>& pattern)
71 {
72     ASSERT_NE(pattern, nullptr);
73     auto layoutProperty = pattern->GetLayoutProperty<TextLayoutProperty>();
74     ASSERT_NE(layoutProperty, nullptr);
75     layoutProperty->UpdateFontSize(ADAPT_UPDATE_FONTSIZE_VALUE);
76     pattern->BeforeCreateLayoutWrapper();
77     auto host = pattern->GetHost();
78     ASSERT_NE(host, nullptr);
79     for (const auto& child : host->GetChildren()) {
80         auto spanNode = AceType::DynamicCast<SpanNode>(child);
81         ASSERT_NE(spanNode, nullptr);
82         auto inheritPropertyInfo = spanNode->CalculateInheritPropertyInfo();
83         auto iter = inheritPropertyInfo.find(PropertyInfo::FONTSIZE);
84         if (iter != inheritPropertyInfo.end()) {
85             EXPECT_EQ(spanNode->GetFontSize().value(), ADAPT_UPDATE_FONTSIZE_VALUE);
86         }
87     }
88 }
89 
ConstructSpanItemList1(std::list<RefPtr<SpanItem>> & spans)90 void TextBases::ConstructSpanItemList1(std::list<RefPtr<SpanItem>>& spans)
91 {
92     RefPtr<SpanItem> span1 = AceType::MakeRefPtr<SpanItem>();
93     RefPtr<SpanItem> span2 = AceType::MakeRefPtr<SpanItem>();
94     RefPtr<SpanItem> span3 = AceType::MakeRefPtr<SpanItem>();
95     RefPtr<SpanItem> span4 = AceType::MakeRefPtr<SpanItem>();
96     span1->content = MULTIPLE_SPAN1;
97     spans.emplace_back(span1);
98 
99     span2->content = MULTIPLE_SPAN2;
100     span2->textLineStyle->UpdateTextAlign(TextAlign::CENTER);
101     span2->textLineStyle->UpdateMaxLines(1);
102     spans.emplace_back(span2);
103 
104     span3->content = MULTIPLE_SPAN3;
105     span3->textLineStyle->UpdateTextAlign(TextAlign::END);
106     span3->textLineStyle->UpdateTextIndent(Dimension(20.0f));
107     span3->textLineStyle->UpdateWordBreak(WordBreak::BREAK_ALL);
108     span3->textLineStyle->UpdateTextOverflow(TextOverflow::ELLIPSIS);
109     spans.emplace_back(span3);
110 
111     span4->content = MULTIPLE_SPAN4;
112     spans.emplace_back(span4);
113 }
114 
SetUpTestSuite()115 void TextBases::SetUpTestSuite()
116 {
117     MockPipelineContext::SetUp();
118     MockContainer::SetUp();
119     MockContainer::Current()->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
120 }
121 
TearDownTestSuite()122 void TextBases::TearDownTestSuite()
123 {
124     MockPipelineContext::TearDown();
125     MockContainer::TearDown();
126 }
127 
SetUp()128 void TextBases::SetUp()
129 {
130     MockParagraph::GetOrCreateMockParagraph();
131 }
132 
TearDown()133 void TextBases::TearDown()
134 {
135     MockParagraph::TearDown();
136 }
137 
CreateSpanNodeWithSetDefaultProperty(const std::string & content)138 RefPtr<SpanNode> TextBases::CreateSpanNodeWithSetDefaultProperty(const std::string& content)
139 {
140     SpanModelNG spanModelNG;
141     spanModelNG.Create(content);
142     spanModelNG.SetFontSize(FONT_SIZE_VALUE);
143     spanModelNG.SetTextColor(TEXT_COLOR_VALUE);
144     spanModelNG.SetItalicFontStyle(ITALIC_FONT_STYLE_VALUE);
145     spanModelNG.SetFontWeight(FONT_WEIGHT_VALUE);
146     spanModelNG.SetFontFamily(FONT_FAMILY_VALUE);
147     spanModelNG.SetTextDecoration(TEXT_DECORATION_VALUE);
148     spanModelNG.SetTextDecorationColor(TEXT_DECORATION_COLOR_VALUE);
149     spanModelNG.SetTextCase(TEXT_CASE_VALUE);
150     spanModelNG.SetLetterSpacing(LETTER_SPACING);
151     spanModelNG.SetLineHeight(LINE_HEIGHT_VALUE);
152     return AceType::DynamicCast<SpanNode>(ViewStackProcessor::GetInstance()->Finish());
153 }
154 
CreateImageSpanNode(const ImageSpanNodeProperty & property)155 RefPtr<ImageSpanNode> TextBases::CreateImageSpanNode(const ImageSpanNodeProperty& property)
156 {
157     auto imageSpanNode = ImageSpanNode::GetOrCreateSpanNode(V2::IMAGE_ETS_TAG,
158         ElementRegister::GetInstance()->MakeUniqueId(), []() { return AceType::MakeRefPtr<ImagePattern>(); });
159     auto imageLayoutProperty = AceType::DynamicCast<ImageLayoutProperty>(imageSpanNode->GetLayoutProperty());
160     if (property.imageSrc.has_value()) {
161         imageLayoutProperty->UpdateImageSourceInfo(ImageSourceInfo(property.imageSrc.value()));
162     }
163     if (property.pixelMap.has_value()) {
164         imageLayoutProperty->UpdateImageSourceInfo(ImageSourceInfo(property.pixelMap.value()));
165     }
166     if (property.imageFit.has_value()) {
167         imageLayoutProperty->UpdateImageFit(property.imageFit.value());
168     }
169     if (property.verticalAlign.has_value()) {
170         imageLayoutProperty->UpdateVerticalAlign(property.verticalAlign.value());
171     }
172     if (property.margin.has_value()) {
173         auto geometryNode = imageSpanNode->GetGeometryNode();
174         geometryNode->UpdateMargin(property.margin.value());
175     }
176     return imageSpanNode;
177 }
178 
CreateTextParagraph(const std::string & createValue,const TestProperty & testProperty)179 RefPtr<FrameNode> TextBases::CreateTextParagraph(const std::string& createValue, const TestProperty& testProperty)
180 {
181     TextModelNG textModel;
182     textModel.Create(createValue);
183     if (testProperty.fontSizeValue.has_value()) {
184         textModel.SetFontSize(testProperty.fontSizeValue.value());
185     }
186     if (testProperty.textColorValue.has_value()) {
187         textModel.SetTextColor(testProperty.textColorValue.value());
188     }
189     if (testProperty.italicFontStyleValue.has_value()) {
190         textModel.SetItalicFontStyle(testProperty.italicFontStyleValue.value());
191     }
192     if (testProperty.fontWeightValue.has_value()) {
193         textModel.SetFontWeight(testProperty.fontWeightValue.value());
194     }
195     if (testProperty.fontFamilyValue.has_value()) {
196         textModel.SetFontFamily(testProperty.fontFamilyValue.value());
197     }
198     if (testProperty.textAlignValue.has_value()) {
199         textModel.SetTextAlign(testProperty.textAlignValue.value());
200     }
201     if (testProperty.textOverflowValue.has_value()) {
202         textModel.SetTextOverflow(testProperty.textOverflowValue.value());
203     }
204     if (testProperty.maxLinesValue.has_value()) {
205         textModel.SetMaxLines(testProperty.maxLinesValue.value());
206     }
207     if (testProperty.lineHeightValue.has_value()) {
208         textModel.SetLineHeight(testProperty.lineHeightValue.value());
209     }
210     if (testProperty.lineSpacingValue.has_value()) {
211         textModel.SetLineSpacing(testProperty.lineSpacingValue.value());
212     }
213     if (testProperty.textDecorationValue.has_value()) {
214         textModel.SetTextDecoration(testProperty.textDecorationValue.value());
215     }
216     if (testProperty.textDecorationColorValue.has_value()) {
217         textModel.SetTextDecorationColor(testProperty.textDecorationColorValue.value());
218     }
219     if (testProperty.baselineOffsetValue.has_value()) {
220         textModel.SetBaselineOffset(testProperty.baselineOffsetValue.value());
221     }
222     if (testProperty.textCaseValue.has_value()) {
223         textModel.SetTextCase(testProperty.textCaseValue.value());
224     }
225     if (testProperty.adaptMinFontSize.has_value()) {
226         textModel.SetAdaptMinFontSize(testProperty.adaptMinFontSize.value());
227     }
228     if (testProperty.adaptMaxFontSize.has_value()) {
229         textModel.SetAdaptMaxFontSize(testProperty.adaptMaxFontSize.value());
230     }
231     if (testProperty.letterSpacing.has_value()) {
232         textModel.SetLetterSpacing(testProperty.letterSpacing.value());
233     }
234     if (testProperty.textIndent.has_value()) {
235         textModel.SetTextIndent(testProperty.textIndent.value());
236     }
237     if (testProperty.wordBreak.has_value()) {
238         textModel.SetWordBreak(testProperty.wordBreak.value());
239     }
240     if (testProperty.lineBreakStrategy.has_value()) {
241         textModel.SetLineBreakStrategy(testProperty.lineBreakStrategy.value());
242     }
243 
244     RefPtr<UINode> element = ViewStackProcessor::GetInstance()->Finish(); // TextView pop
245     return AceType::DynamicCast<FrameNode>(element);
246 }
247 
SetContentModifier(TextContentModifier & textContentModifier)248 void TextBases::SetContentModifier(TextContentModifier& textContentModifier)
249 {
250     auto textFrameNode = FrameNode::CreateFrameNode(V2::TEXT_ETS_TAG, 0, AceType::MakeRefPtr<TextPattern>());
251     ASSERT_NE(textFrameNode, nullptr);
252     RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
253     ASSERT_NE(geometryNode, nullptr);
254     RefPtr<LayoutWrapperNode> layoutWrapper =
255         AceType::MakeRefPtr<LayoutWrapperNode>(textFrameNode, geometryNode, textFrameNode->GetLayoutProperty());
256     auto textPattern = textFrameNode->GetPattern<TextPattern>();
257     ASSERT_NE(textPattern, nullptr);
258     auto textLayoutProperty = textPattern->GetLayoutProperty<TextLayoutProperty>();
259     ASSERT_NE(textLayoutProperty, nullptr);
260     auto frameNode = layoutWrapper->GetHostNode();
261     auto pipeline = frameNode->GetContextRefPtr();
262     TextStyle textStyle = CreateTextStyleUsingTheme(
263         textLayoutProperty->GetFontStyle(), textLayoutProperty->GetTextLineStyle(), pipeline->GetTheme<TextTheme>());
264 
265     textContentModifier.SetFontWeight(Ace::FontWeight::W200);
266     textContentModifier.SetTextColor(TEXT_COLOR_VALUE);
267     textContentModifier.SetTextDecorationColor(TEXT_COLOR_VALUE);
268     Shadow textShadow;
269     textShadow.SetBlurRadius(BLURRADIUS_VALUE);
270     textShadow.SetColor(TEXT_COLOR_VALUE);
271     textShadow.SetSpreadRadius(SPREADRADIUS_VALUE);
272     textShadow.SetOffsetX(ADAPT_OFFSETX_VALUE);
273     textShadow.SetOffsetY(ADAPT_OFFSETY_VALUE);
274     textContentModifier.SetTextShadow({ textShadow });
275     textContentModifier.SetFontSize(ADAPT_FONT_SIZE_VALUE, textStyle);
276     textContentModifier.SetBaselineOffset(BASELINE_OFFSET_VALUE, textStyle);
277     OffsetF paintOffset;
278     textContentModifier.SetPrintOffset(paintOffset);
279 }
280 
SetPaintMethodModifier(TextPaintMethod & textPaintMethod)281 void TextBases::SetPaintMethodModifier(TextPaintMethod& textPaintMethod)
282 {
283     textPaintMethod.textContentModifier_->fontSize_ = ADAPT_FONT_SIZE_VALUE;
284     textPaintMethod.textContentModifier_->fontWeight_ = FontWeight::LIGHTER;
285     textPaintMethod.textContentModifier_->textColor_ = TEXT_COLOR_VALUE;
286     textPaintMethod.textContentModifier_->AddDefaultShadow();
287     textPaintMethod.textContentModifier_->textDecorationColorAlpha_ =
288         AceType::MakeRefPtr<AnimatablePropertyFloat>(1.0f);
289     textPaintMethod.textContentModifier_->textDecoration_ = TextDecoration::NONE;
290     textPaintMethod.textContentModifier_->baselineOffset_ = BASELINE_OFFSET_VALUE;
291 }
292 
UpdateTextLayoutProperty(RefPtr<TextLayoutProperty> textLayoutProperty)293 void TextBases::UpdateTextLayoutProperty(RefPtr<TextLayoutProperty> textLayoutProperty)
294 {
295     textLayoutProperty->UpdateTextOverflow(TextOverflow::MARQUEE);
296     textLayoutProperty->UpdateFontSize(ADAPT_FONT_SIZE_VALUE);
297     textLayoutProperty->UpdateFontWeight(Ace::FontWeight::W200);
298     textLayoutProperty->UpdateTextColor(TEXT_COLOR_VALUE);
299     Shadow textShadow;
300     textShadow.SetBlurRadius(BLURRADIUS_VALUE);
301     textShadow.SetColor(TEXT_COLOR_VALUE);
302     textShadow.SetSpreadRadius(SPREADRADIUS_VALUE);
303     textShadow.SetOffsetX(ADAPT_OFFSETX_VALUE);
304     textShadow.SetOffsetY(ADAPT_OFFSETY_VALUE);
305     textLayoutProperty->UpdateTextShadow({ textShadow });
306     textLayoutProperty->UpdateTextDecorationColor(TEXT_COLOR_VALUE);
307     textLayoutProperty->UpdateTextDecoration(TextDecoration::OVERLINE);
308     textLayoutProperty->UpdateBaselineOffset(ADAPT_BASE_LINE_OFFSET_VALUE);
309     textLayoutProperty->UpdateWordBreak(TEXT_WORD_BREAK);
310     textLayoutProperty->UpdateLineBreakStrategy(TEXT_LINE_BREAK_STRATEGY);
311 }
312 } // namespace OHOS::Ace::NG