This commit is contained in:
jiangzhijun8 2024-08-08 10:35:37 +08:00
commit eccca7a47d
2 changed files with 266 additions and 0 deletions

View File

@ -27,6 +27,7 @@ ace_unittest("rich_editor_test_ng") {
"rich_editor_overlay_test_ng.cpp",
"rich_editor_pattern_test_ng.cpp",
"rich_editor_pattern_testone_ng.cpp",
"rich_editor_pattern_testtwo_ng.cpp",
"rich_editor_preview_text_test_ng.cpp",
"rich_editor_styled_string_test_ng.cpp",
]

View File

@ -0,0 +1,265 @@
/*
* 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 RichEditorPatternTestTwoNg : public RichEditorCommonTestNg {
public:
void SetUp() override;
void TearDown() override;
static void TearDownTestSuite();
};
void RichEditorPatternTestTwoNg::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 RichEditorPatternTestTwoNg::TearDown()
{
richEditorNode_ = nullptr;
testOnReadyEvent = 0;
testAboutToIMEInput = 0;
testOnIMEInputComplete = 0;
testAboutToDelete = 0;
testOnDeleteComplete = 0;
MockParagraph::TearDown();
}
void RichEditorPatternTestTwoNg::TearDownTestSuite()
{
TestNG::TearDownTestSuite();
}
/**
* @tc.name: CalcDragSpeed001
* @tc.desc: test CalcDragSpeed
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, CalcDragSpeed001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
float speed = 0.0f;
float hotAreaStart = 1.1f;
float hotAreaEnd = 101.1f;
float point = 50.1f;
float result = 17.472723f;
speed = richEditorPattern->CalcDragSpeed(hotAreaStart, hotAreaEnd, point);
EXPECT_EQ(result, speed);
}
/**
* @tc.name: CalcMoveDownPos001
* @tc.desc: test CalcMoveDownPos
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, CalcMoveDownPos001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
richEditorPattern->overlayMod_ = nullptr;
int32_t result = 1;
float leadingMarginOffset = 10.0f;
result = richEditorPattern->CalcMoveDownPos(leadingMarginOffset);
EXPECT_EQ(result, 0);
}
/**
* @tc.name: CalcMoveDownPos002
* @tc.desc: test CalcMoveDownPos
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, CalcMoveDownPos002, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
ASSERT_NE(richEditorPattern->overlayMod_, nullptr);
int32_t result = 1;
float leadingMarginOffset = 10.0f;
result = richEditorPattern->CalcMoveDownPos(leadingMarginOffset);
EXPECT_EQ(result, 0);
}
/**
* @tc.name: CalcLineBeginPosition001
* @tc.desc: test CalcLineBeginPosition
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, CalcLineBeginPosition001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
int32_t result = 1;
result = richEditorPattern->CalcLineBeginPosition();
EXPECT_EQ(result, 0);
}
/**
* @tc.name: OnSelectionMenuOptionsUpdate001
* @tc.desc: test OnSelectionMenuOptionsUpdate
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, OnSelectionMenuOptionsUpdate001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
OnCreateMenuCallback onCreateMenuCallback;
OnMenuItemClickCallback onMenuItemClick;
richEditorPattern->OnSelectionMenuOptionsUpdate(std::move(onCreateMenuCallback), std::move(onMenuItemClick));
}
/**
* @tc.name: RequestKeyboardToEdit001
* @tc.desc: test RequestKeyboardToEdit
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, RequestKeyboardToEdit001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
richEditorPattern->previewLongPress_ = true;
richEditorPattern->RequestKeyboardToEdit();
EXPECT_FALSE(richEditorPattern->previewLongPress_);
}
/**
* @tc.name: RequestKeyboardToEdit002
* @tc.desc: test RequestKeyboardToEdit
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, RequestKeyboardToEdit002, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
richEditorPattern->RequestKeyboardToEdit();
}
/**
* @tc.name: IsResponseRegionExpandingNeededForStylus001
* @tc.desc: test IsResponseRegionExpandingNeededForStylus
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, IsResponseRegionExpandingNeededForStylus001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
TouchEvent touchEvent;
touchEvent.sourceTool = SourceTool::UNKNOWN;
bool ret = true;
ret = richEditorPattern->IsResponseRegionExpandingNeededForStylus(touchEvent);
EXPECT_FALSE(ret);
}
/**
* @tc.name: IsResponseRegionExpandingNeededForStylus002
* @tc.desc: test IsResponseRegionExpandingNeededForStylus
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, IsResponseRegionExpandingNeededForStylus002, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
TouchEvent touchEvent;
touchEvent.type = TouchType::UNKNOWN;
bool ret = true;
ret = richEditorPattern->IsResponseRegionExpandingNeededForStylus(touchEvent);
EXPECT_FALSE(ret);
}
/**
* @tc.name: IsResponseRegionExpandingNeededForStylus003
* @tc.desc: test IsResponseRegionExpandingNeededForStylus
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, IsResponseRegionExpandingNeededForStylus003, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
TouchEvent touchEvent;
touchEvent.sourceTool = SourceTool::PEN;
touchEvent.type = TouchType::DOWN;
bool ret = false;
ret = richEditorPattern->IsResponseRegionExpandingNeededForStylus(touchEvent);
EXPECT_TRUE(ret);
}
/**
* @tc.name: ExpandDefaultResponseRegion001
* @tc.desc: test ExpandDefaultResponseRegion
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, ExpandDefaultResponseRegion001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
RectF rect(0, 0, 5, 5);
RectF retRect;
retRect = richEditorPattern->ExpandDefaultResponseRegion(rect);
EXPECT_EQ(rect.Width(), retRect.Width());
}
/**
* @tc.name: AdjustWordSelection001
* @tc.desc: test AdjustWordSelection
* @tc.type: FUNC
*/
HWTEST_F(RichEditorPatternTestTwoNg, AdjustWordSelection001, TestSize.Level1)
{
ASSERT_NE(richEditorNode_, nullptr);
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
int32_t start = 100;
int32_t end = 200;
bool ret = true;
ret = richEditorPattern->AdjustWordSelection(start, end);
EXPECT_FALSE(ret);
}
} // namespace OHOS::Ace::NG