!37409 text组件TDD用例补充

Merge pull request !37409 from jiangzhijun8/master
This commit is contained in:
openharmony_ci 2024-07-18 04:22:12 +00:00 committed by Gitee
commit a3d3c72c1a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -14,6 +14,8 @@
*/
#include "text_base.h"
#include "test/mock/core/render/mock_canvas_image.h"
#include "core/components_ng/render/adapter/pixelmap_image.h"
namespace OHOS::Ace::NG {
@ -491,4 +493,460 @@ HWTEST_F(TextTestFiveNg, GetTextHeight001, TestSize.Level1)
EXPECT_EQ(textPattern->GetTextHeight(0, false), 0.0);
EXPECT_EQ(textPattern->GetTextHeight(0, true), 0.0);
}
/**
* @tc.name: CheckHandleIsVisibleWithTransform001
* @tc.desc: test base_text_select_overlay.cpp CheckHandleIsVisibleWithTransform function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, CheckHandleIsVisibleWithTransform001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto frameNode = FrameNode::CreateFrameNode("Test", 1, pattern);
ASSERT_NE(frameNode, nullptr);
pattern->AttachToFrameNode(frameNode);
auto textSelectOverlay = pattern->selectOverlay_;
ASSERT_NE(textSelectOverlay, nullptr);
OffsetF startPoint(0, 0);
OffsetF endPoint(1, 1);
EXPECT_EQ(textSelectOverlay->CheckHandleIsVisibleWithTransform(startPoint, endPoint, 10.0), true);
startPoint.SetX(-11);
startPoint.SetY(-11);
endPoint.SetX(11);
endPoint.SetY(11);
EXPECT_EQ(textSelectOverlay->CheckHandleIsVisibleWithTransform(startPoint, endPoint, 10.0), false);
startPoint.SetX(0);
startPoint.SetY(0);
endPoint.SetX(11);
endPoint.SetY(11);
EXPECT_EQ(textSelectOverlay->CheckHandleIsVisibleWithTransform(startPoint, endPoint, 10.0), false);
startPoint.SetX(-11);
startPoint.SetY(-11);
endPoint.SetX(0);
endPoint.SetY(0);
EXPECT_EQ(textSelectOverlay->CheckHandleIsVisibleWithTransform(startPoint, endPoint, 10.0), false);
}
/**
* @tc.name: IsPointsInRegion001
* @tc.desc: test base_text_select_overlay.cpp IsPointsInRegion function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, IsPointsInRegion001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto textSelectOverlay = pattern->selectOverlay_;
ASSERT_NE(textSelectOverlay, nullptr);
std::vector<PointF> points;
RectF regionRect(1, 1, 4, 4);
EXPECT_EQ(textSelectOverlay->IsPointsInRegion(points, regionRect), true);
points.emplace_back(PointF(0, 0));
points.emplace_back(PointF(1, 1));
points.emplace_back(PointF(3, 3));
points.emplace_back(PointF(5, 5));
points.emplace_back(PointF(6, 6));
EXPECT_EQ(textSelectOverlay->IsPointsInRegion(points, regionRect), false);
}
/**
* @tc.name: CheckHandleCanPaintInHost001
* @tc.desc: test base_text_select_overlay.cpp CheckHandleCanPaintInHost function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, CheckHandleCanPaintInHost001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto frameNode = FrameNode::CreateFrameNode("Test", 1, pattern);
ASSERT_NE(frameNode, nullptr);
pattern->AttachToFrameNode(frameNode);
auto textSelectOverlay = pattern->selectOverlay_;
ASSERT_NE(textSelectOverlay, nullptr);
ASSERT_NE(frameNode->GetGeometryNode(), nullptr);
frameNode->GetGeometryNode()->SetFrameSize(SizeF(10, 10));
textSelectOverlay->isChangeToOverlayModeAtEdge_ = false;
RectF firstRect(3, 3, 5, 5);
RectF secondRect(4, 4, 6, 6);
EXPECT_EQ(textSelectOverlay->CheckHandleCanPaintInHost(firstRect, secondRect), true);
firstRect.SetRect(3, 3, 5, 5);
secondRect.SetRect(11, 11, 15, 15);
EXPECT_EQ(textSelectOverlay->CheckHandleCanPaintInHost(firstRect, secondRect), false);
frameNode->GetGeometryNode()->SetFrameSize(SizeF(3, 3));
firstRect.SetRect(11, 11, 15, 15);
secondRect.SetRect(3, 3, 5, 5);
EXPECT_EQ(textSelectOverlay->CheckHandleCanPaintInHost(firstRect, secondRect), false);
firstRect.SetRect(11, 11, 15, 15);
secondRect.SetRect(13, 13, 15, 15);
EXPECT_EQ(textSelectOverlay->CheckHandleCanPaintInHost(firstRect, secondRect), false);
}
/**
* @tc.name: IsTouchAtHandle001
* @tc.desc: test base_text_select_overlay.cpp IsTouchAtHandle function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, IsTouchAtHandle001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto textSelectOverlay = pattern->selectOverlay_;
ASSERT_NE(textSelectOverlay, nullptr);
auto manager = SelectContentOverlayManager::GetOverlayManager();
ASSERT_NE(manager, nullptr);
textSelectOverlay->OnBind(manager);
TouchEventInfo info0("touch0");
TouchLocationInfo locationInfo0(1);
locationInfo0.SetTouchType(TouchType::DOWN);
locationInfo0.SetLocalLocation(Offset(0, 0));
info0.AddTouchLocationInfo(std::move(locationInfo0));
EXPECT_EQ(textSelectOverlay->IsTouchAtHandle(info0), false);
TouchEventInfo info1("touch1");
TouchLocationInfo locationInfo1(1);
locationInfo1.SetTouchType(TouchType::UP);
locationInfo1.SetLocalLocation(Offset(0, 0));
info1.AddTouchLocationInfo(std::move(locationInfo1));
EXPECT_EQ(textSelectOverlay->IsTouchAtHandle(info1), false);
TouchEventInfo info2("touch2");
TouchLocationInfo locationInfo2(1);
locationInfo2.SetTouchType(TouchType::MOVE);
locationInfo2.SetLocalLocation(Offset(0, 0));
info2.AddTouchLocationInfo(std::move(locationInfo2));
EXPECT_EQ(textSelectOverlay->IsTouchAtHandle(info2), false);
TouchEventInfo info3("touch3");
TouchLocationInfo locationInfo3(1);
locationInfo3.SetTouchType(TouchType::UP);
locationInfo3.SetLocalLocation(Offset(0, 0));
locationInfo3.SetGlobalLocation(Offset(0, 0));
info3.AddTouchLocationInfo(std::move(locationInfo3));
EXPECT_EQ(textSelectOverlay->IsTouchAtHandle(info3), false);
}
/**
* @tc.name: CheckSwitchToMode001
* @tc.desc: test base_text_select_overlay.cpp CheckSwitchToMode function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, CheckSwitchToMode001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto textSelectOverlay = pattern->selectOverlay_;
ASSERT_NE(textSelectOverlay, nullptr);
EXPECT_EQ(textSelectOverlay->CheckSwitchToMode(HandleLevelMode::OVERLAY), true);
EXPECT_EQ(textSelectOverlay->CheckSwitchToMode(HandleLevelMode::EMBED), true);
}
/**
* @tc.name: DrawImage001
* @tc.desc: test text_content_modifier.cpp DrawImage function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, DrawImage001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<ImagePattern>();
ASSERT_NE(pattern, nullptr);
auto frameNode = FrameNode::CreateFrameNode("Test", 1, pattern);
ASSERT_NE(frameNode, nullptr);
pattern->AttachToFrameNode(frameNode);
auto textContentModifier =
AceType::MakeRefPtr<TextContentModifier>(std::optional<TextStyle>(TextStyle()), pattern);
ASSERT_NE(textContentModifier, nullptr);
RSCanvas canvas;
RectF rect(0, 0, 10, 10);
EXPECT_EQ(textContentModifier->DrawImage(frameNode, canvas, 0, 0, rect), false);
frameNode->layoutProperty_->margin_ = std::make_unique<MarginProperty>();
EXPECT_EQ(textContentModifier->DrawImage(frameNode, canvas, 0, 0, rect), false);
frameNode->layoutProperty_->margin_->left = CalcLength("0vp");
frameNode->layoutProperty_->margin_->top = CalcLength("0vp");
EXPECT_EQ(textContentModifier->DrawImage(frameNode, canvas, 0, 0, rect), false);
auto layoutProperty = frameNode->layoutProperty_;
frameNode->layoutProperty_ = nullptr;
EXPECT_EQ(textContentModifier->DrawImage(frameNode, canvas, 0, 0, rect), false);
frameNode->layoutProperty_ = layoutProperty;
pattern->altImage_ = AceType::MakeRefPtr<NG::MockCanvasImage>();
ASSERT_NE(pattern->altImage_, nullptr);
EXPECT_EQ(textContentModifier->DrawImage(frameNode, canvas, 0, 0, rect), false);
pattern->image_ = AceType::MakeRefPtr<NG::MockCanvasImage>();
ASSERT_NE(pattern->image_, nullptr);
EXPECT_EQ(textContentModifier->DrawImage(frameNode, canvas, 0, 0, rect), false);
auto geometryNode = frameNode->geometryNode_;
frameNode->geometryNode_ = nullptr;
EXPECT_EQ(textContentModifier->DrawImage(frameNode, canvas, 0, 0, rect), false);
frameNode->geometryNode_ = geometryNode;
auto& config = pattern->image_->GetPaintConfig();
config.isSvg_ = !config.isSvg_;
EXPECT_EQ(textContentModifier->DrawImage(frameNode, canvas, 0, 0, rect), true);
config.isSvg_ = !config.isSvg_;
pattern->image_ = AceType::MakeRefPtr<PixelMapImage>();
EXPECT_EQ(textContentModifier->DrawImage(frameNode, canvas, 0, 0, rect), true);
}
/**
* @tc.name: GetTextDirection001
* @tc.desc: test multiple_paragraph_layout_algorithm.cpp GetTextDirection function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, GetTextDirection001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto frameNode = FrameNode::CreateFrameNode("Test", 1, pattern);
ASSERT_NE(frameNode, nullptr);
pattern->AttachToFrameNode(frameNode);
std::string content = "Hello World";
auto layoutWrapper = frameNode->CreateLayoutWrapper(true, true);
ASSERT_NE(layoutWrapper, nullptr);
auto textLayoutProperty = AceType::DynamicCast<TextLayoutProperty>(layoutWrapper->GetLayoutProperty());
ASSERT_NE(textLayoutProperty, nullptr);
EXPECT_EQ(MultipleParagraphLayoutAlgorithm::GetTextDirection(
content, layoutWrapper.GetRawPtr()), TextDirection::LTR);
textLayoutProperty->UpdateLayoutDirection(TextDirection::LTR);
EXPECT_EQ(MultipleParagraphLayoutAlgorithm::GetTextDirection(
content, layoutWrapper.GetRawPtr()), TextDirection::LTR);
textLayoutProperty->UpdateLayoutDirection(TextDirection::RTL);
EXPECT_EQ(MultipleParagraphLayoutAlgorithm::GetTextDirection(
content, layoutWrapper.GetRawPtr()), TextDirection::RTL);
textLayoutProperty->UpdateLayoutDirection(TextDirection::AUTO);
AceApplicationInfo::GetInstance().isRightToLeft_ = !AceApplicationInfo::GetInstance().IsRightToLeft();
EXPECT_EQ(MultipleParagraphLayoutAlgorithm::GetTextDirection(
content, layoutWrapper.GetRawPtr()), TextDirection::RTL);
AceApplicationInfo::GetInstance().isRightToLeft_ = !AceApplicationInfo::GetInstance().IsRightToLeft();
}
/**
* @tc.name: InheritParentProperties001
* @tc.desc: test span_node.cpp InheritParentProperties function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, InheritParentProperties001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto frameNode = FrameNode::CreateFrameNode("Test", 1, pattern);
ASSERT_NE(frameNode, nullptr);
pattern->AttachToFrameNode(frameNode);
auto pipeline = PipelineContext::GetCurrentContext();
auto theme = AceType::MakeRefPtr<MockThemeManager>();
pipeline->SetThemeManager(theme);
EXPECT_CALL(*theme, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<TextTheme>()));
auto spanNode = AceType::MakeRefPtr<SpanNode>(1);
ASSERT_NE(spanNode, nullptr);
auto spanItem = spanNode->GetSpanItem();
ASSERT_NE(spanNode, nullptr);
EXPECT_EQ(spanItem->InheritParentProperties(frameNode, true), TextStyle());
}
/**
* @tc.name: AdaptMinFontSize001
* @tc.desc: test text_adapt_font_sizer.cpp AdaptMinFontSize function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, AdaptMinFontSize001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto frameNode = FrameNode::CreateFrameNode("Test", 1, pattern);
ASSERT_NE(frameNode, nullptr);
pattern->AttachToFrameNode(frameNode);
auto layoutWrapper = frameNode->CreateLayoutWrapper(true, true);
ASSERT_NE(layoutWrapper, nullptr);
auto textAdaptFontSizer = AceType::MakeRefPtr<TextLayoutAlgorithm>();
ASSERT_NE(textAdaptFontSizer, nullptr);
TextStyle textStyle;
std::string content;
Dimension stepUnit;
LayoutConstraintF contentConstraint;
EXPECT_EQ(textAdaptFontSizer->AdaptMinFontSize(
textStyle, content, stepUnit, contentConstraint, layoutWrapper.GetRawPtr()), true);
textStyle.SetAdaptTextSize(Dimension(100, DimensionUnit::PERCENT),
Dimension(10, DimensionUnit::PERCENT), Dimension(10, DimensionUnit::PERCENT));
contentConstraint.maxSize.SetWidth(-1.0);
contentConstraint.maxSize.SetHeight(-1.0);
EXPECT_EQ(textAdaptFontSizer->AdaptMinFontSize(
textStyle, content, stepUnit, contentConstraint, layoutWrapper.GetRawPtr()), false);
contentConstraint.maxSize.SetWidth(1.0);
contentConstraint.maxSize.SetHeight(1.0);
EXPECT_EQ(textAdaptFontSizer->AdaptMinFontSize(
textStyle, content, stepUnit, contentConstraint, layoutWrapper.GetRawPtr()), true);
TextStyle textStyleFP;
textStyleFP.SetAdaptTextSize(Dimension(100, DimensionUnit::FP),
Dimension(10, DimensionUnit::FP), Dimension(10, DimensionUnit::FP));
textStyleFP.SetFontSize(Dimension(10, DimensionUnit::FP));
EXPECT_EQ(textAdaptFontSizer->AdaptMinFontSize(
textStyleFP, content, stepUnit, contentConstraint, layoutWrapper.GetRawPtr()), true);
}
/**
* @tc.name: IsNeedAdaptFontSize001
* @tc.desc: test text_adapt_font_sizer.cpp IsNeedAdaptFontSize function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, IsNeedAdaptFontSize001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto frameNode = FrameNode::CreateFrameNode("Test", 1, pattern);
ASSERT_NE(frameNode, nullptr);
pattern->AttachToFrameNode(frameNode);
auto layoutWrapper = frameNode->CreateLayoutWrapper(true, true);
ASSERT_NE(layoutWrapper, nullptr);
auto textAdaptFontSizer = AceType::MakeRefPtr<TextLayoutAlgorithm>();
ASSERT_NE(textAdaptFontSizer, nullptr);
double maxFontSize = 0.0;
double minFontSize = 0.0;
EXPECT_EQ(textAdaptFontSizer->IsNeedAdaptFontSize(maxFontSize, minFontSize), false);
maxFontSize = 1.0;
minFontSize = 1.0;
EXPECT_EQ(textAdaptFontSizer->IsNeedAdaptFontSize(maxFontSize, minFontSize), true);
maxFontSize = -2.0;
minFontSize = -1.0;
EXPECT_EQ(textAdaptFontSizer->IsNeedAdaptFontSize(maxFontSize, minFontSize), false);
maxFontSize = 0.0;
minFontSize = -1.0;
EXPECT_EQ(textAdaptFontSizer->IsNeedAdaptFontSize(maxFontSize, minFontSize), false);
TextStyle textStyle;
LayoutConstraintF contentConstraint;
textAdaptFontSizer->IsNeedAdaptFontSize(textStyle, contentConstraint);
textStyle.SetAdaptTextSize(Dimension(100, DimensionUnit::PERCENT),
Dimension(10, DimensionUnit::PERCENT), Dimension(10, DimensionUnit::PERCENT));
contentConstraint.maxSize.SetWidth(-1.0);
contentConstraint.maxSize.SetHeight(-1.0);
EXPECT_EQ(textAdaptFontSizer->IsNeedAdaptFontSize(textStyle, contentConstraint), false);
}
/**
* @tc.name: IsAdaptFontSizeExceedLineHeight001
* @tc.desc: test text_adapt_font_sizer.cpp IsAdaptFontSizeExceedLineHeight function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, IsAdaptFontSizeExceedLineHeight001, TestSize.Level1)
{
auto pattern = AceType::MakeRefPtr<TextPattern>();
ASSERT_NE(pattern, nullptr);
auto frameNode = FrameNode::CreateFrameNode("Test", 1, pattern);
ASSERT_NE(frameNode, nullptr);
pattern->AttachToFrameNode(frameNode);
auto layoutWrapper = frameNode->CreateLayoutWrapper(true, true);
ASSERT_NE(layoutWrapper, nullptr);
auto textAdaptFontSizer = AceType::MakeRefPtr<TextLayoutAlgorithm>();
ASSERT_NE(textAdaptFontSizer, nullptr);
auto paragraph = MockParagraph::GetOrCreateMockParagraph();
textAdaptFontSizer->lineHeight_ = 0.0;
EXPECT_EQ(textAdaptFontSizer->IsAdaptFontSizeExceedLineHeight(paragraph), false);
textAdaptFontSizer->lineHeight_ = 1.0;
EXPECT_EQ(textAdaptFontSizer->IsAdaptFontSizeExceedLineHeight(paragraph), false);
}
/**
* @tc.name: EncodeTlv001
* @tc.desc: test span_node.cpp EncodeTlv function
* @tc.type: FUNC
*/
HWTEST_F(TextTestFiveNg, EncodeTlv001, TestSize.Level1)
{
BorderRadiusProperty borderRadius;
borderRadius.SetRadius(2.0_vp);
MarginProperty margins;
margins.SetEdges(CalcLength(10.0));
PaddingProperty paddings;
paddings.SetEdges(CalcLength(5.0));
ImageSpanAttribute attr { .paddingProp = paddings,
.marginProp = margins,
.borderRadius = borderRadius,
.objectFit = ImageFit::COVER,
.verticalAlign = VerticalAlign::BOTTOM };
auto imageSpanItem = AceType::MakeRefPtr<NG::ImageSpanItem>();
ASSERT_NE(imageSpanItem, nullptr);
std::vector<uint8_t> buff;
ImageSpanOptions options;
imageSpanItem->SetImageSpanOptions(options);
EXPECT_EQ(imageSpanItem->EncodeTlv(buff), true);
buff.clear();
options.offset = 0;
options.image = "textImage";
options.bundleName = "textBundleName";
options.moduleName = "textModuleName";
options.imagePixelMap = nullptr;
options.imageAttribute = attr;
imageSpanItem->SetImageSpanOptions(options);
EXPECT_EQ(imageSpanItem->EncodeTlv(buff), true);
int32_t cursor = 1;
auto newImageSpanItem = imageSpanItem->DecodeTlv(buff, cursor);
EXPECT_NE(newImageSpanItem, nullptr);
cursor = 0;
newImageSpanItem = imageSpanItem->DecodeTlv(buff, cursor);
EXPECT_NE(newImageSpanItem, nullptr);
}
} // namespace OHOS::Ace::NG