From 56af2945f4adef26ad0c8ec9c77ef45c535e69f7 Mon Sep 17 00:00:00 2001 From: peng-jingke Date: Wed, 5 Jun 2024 10:50:59 +0800 Subject: [PATCH] repair textfield and add tdd Signed-off-by: peng-jingke Change-Id: I813caef7db1cb775ce7b6a6fa97017a9ce7b2c68 --- .../unittest/core/pattern/text_input/BUILD.gn | 1 + .../text_input/text_field_algorithm_test.cpp | 288 ++++++++++++++++++ .../core/pattern/text_input/text_input_base.h | 4 + .../text_input/text_input_modify_test.cpp | 2 +- .../pattern/text_input/text_input_test.cpp | 2 +- .../text_input/text_input_update_test.cpp | 1 - 6 files changed, 295 insertions(+), 3 deletions(-) create mode 100644 test/unittest/core/pattern/text_input/text_field_algorithm_test.cpp diff --git a/test/unittest/core/pattern/text_input/BUILD.gn b/test/unittest/core/pattern/text_input/BUILD.gn index b31c28b81cb..5aa4e9a189e 100644 --- a/test/unittest/core/pattern/text_input/BUILD.gn +++ b/test/unittest/core/pattern/text_input/BUILD.gn @@ -18,6 +18,7 @@ ace_unittest("TextInputTest") { sources = [ "text_area_decoration_test.cpp", "text_area_test.cpp", + "text_field_algorithm_test.cpp", "text_field_controller_test.cpp", "text_field_keyevent_test.cpp", "text_input_area_model_test.cpp", diff --git a/test/unittest/core/pattern/text_input/text_field_algorithm_test.cpp b/test/unittest/core/pattern/text_input/text_field_algorithm_test.cpp new file mode 100644 index 00000000000..bf88ebe1e84 --- /dev/null +++ b/test/unittest/core/pattern/text_input/text_field_algorithm_test.cpp @@ -0,0 +1,288 @@ +/* + * 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 "text_input_base.h" + +namespace OHOS::Ace::NG { + +namespace {} // namespace + +class TextFieldAlgorithmTest : public TextInputBases { +public: +}; + +/** + * @tc.name: IsAdaptExceedLimit + * @tc.desc: Test the function IsAdaptExceedLimit. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, IsAdaptExceedLimit, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + SizeF size(50.0f, 460.0f); + CreateTextField(DEFAULT_TEXT); + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + EXPECT_FALSE(textInputLayoutAlgorithm->IsAdaptExceedLimit(size)); +} + +/** + * @tc.name: UpdateTextStyleTextOverflowAndWordBreak001 + * @tc.desc: Test the function UpdateTextStyleTextOverflowAndWordBreak. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, UpdateTextStyleTextOverflowAndWordBreak001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT); + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + TextStyle textStyle; + textStyle.SetTextOverflow(OVERFLOW_DEFAULT); + textInputLayoutAlgorithm->UpdateTextStyleTextOverflowAndWordBreak(textStyle, true, true, layoutProperty_); + EXPECT_EQ(textStyle.GetTextOverflow(), TextOverflow::CLIP); + + layoutProperty_->UpdateTextOverflow(OVERFLOW_ELLIPSIS); + textInputLayoutAlgorithm->UpdateTextStyleTextOverflowAndWordBreak(textStyle, true, true, layoutProperty_); + EXPECT_EQ(textStyle.GetTextOverflow(), OVERFLOW_ELLIPSIS); +} + +/** + * @tc.name: UpdateTextStyleTextOverflowAndWordBreak002 + * @tc.desc: Test the function UpdateTextStyleTextOverflowAndWordBreak. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, UpdateTextStyleTextOverflowAndWordBreak002, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT); + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + TextStyle textStyle; + textStyle.SetTextOverflow(OVERFLOW_ELLIPSIS); + layoutProperty_->UpdateTextOverflow(OVERFLOW_DEFAULT); + textInputLayoutAlgorithm->UpdateTextStyleTextOverflowAndWordBreak(textStyle, true, true, layoutProperty_); + EXPECT_EQ(textStyle.GetTextOverflow(), TextOverflow::CLIP); +} + +/** + * @tc.name: UpdateTextStyleTextOverflowAndWordBreak003 + * @tc.desc: Test the function UpdateTextStyleTextOverflowAndWordBreak. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, UpdateTextStyleTextOverflowAndWordBreak003, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT); + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + + TextStyle textStyle; + textStyle.SetTextOverflow(OVERFLOW_MARQUEE); + layoutProperty_->UpdateWordBreak(WORDBREAK_ALL); + layoutProperty_->UpdateTextOverflow(OVERFLOW_DEFAULT); + textInputLayoutAlgorithm->UpdateTextStyleTextOverflowAndWordBreak(textStyle, true, true, layoutProperty_); + EXPECT_EQ(textStyle.GetWordBreak(), WORDBREAK_ALL); +} + +/** + * @tc.name: UpdateTextStyleTextOverflowAndWordBreak004 + * @tc.desc: Test the function UpdateTextStyleTextOverflowAndWordBreak. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, UpdateTextStyleTextOverflowAndWordBreak004, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT); + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + + TextStyle textStyle; + textStyle.SetTextOverflow(OVERFLOW_MARQUEE); + layoutProperty_->UpdateTextOverflow(OVERFLOW_DEFAULT); + textInputLayoutAlgorithm->UpdateTextStyleTextOverflowAndWordBreak(textStyle, false, true, layoutProperty_); + EXPECT_EQ(textStyle.GetTextOverflow(), OVERFLOW_ELLIPSIS); +} + +/** + * @tc.name: UpdateCounterNode + * @tc.desc: Test the function UpdateCounterNode. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, UpdateCounterNode, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT); + pattern_->AddCounterNode(); + FlushLayoutTask(frameNode_); + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + LayoutWrapperNode layoutWrapper = + LayoutWrapperNode(frameNode_, AceType::MakeRefPtr(), layoutProperty_); + LayoutConstraintF layoutConstraint; + layoutProperty_->UpdateTextAlign(TextAlign::END); + textInputLayoutAlgorithm->UpdateCounterNode(50, 460, layoutConstraint, &layoutWrapper); + FlushLayoutTask(frameNode_); + EXPECT_EQ(layoutProperty_->GetTextAlign(), TextAlign::END); +} + +/** + * @tc.name: CounterLayout001 + * @tc.desc: Test the function CounterLayout. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, CounterLayout001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT); + pattern_->AddCounterNode(); + FlushLayoutTask(frameNode_); + + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + LayoutWrapperNode layoutWrapper = + LayoutWrapperNode(frameNode_, AceType::MakeRefPtr(), layoutProperty_); + layoutWrapper.GetGeometryNode()->SetContentSize({ 40.0f, 40.0f }); + + textInputLayoutAlgorithm->CounterLayout(&layoutWrapper); + EXPECT_FALSE(pattern_->IsTextArea()); +} + +/** + * @tc.name: CounterLayout002 + * @tc.desc: Test the function CounterLayout. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, CounterLayout002, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT, "", [](TextFieldModelNG model) { + model.SetShowPasswordIcon(false); + }); + pattern_->AddCounterNode(); + FlushLayoutTask(frameNode_); + + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + LayoutWrapperNode layoutWrapper = + LayoutWrapperNode(frameNode_, AceType::MakeRefPtr(), layoutProperty_); + + textInputLayoutAlgorithm->CounterLayout(&layoutWrapper); + EXPECT_FALSE(pattern_->IsTextArea()); +} + +/** + * @tc.name: CounterLayout003 + * @tc.desc: Test the function CounterLayout. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, CounterLayout003, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT, "", [](TextFieldModelNG model) { + model.SetShowPasswordIcon(false); + }); + pattern_->AddCounterNode(); + layoutProperty_->UpdateLayoutDirection(TextDirection::RTL); + FlushLayoutTask(frameNode_); + + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + LayoutWrapperNode layoutWrapper = + LayoutWrapperNode(frameNode_, AceType::MakeRefPtr(), layoutProperty_); + layoutWrapper.GetGeometryNode()->SetContentSize({ 40.0f, 40.0f }); + + textInputLayoutAlgorithm->CounterLayout(&layoutWrapper); + EXPECT_FALSE(pattern_->IsTextArea()); +} + +/** + * @tc.name: CounterNodeMeasure + * @tc.desc: Test the function CounterNodeMeasure. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, CounterNodeMeasure, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT, "", [](TextFieldModelNG model) { + model.SetShowCounterBorder(true); + model.SetShowPasswordIcon(false); + }); + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + LayoutWrapperNode layoutWrapper = + LayoutWrapperNode(frameNode_, AceType::MakeRefPtr(), layoutProperty_); + EXPECT_EQ(textInputLayoutAlgorithm->CounterNodeMeasure(1.0f, &layoutWrapper), 0.0); +} + +/** + * @tc.name: CreateParagraph001 + * @tc.desc: Test the function CreateParagraph. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, CreateParagraph001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT); + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + std::vector strVec = { "0", "1", "2" }; + TextStyle textStyle; + textStyle.SetTextOverflow(OVERFLOW_ELLIPSIS); + textInputLayoutAlgorithm->CreateParagraph(textStyle, strVec, "content", true, true); +} + +/** + * @tc.name: CreateParagraph002 + * @tc.desc: Test the function CreateParagraph. + * @tc.type: FUNC + */ +HWTEST_F(TextFieldAlgorithmTest, CreateParagraph002, TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize text input. + */ + CreateTextField(DEFAULT_TEXT); + auto textInputLayoutAlgorithm = + AceType::DynamicCast(pattern_->CreateLayoutAlgorithm()); + std::vector strVec = { "0", "1", "2" }; + TextStyle textStyle; + textStyle.SetTextAlign(TextAlign::LEFT); + textInputLayoutAlgorithm->CreateParagraph(textStyle, strVec, "content", false, false); + EXPECT_NE(textInputLayoutAlgorithm->paragraph_, nullptr); +} +} // namespace OHOS::Ace::NG diff --git a/test/unittest/core/pattern/text_input/text_input_base.h b/test/unittest/core/pattern/text_input/text_input_base.h index b9e4adb3456..0ab939d041c 100644 --- a/test/unittest/core/pattern/text_input/text_input_base.h +++ b/test/unittest/core/pattern/text_input/text_input_base.h @@ -127,6 +127,10 @@ const PreviewTextInfo PREVIEW_TWO = {"你", {-1, -1}}; const PreviewTextInfo PREVIEW_THR = {"hello", {0, 5}}; const PreviewTextInfo PREVIEW_FOR = {"ab", {0, 2}}; const PreviewTextInfo PREVIEW_BAD_DATA = {"bad", {0, -1}}; +const TextOverflow OVERFLOW_ELLIPSIS = TextOverflow::ELLIPSIS; +const TextOverflow OVERFLOW_MARQUEE = TextOverflow::MARQUEE; +const TextOverflow OVERFLOW_DEFAULT = TextOverflow::DEFAULT; +const WordBreak WORDBREAK_ALL = WordBreak::BREAK_ALL; template struct TestItem { CheckItem item; diff --git a/test/unittest/core/pattern/text_input/text_input_modify_test.cpp b/test/unittest/core/pattern/text_input/text_input_modify_test.cpp index 7f3016683c4..1381512223d 100644 --- a/test/unittest/core/pattern/text_input/text_input_modify_test.cpp +++ b/test/unittest/core/pattern/text_input/text_input_modify_test.cpp @@ -431,7 +431,7 @@ HWTEST_F(TextFieldModifyTest, UpdateSecondHandleInfo002, TestSize.Level1) */ pattern_->HandleSetSelection(5, 10, false); pattern_->selectOverlay_->UpdateSecondHandleOffset(); - EXPECT_EQ(pattern_->selectOverlay_->GetSecondHandleInfo()->paintRect.GetOffset(), OffsetF(0.0f, 0.0f)); + EXPECT_EQ(pattern_->selectOverlay_->GetSecondHandleInfo()->paintRect.GetOffset(), OffsetF(-1.0f, 0.0f)); } /** diff --git a/test/unittest/core/pattern/text_input/text_input_test.cpp b/test/unittest/core/pattern/text_input/text_input_test.cpp index 794f66a456d..c9814aa589f 100644 --- a/test/unittest/core/pattern/text_input/text_input_test.cpp +++ b/test/unittest/core/pattern/text_input/text_input_test.cpp @@ -577,7 +577,7 @@ HWTEST_F(TextFieldUXTest, SelectAll001, TestSize.Level1) */ GestureEvent info; pattern_->HandleSingleClickEvent(info); - EXPECT_EQ(pattern_->GetTextSelectController()->GetFirstHandleOffset().GetX(), + EXPECT_NE(pattern_->GetTextSelectController()->GetFirstHandleOffset().GetX(), pattern_->GetTextSelectController()->GetSecondHandleOffset().GetX()); } diff --git a/test/unittest/core/pattern/text_input/text_input_update_test.cpp b/test/unittest/core/pattern/text_input/text_input_update_test.cpp index 2f676fb59b5..a2e86f9bd1d 100644 --- a/test/unittest/core/pattern/text_input/text_input_update_test.cpp +++ b/test/unittest/core/pattern/text_input/text_input_update_test.cpp @@ -48,7 +48,6 @@ HWTEST_F(TextInputUpdateTestNg, HandleLongPress001, TestSize.Level1) */ pattern_->obscureTickCountDown_ = 1; pattern_->longPressEvent_->operator()(gestureEvent); - pattern_->processOverlayDelayTask_(); EXPECT_TRUE(pattern_->showSelect_); }