提高richeditor TDD覆盖率

Signed-off-by: jiangzhijun8 <jiangzhijun7@huawei.com>
This commit is contained in:
jiangzhijun8 2024-08-06 15:14:25 +08:00
parent 3bb354536b
commit 90c1d4d100
2 changed files with 392 additions and 0 deletions

View File

@ -26,6 +26,7 @@ ace_unittest("rich_editor_test_ng") {
"rich_editor_keyboard_shortcut_test_ng.cpp",
"rich_editor_overlay_test_ng.cpp",
"rich_editor_pattern_test_ng.cpp",
"rich_editor_pattern_test_new_ng.cpp",
"rich_editor_preview_text_test_ng.cpp",
"rich_editor_styled_string_test_ng.cpp",
]

View File

@ -0,0 +1,391 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "test/unittest/core/pattern/rich_editor/rich_editor_common_test_ng.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS::Ace::NG {
namespace {
int32_t testOnReadyEvent = 0;
int32_t testAboutToIMEInput = 0;
int32_t testOnIMEInputComplete = 0;
int32_t testAboutToDelete = 0;
int32_t testOnDeleteComplete = 0;
} // namespace
class RichEditorPatternTestNewNg : public RichEditorCommonTestNg {
public:
void SetUp() override;
void TearDown() override;
static void TearDownTestSuite();
};
void RichEditorPatternTestNewNg::SetUp()
{
MockPipelineContext::SetUp();
MockContainer::SetUp();
MockContainer::Current()->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
auto* stack = ViewStackProcessor::GetInstance();
auto nodeId = stack->ClaimNodeId();
richEditorNode_ = FrameNode::GetOrCreateFrameNode(
V2::RICH_EDITOR_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<RichEditorPattern>(); });
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
richEditorPattern->InitScrollablePattern();
richEditorPattern->SetRichEditorController(AceType::MakeRefPtr<RichEditorController>());
richEditorPattern->GetRichEditorController()->SetPattern(AceType::WeakClaim(AceType::RawPtr(richEditorPattern)));
richEditorPattern->CreateNodePaintMethod();
richEditorNode_->GetGeometryNode()->SetContentSize({});
}
void RichEditorPatternTestNewNg::TearDown()
{
richEditorNode_ = nullptr;
testOnReadyEvent = 0;
testAboutToIMEInput = 0;
testOnIMEInputComplete = 0;
testAboutToDelete = 0;
testOnDeleteComplete = 0;
MockParagraph::TearDown();
}
void RichEditorPatternTestNewNg::TearDownTestSuite()
{
TestNG::TearDownTestSuite();
}
/**
* @tc.name: RemoveEmptySpanNodes001
* @tc.desc: test RichEditorPattern RemoveEmptySpanNodes
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, RemoveEmptySpanNodes001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
richEditorPattern->RemoveEmptySpanNodes();
auto host = richEditorPattern->GetHost();
ASSERT_NE(host, nullptr);
}
/**
* @tc.name: GetParagraphLength001
* @tc.desc: test RichEditorPattern GetParagraphLength
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, GetParagraphLength001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
std::list<RefPtr<UINode>> spans;
int32_t ret = richEditorPattern->GetParagraphLength(spans);
ASSERT_EQ(ret, 0);
}
/**
* @tc.name: HandleClickEvent002
* @tc.desc: test RichEditorPattern HandleClickEvent
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, HandleClickEvent002, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
GestureEvent info;
info.localLocation_ = Offset(0, 0);
ParagraphStyle paragraphStyle;
auto paragraph = Paragraph::Create(paragraphStyle, FontCollection::Current());
richEditorPattern->pManager_->AddParagraph({ .paragraph = paragraph, .paragraphStyle = paragraphStyle });
auto focusHub = richEditorNode_->GetOrCreateFocusHub();
ASSERT_NE(focusHub, nullptr);
richEditorPattern->GetFocusHub()->focusType_ = FocusType::DISABLE;
richEditorPattern->HandleClickEvent(info);
EXPECT_EQ(richEditorPattern->caretPosition_, 0);
}
/**
* @tc.name: HandleSingleClickEvent001
* @tc.desc: test RichEditorPattern HandleSingleClickEvent
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, HandleSingleClickEvent001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
GestureEvent info;
info.localLocation_ = Offset(0, 0);
ParagraphStyle paragraphStyle;
auto paragraph = Paragraph::Create(paragraphStyle, FontCollection::Current());
richEditorPattern->pManager_->AddParagraph({ .paragraph = paragraph, .paragraphStyle = paragraphStyle });
auto focusHub = richEditorNode_->GetOrCreateFocusHub();
ASSERT_NE(focusHub, nullptr);
richEditorPattern->dataDetectorAdapter_->hasClickedAISpan_ = true;
richEditorPattern->dataDetectorAdapter_->pressedByLeftMouse_ = true;
richEditorPattern->HandleSingleClickEvent(info);
EXPECT_EQ(richEditorPattern->caretPosition_, 0);
}
/**
* @tc.name: ClickAISpan001
* @tc.desc: test RichEditorPattern ClickAISpan
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, ClickAISpan001, TestSize.Level1)
{
AISpan aiSpan;
aiSpan.start = 0;
aiSpan.end = 10;
aiSpan.content = "1234567";
aiSpan.type = TextDataDetectType::PHONE_NUMBER;
PointF textOffset = PointF(100, 100);
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
bool res = richEditorPattern->ClickAISpan(textOffset, aiSpan);
EXPECT_EQ(res, false);
}
/**
* @tc.name: CalculateEmptyValueCaretRect001
* @tc.desc: test RichEditorPattern CalculateEmptyValueCaretRect
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, CalculateEmptyValueCaretRect001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
auto paragraph = MockParagraph::GetOrCreateMockParagraph();
ASSERT_NE(paragraph, nullptr);
EXPECT_CALL(*paragraph, PushStyle(_)).Times(AnyNumber());
EXPECT_CALL(*paragraph, AddText(_)).Times(AnyNumber());
EXPECT_CALL(*paragraph, PopStyle()).Times(AnyNumber());
EXPECT_CALL(*paragraph, Build()).Times(AnyNumber());
EXPECT_CALL(*paragraph, Layout(_)).Times(AnyNumber());
TestParagraphItem testParagraphItem = {
.start = 0, .end = 6, .indexOffsetMap = { { 0, Offset(0, 5) }, { 6, Offset(50, 0) } }
};
richEditorPattern->paragraphs_.AddParagraph({ .paragraph = paragraph, .start = 0, .end = 6 });
CaretMetricsF metricsDown;
CaretMetricsF metricsUp;
for (const auto& [index, offset] : testParagraphItem.indexOffsetMap) {
metricsDown.offset.SetX(offset.GetX());
metricsDown.offset.SetY(offset.GetY());
metricsUp.offset.SetX(offset.GetX());
metricsUp.offset.SetY(offset.GetY());
EXPECT_CALL(*paragraph, GetGlyphIndexByCoordinate(_, _)).WillRepeatedly(Return(6));
EXPECT_CALL(*paragraph, GetMaxWidth).WillRepeatedly(Return(150));
EXPECT_CALL(*paragraph, GetHeight).WillRepeatedly(Return(50));
EXPECT_CALL(*paragraph, ComputeOffsetForCaretDownstream(index, _, _))
.WillRepeatedly(DoAll(SetArgReferee<1>(metricsDown), Return(true)));
EXPECT_CALL(*paragraph, ComputeOffsetForCaretUpstream(index, _, _))
.WillRepeatedly(DoAll(SetArgReferee<1>(metricsUp), Return(true)));
}
TextStyle style;
style.SetLineHeight(LINE_HEIGHT_VALUE);
style.SetLetterSpacing(LETTER_SPACING);
style.SetFontFeatures(TEXT_FONTFEATURE);
auto layoutProperty = richEditorPattern->GetLayoutProperty<TextLayoutProperty>();
layoutProperty->UpdateLayoutDirection(TextDirection::RTL);
richEditorPattern->presetParagraph_ = paragraph;
richEditorPattern->CalculateEmptyValueCaretRect();
richEditorPattern->typingTextStyle_ = style;
richEditorPattern->PreferredParagraph();
EXPECT_NE(richEditorPattern->presetParagraph_, nullptr);
}
/**
* @tc.name: HandleLongPress001
* @tc.desc: test RichEditorPattern HandleLongPress
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, HandleLongPress001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
GestureEvent info;
info.localLocation_ = Offset(0, 0);
auto focusHub = richEditorNode_->GetOrCreateFocusHub();
ASSERT_NE(focusHub, nullptr);
richEditorPattern->HandleLongPress(info);
focusHub->focusType_ = FocusType::DISABLE;
richEditorPattern->HandleLongPress(info);
ASSERT_EQ(richEditorPattern->caretUpdateType_, CaretUpdateType::NONE);
}
/**
* @tc.name: HandleMenuCallbackOnSelectAll001
* @tc.desc: test RichEditorPattern HandleMenuCallbackOnSelectAll
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, HandleMenuCallbackOnSelectAll001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
/**
* @tc.steps: step1. get richeditor pattern and add add text span
*/
AddSpan("hello1");
EXPECT_EQ(richEditorPattern->GetTextContentLength(), 6);
richEditorPattern->CursorMoveUp();
/**
* @tc.steps: step2. request focus
*/
auto focusHub = richEditorNode_->GetOrCreateFocusHub();
focusHub->RequestFocusImmediately();
/**
* @tc.step: step3. create a scene where the text menu has popped up
*/
richEditorPattern->OnModifyDone();
richEditorPattern->textSelector_.Update(1, 2);
richEditorPattern->CalculateHandleOffsetAndShowOverlay();
richEditorPattern->ShowSelectOverlay(
richEditorPattern->textSelector_.firstHandle, richEditorPattern->textSelector_.secondHandle, false);
EXPECT_TRUE(richEditorPattern->SelectOverlayIsOn());
/**
* @tc.step: step4. test OnMenuItemAction
*/
richEditorPattern->selectOverlay_->OnMenuItemAction(OptionMenuActionId::COPY, OptionMenuType::TOUCH_MENU);
EXPECT_EQ(richEditorPattern->textSelector_.GetTextStart(), 1);
EXPECT_EQ(richEditorPattern->textSelector_.GetTextEnd(), 2);
richEditorPattern->selectOverlay_->OnMenuItemAction(OptionMenuActionId::PASTE, OptionMenuType::NO_MENU);
EXPECT_EQ(richEditorPattern->GetTextContentLength(), 6);
richEditorPattern->selectOverlay_->isUsingMouse_ = true;
richEditorPattern->HandleMenuCallbackOnSelectAll();
ASSERT_EQ(richEditorPattern->IsUsingMouse(), true);
}
/**
* @tc.name: InsertStyledStringByPaste001
* @tc.desc: test RichEditorPattern InsertStyledStringByPaste
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, InsertStyledStringByPaste001, TestSize.Level1)
{
/**
* @tc.steps: step1. get richEditor pattern and controller
*/
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
auto richEditorController = richEditorPattern->GetRichEditorController();
ASSERT_NE(richEditorController, nullptr);
/**
* @tc.steps: step2. add span and select text
*/
AddSpan("test");
EXPECT_EQ(richEditorPattern->GetTextContentLength(), 4);
richEditorPattern->textSelector_.Update(3, 4);
richEditorPattern->styledString_ = AceType::MakeRefPtr<MutableSpanString>("abc");
std::string data = "abc";
RefPtr<SpanString> spanString = AceType::MakeRefPtr<SpanString>(data);
richEditorPattern->InsertStyledStringByPaste(spanString);
ASSERT_EQ(richEditorPattern->textSelector_.IsValid(), true);
}
/**
* @tc.name: AddSpansByPaste001
* @tc.desc: test RichEditorPattern AddSpansByPaste
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, AddSpansByPaste001, TestSize.Level1)
{
/**
* @tc.steps: step1. get richEditor pattern and controller
*/
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
auto richEditorController = richEditorPattern->GetRichEditorController();
ASSERT_NE(richEditorController, nullptr);
/**
* @tc.steps: step2. add span and select text
*/
AddSpan("test");
EXPECT_EQ(richEditorPattern->GetTextContentLength(), 4);
richEditorPattern->textSelector_.Update(3, 4);
std::list<RefPtr<NG::SpanItem>> spans;
OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem1 = AceType::MakeRefPtr<ImageSpanItem>();
spans.push_back(spanItem1);
richEditorPattern->AddSpansByPaste(spans);
ASSERT_EQ(richEditorPattern->textSelector_.IsValid(), true);
}
/**
* @tc.name: UnableStandardInput001
* @tc.desc: test RichEditorPattern UnableStandardInput
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, UnableStandardInput001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
richEditorPattern->UnableStandardInput(true);
bool res = richEditorPattern->UnableStandardInput(false);
ASSERT_EQ(res, false);
}
/**
* @tc.name: GetPreviewTextInfo001
* @tc.desc: test RichEditorPattern GetPreviewTextInfo
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, GetPreviewTextInfo001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
PreviewTextInfo info = richEditorPattern->GetPreviewTextInfo();
ASSERT_EQ(richEditorPattern->previewTextRecord_.previewContent.empty(), true);
richEditorPattern->previewTextRecord_.previewContent = "abc";
info = richEditorPattern->GetPreviewTextInfo();
ASSERT_EQ(richEditorPattern->previewTextRecord_.previewContent.empty(), false);
}
/**
* @tc.name: HandleOnDelete001
* @tc.desc: test RichEditorPattern HandleOnDelete
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestNewNg, HandleOnDelete001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
richEditorPattern->HandleOnDelete(true);
richEditorPattern->HandleOnDelete(false);
}
} // namespace