From 17918be0dccc228b85917471ca33da2df4567c8d Mon Sep 17 00:00:00 2001 From: huangxiaoxiao Date: Mon, 28 Jul 2025 16:17:14 +0800 Subject: [PATCH] update picker for layoutPolicy Signed-off-by: huangxiaoxiao --- .../calendar_picker_layout_algorithm.cpp | 58 ++-- .../calendar_picker_layout_algorithm.h | 1 - .../pattern/picker/datepicker_pattern.cpp | 15 + .../pattern/picker/datepicker_pattern.h | 2 + .../text_picker/textpicker_pattern.cpp | 14 + .../pattern/text_picker/textpicker_pattern.h | 2 + .../time_picker/timepicker_row_pattern.cpp | 14 + .../time_picker/timepicker_row_pattern.h | 2 + .../calendar_picker_layout_algorithm_test.cpp | 281 +----------------- ...picker_column_layout_algorithm_test_ng.cpp | 252 ++++++++++++++++ .../text_picker_layout_algorithm_test_ng.cpp | 265 +++++++++++++++++ ...picker_column_layout_algorithm_test_ng.cpp | 252 ++++++++++++++++ 12 files changed, 848 insertions(+), 310 deletions(-) diff --git a/frameworks/core/components_ng/pattern/calendar_picker/calendar_picker_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/calendar_picker/calendar_picker_layout_algorithm.cpp index 378e090c640..ba704151f2f 100644 --- a/frameworks/core/components_ng/pattern/calendar_picker/calendar_picker_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/calendar_picker/calendar_picker_layout_algorithm.cpp @@ -72,35 +72,37 @@ void UpdateContentSizeWithLayoutPolicy(const RefPtr& theme, Layou currentBorderWidth.SetBorderWidth(theme->GetEntryBorderWidth()); } + float contentWidth = contentSize.Width(); + auto borderAndButtonWidth = theme->GetEntryButtonWidth().ConvertToPx() + + currentBorderWidth.topDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx() + + currentBorderWidth.bottomDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx(); if (layoutPolicy->IsWidthMatch()) { - auto contentWidth = layoutConstraint->parentIdealSize.Width().value_or(0) - - theme->GetEntryButtonWidth().ConvertToPx() - - currentBorderWidth.topDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx() - - currentBorderWidth.bottomDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx(); + contentWidth = layoutConstraint->parentIdealSize.Width().value_or(0) - borderAndButtonWidth; contentSize.SetWidth(contentWidth); - } else if (layoutPolicy->IsWidthWrap() || layoutPolicy->IsWidthFix()) { + } else if (layoutPolicy->IsWidthWrap()) { + float maxWidth = layoutConstraint->maxSize.Width() - borderAndButtonWidth; + contentSize.SetWidth(std::min(contentWidth, maxWidth)); + } else if (layoutPolicy->IsWidthFix()) { const auto& calcConstraint = layoutProperty->GetCalcLayoutConstraint(); if (calcConstraint && calcConstraint->maxSize->Width().has_value()) { - float contentWidth = contentSize.Width(); - float maxWidth = layoutConstraint->maxSize.Width() - theme->GetEntryButtonWidth().ConvertToPx() - - currentBorderWidth.topDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx() - - currentBorderWidth.bottomDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx(); + float maxWidth = layoutConstraint->maxSize.Width() - borderAndButtonWidth; contentSize.SetWidth(std::min(contentWidth, maxWidth)); } } + float contentHeight = contentSize.Height(); + auto borderHeight = currentBorderWidth.leftDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx() + + currentBorderWidth.rightDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx(); if (layoutPolicy->IsHeightMatch()) { - auto contentHeight = layoutConstraint->parentIdealSize.Height().value_or(0) - - currentBorderWidth.leftDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx() - - currentBorderWidth.rightDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx(); + contentHeight = layoutConstraint->parentIdealSize.Height().value_or(0) - borderHeight; contentSize.SetHeight(contentHeight); - } else if (layoutPolicy->IsHeightWrap() || layoutPolicy->IsHeightFix()) { + } else if (layoutPolicy->IsHeightWrap()) { + float maxHeight = layoutConstraint->maxSize.Height() - borderHeight; + contentSize.SetHeight(std::min(contentHeight, maxHeight)); + } else if (layoutPolicy->IsHeightFix()) { const auto& calcConstraint = layoutProperty->GetCalcLayoutConstraint(); if (calcConstraint && calcConstraint->maxSize->Height().has_value()) { - float contentHeight = contentSize.Height(); - float maxHeight = layoutConstraint->maxSize.Height() - - currentBorderWidth.leftDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx() - - currentBorderWidth.rightDimen.value_or(theme->GetEntryBorderWidth()).ConvertToPx(); + float maxHeight = layoutConstraint->maxSize.Height() - borderHeight; contentSize.SetHeight(std::min(contentHeight, maxHeight)); } } @@ -142,7 +144,6 @@ void CalendarPickerLayoutAlgorithm::CalendarPickerContentMeasure(LayoutWrapper* CHECK_NULL_VOID(linearLayoutProperty); SizeF policySize = {constraint->selfIdealSize.Width().value_or(0), constraint->selfIdealSize.Height().value_or(0)}; - UpdateFrameSizeWithLayoutPolicy(layoutWrapper, policySize); if (LessNotEqual(contentSize.Width(), policySize.Width() - theme->GetEntryButtonWidth().ConvertToPx())) { contentSize.SetWidth(policySize.Width() - theme->GetEntryButtonWidth().ConvertToPx()); linearLayoutProperty->UpdateMainAxisAlign(FlexAlign::CENTER); @@ -217,25 +218,4 @@ void CalendarPickerLayoutAlgorithm::SelfMeasure(LayoutWrapper* layoutWrapper) } } -void CalendarPickerLayoutAlgorithm::UpdateFrameSizeWithLayoutPolicy(LayoutWrapper* layoutWrapper, SizeF& frameSize) -{ - CHECK_NULL_VOID(layoutWrapper); - auto layoutProperty = AceType::DynamicCast(layoutWrapper->GetLayoutProperty()); - CHECK_NULL_VOID(layoutProperty); - auto layoutPolicy = layoutProperty->GetLayoutPolicyProperty(); - CHECK_NULL_VOID(layoutPolicy.has_value()); - auto layoutConstraint = layoutProperty->GetLayoutConstraint(); - CHECK_NULL_VOID(layoutConstraint.has_value()); - - if (layoutPolicy->IsWidthMatch()) { - auto width = layoutConstraint->parentIdealSize.Width().value_or(0); - frameSize.SetWidth(width); - } - - if (layoutPolicy->IsHeightMatch()) { - auto height = layoutConstraint->parentIdealSize.Height().value_or(0); - frameSize.SetHeight(height); - } -} - } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/calendar_picker/calendar_picker_layout_algorithm.h b/frameworks/core/components_ng/pattern/calendar_picker/calendar_picker_layout_algorithm.h index 50489d28f74..a35cb172f5f 100644 --- a/frameworks/core/components_ng/pattern/calendar_picker/calendar_picker_layout_algorithm.h +++ b/frameworks/core/components_ng/pattern/calendar_picker/calendar_picker_layout_algorithm.h @@ -33,7 +33,6 @@ public: private: void CalendarPickerContentMeasure(LayoutWrapper* layoutWrapper); void SelfMeasure(LayoutWrapper* layoutWrapper); - void UpdateFrameSizeWithLayoutPolicy(LayoutWrapper* layoutWrapper, SizeF& frameSize); SizeF contentMeasure_; SizeF flexMeasure_; diff --git a/frameworks/core/components_ng/pattern/picker/datepicker_pattern.cpp b/frameworks/core/components_ng/pattern/picker/datepicker_pattern.cpp index a92ae79da68..07bc6bea715 100644 --- a/frameworks/core/components_ng/pattern/picker/datepicker_pattern.cpp +++ b/frameworks/core/components_ng/pattern/picker/datepicker_pattern.cpp @@ -63,6 +63,7 @@ constexpr int32_t RATIO_ZERO = 0; constexpr int32_t RATIO_ONE = 1; constexpr int32_t SECOND_PAGE = 1; constexpr float PICKER_MAXFONTSCALE = 1.0f; +constexpr float DEFAULT_SIZE_ZERO = 0.0f; } // namespace bool DatePickerPattern::inited_ = false; const std::string DatePickerPattern::empty_; @@ -3221,4 +3222,18 @@ void DatePickerPattern::UpdateSelectedTextStyle(const PickerTextStyle& textStyle [&](const std::vector& fontFamily) { pickerProperty->UpdateSelectedFontFamily(fontFamily); } ); } + +void DatePickerPattern::BeforeCreateLayoutWrapper() +{ + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto layoutProperty = host->GetLayoutProperty(); + CHECK_NULL_VOID(layoutProperty); + auto layoutPolicy = layoutProperty->GetLayoutPolicyProperty(); + if (layoutPolicy.has_value() && (layoutPolicy->IsWrap() || layoutPolicy->IsFix())) { + layoutProperty->UpdateUserDefinedIdealSize( + CalcSize(CalcLength(DEFAULT_SIZE_ZERO), CalcLength(DEFAULT_SIZE_ZERO))); + } +} + } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/picker/datepicker_pattern.h b/frameworks/core/components_ng/pattern/picker/datepicker_pattern.h index 5a0e7fed43a..630a7cfb980 100644 --- a/frameworks/core/components_ng/pattern/picker/datepicker_pattern.h +++ b/frameworks/core/components_ng/pattern/picker/datepicker_pattern.h @@ -51,6 +51,8 @@ public: ~DatePickerPattern() override = default; + void BeforeCreateLayoutWrapper() override; + void OnColorModeChange(uint32_t colorMode) override { LinearLayoutPattern::OnColorModeChange(colorMode); diff --git a/frameworks/core/components_ng/pattern/text_picker/textpicker_pattern.cpp b/frameworks/core/components_ng/pattern/text_picker/textpicker_pattern.cpp index 7dfa2778e65..d78c564ef18 100644 --- a/frameworks/core/components_ng/pattern/text_picker/textpicker_pattern.cpp +++ b/frameworks/core/components_ng/pattern/text_picker/textpicker_pattern.cpp @@ -53,6 +53,7 @@ constexpr float MAX_PERCENT = 100.0f; const int32_t UNOPTION_COUNT = 2; constexpr float PICKER_MAXFONTSCALE = 1.0f; constexpr uint32_t PRECISION_TWO = 2; +constexpr float DEFAULT_SIZE_ZERO = 0.0f; } // namespace void TextPickerPattern::OnAttachToFrameNode() @@ -2183,4 +2184,17 @@ void TextPickerPattern::GetAndUpdateRealSelectedArr(const std::vectorUpdateSelectedIndex(selectedArr); } +void TextPickerPattern::BeforeCreateLayoutWrapper() +{ + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto layoutProperty = host->GetLayoutProperty(); + CHECK_NULL_VOID(layoutProperty); + auto layoutPolicy = layoutProperty->GetLayoutPolicyProperty(); + if (layoutPolicy.has_value() && (layoutPolicy->IsWrap() || layoutPolicy->IsFix())) { + layoutProperty->UpdateUserDefinedIdealSize( + CalcSize(CalcLength(DEFAULT_SIZE_ZERO), CalcLength(DEFAULT_SIZE_ZERO))); + } +} + } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/text_picker/textpicker_pattern.h b/frameworks/core/components_ng/pattern/text_picker/textpicker_pattern.h index 3829351b35d..e4d63e1ca4d 100644 --- a/frameworks/core/components_ng/pattern/text_picker/textpicker_pattern.h +++ b/frameworks/core/components_ng/pattern/text_picker/textpicker_pattern.h @@ -49,6 +49,8 @@ public: ~TextPickerPattern() override = default; + void BeforeCreateLayoutWrapper() override; + bool IsAtomicNode() const override { return true; diff --git a/frameworks/core/components_ng/pattern/time_picker/timepicker_row_pattern.cpp b/frameworks/core/components_ng/pattern/time_picker/timepicker_row_pattern.cpp index 1b29e5f0dd9..47ca922c7e3 100644 --- a/frameworks/core/components_ng/pattern/time_picker/timepicker_row_pattern.cpp +++ b/frameworks/core/components_ng/pattern/time_picker/timepicker_row_pattern.cpp @@ -62,6 +62,7 @@ const uint32_t INDEX_SECOND_ADD_ZERO = 10; const uint32_t INDEX_SECOND_END = 59; const uint32_t SIZE_OF_AMPM_COLUMN_OPTION = 2; constexpr float PICKER_MAXFONTSCALE = 1.0f; +constexpr float DEFAULT_SIZE_ZERO = 0.0f; } // namespace void TimePickerRowPattern::OnAttachToFrameNode() @@ -2400,4 +2401,17 @@ void TimePickerRowPattern::UpdateSelectedTextStyle(const PickerTextStyle& textSt ); } +void TimePickerRowPattern::BeforeCreateLayoutWrapper() +{ + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto layoutProperty = host->GetLayoutProperty(); + CHECK_NULL_VOID(layoutProperty); + auto layoutPolicy = layoutProperty->GetLayoutPolicyProperty(); + if (layoutPolicy.has_value() && (layoutPolicy->IsWrap() || layoutPolicy->IsFix())) { + layoutProperty->UpdateUserDefinedIdealSize( + CalcSize(CalcLength(DEFAULT_SIZE_ZERO), CalcLength(DEFAULT_SIZE_ZERO))); + } +} + } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/time_picker/timepicker_row_pattern.h b/frameworks/core/components_ng/pattern/time_picker/timepicker_row_pattern.h index 06648a224cc..e45ef3a8c82 100644 --- a/frameworks/core/components_ng/pattern/time_picker/timepicker_row_pattern.h +++ b/frameworks/core/components_ng/pattern/time_picker/timepicker_row_pattern.h @@ -51,6 +51,8 @@ public: ~TimePickerRowPattern() override = default; + void BeforeCreateLayoutWrapper() override; + bool IsAtomicNode() const override { return true; diff --git a/test/unittest/core/pattern/calendar_picker/calendar_picker_layout_algorithm_test.cpp b/test/unittest/core/pattern/calendar_picker/calendar_picker_layout_algorithm_test.cpp index 72c746f350e..585303bb395 100644 --- a/test/unittest/core/pattern/calendar_picker/calendar_picker_layout_algorithm_test.cpp +++ b/test/unittest/core/pattern/calendar_picker/calendar_picker_layout_algorithm_test.cpp @@ -46,6 +46,7 @@ const SizeF SELF_IDEAL_SIZE(600.0f, 1000.f); const Dimension ENTRY_BUTTON_WIDTH = Dimension(100.0, DimensionUnit::PX); const CalcSize CONSTRAINT_SIZE = { CalcLength(300.0), CalcLength(300.0) }; const SizeF CONSTRAINT_MAX_SIZE(300.0f, 300.f); +const CalcSize CONSTRAINT_SELF_SIZE = { CalcLength(400.0), CalcLength(400.0) }; } // namespace class CalendarPickerLayoutAlgorithmTest : public testing::Test { @@ -255,271 +256,6 @@ void CalendarPickerLayoutAlgorithmTest::GetCalendarPickerLayoutAlgorithm() ASSERT_NE(calendarPickerLayoutAlgorithm_, nullptr); } -/** - * @tc.name: UpdateFrameSizeWithLayoutPolicy001 - * @tc.desc: Test UpdateFrameSizeWithLayoutPolicy when layoutWrapper is not nullptr. - * @tc.type: FUNC - */ -HWTEST_F(CalendarPickerLayoutAlgorithmTest, UpdateFrameSizeWithLayoutPolicy001, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create CalendarPicker. - */ - CreateCalendarPicker(); - ASSERT_NE(frameNode_, nullptr); - - /** - * @tc.steps: step2. Get CalendarPicker layout algorithm. - */ - GetCalendarPickerLayoutAlgorithm(); - ASSERT_NE(calendarPickerLayoutAlgorithm_, nullptr); - - /** - * @tc.steps: step3. Call UpdateFrameSizeWithLayoutPolicy function when layoutWrapper is nullptr. - * @tc.expected: frameSize will not be changed. - */ - LayoutWrapper* layoutWrapperTmp = nullptr; - SizeF frameSize = { 1.0f, 1.0f }; - SizeF expectedValue = frameSize; - calendarPickerLayoutAlgorithm_->UpdateFrameSizeWithLayoutPolicy(layoutWrapperTmp, frameSize); - EXPECT_EQ(frameSize, expectedValue); -} - -/** - * @tc.name: UpdateFrameSizeWithLayoutPolicy002 - * @tc.desc: Test UpdateFrameSizeWithLayoutPolicy when layoutPolicy not has value. - * @tc.type: FUNC - */ -HWTEST_F(CalendarPickerLayoutAlgorithmTest, UpdateFrameSizeWithLayoutPolicy002, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create CalendarPicker. - */ - CreateCalendarPicker(); - ASSERT_NE(frameNode_, nullptr); - - /** - * @tc.steps: step2. Get CalendarPicker layout algorithm. - */ - GetCalendarPickerLayoutAlgorithm(); - ASSERT_NE(calendarPickerLayoutAlgorithm_, nullptr); - - /** - * @tc.steps: step3. Set layoutPolicy_ to std::nullopt. - */ - auto layoutProperty = layoutWrapper_->GetLayoutProperty(); - ASSERT_NE(layoutProperty, nullptr); - layoutProperty->layoutPolicy_ = std::nullopt; - - /** - * @tc.steps: step4. Call UpdateFrameSizeWithLayoutPolicy function when layoutPolicy not has value. - * @tc.expected: frameSize will not be changed. - */ - SizeF frameSize = { 1.0f, 1.0f }; - SizeF expectedValue = frameSize; - calendarPickerLayoutAlgorithm_->UpdateFrameSizeWithLayoutPolicy(AceType::RawPtr(layoutWrapper_), frameSize); - EXPECT_EQ(frameSize, expectedValue); -} - -/** - * @tc.name: UpdateFrameSizeWithLayoutPolicy003 - * @tc.desc: Test UpdateFrameSizeWithLayoutPolicy when widthLayoutPolicy_ and heightLayoutPolicy_ are match parent. - * @tc.type: FUNC - */ -HWTEST_F(CalendarPickerLayoutAlgorithmTest, UpdateFrameSizeWithLayoutPolicy003, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create CalendarPicker. - */ - CreateCalendarPicker(); - ASSERT_NE(frameNode_, nullptr); - - /** - * @tc.steps: step2. Get CalendarPicker layout algorithm. - */ - GetCalendarPickerLayoutAlgorithm(); - ASSERT_NE(calendarPickerLayoutAlgorithm_, nullptr); - - /** - * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::MATCH_PARENT. - */ - auto layoutProperty = layoutWrapper_->GetLayoutProperty(); - ASSERT_NE(layoutProperty, nullptr); - LayoutPolicyProperty layoutPolicyProperty; - layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; - layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; - layoutProperty->layoutPolicy_ = layoutPolicyProperty; - - /** - * @tc.steps: step4. set layout constraint to a valid value. - */ - LayoutConstraintF parentLayoutConstraint; - parentLayoutConstraint.maxSize = CONTAINER_SIZE; - parentLayoutConstraint.percentReference = CONTAINER_SIZE; - parentLayoutConstraint.selfIdealSize.SetSize(CONTAINER_SIZE); - parentLayoutConstraint.parentIdealSize.SetSize(CONTAINER_SIZE); - layoutProperty->UpdateLayoutConstraint(parentLayoutConstraint); - - /** - * @tc.steps: step4. Call UpdateFrameSizeWithLayoutPolicy function when layoutPolicy not has value. - * @tc.expected: frameSize will not be changed. - */ - SizeF frameSize = { 1.0f, 1.0f }; - calendarPickerLayoutAlgorithm_->UpdateFrameSizeWithLayoutPolicy(AceType::RawPtr(layoutWrapper_), frameSize); - EXPECT_EQ(frameSize, CONTAINER_SIZE); -} - -/** - * @tc.name: UpdateFrameSizeWithLayoutPolicy004 - * @tc.desc: Test UpdateFrameSizeWithLayoutPolicy when widthLayoutPolicy_ and heightLayoutPolicy_ are not match parent. - * @tc.type: FUNC - */ -HWTEST_F(CalendarPickerLayoutAlgorithmTest, UpdateFrameSizeWithLayoutPolicy004, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create CalendarPicker. - */ - CreateCalendarPicker(); - ASSERT_NE(frameNode_, nullptr); - - /** - * @tc.steps: step2. Get CalendarPicker layout algorithm. - */ - GetCalendarPickerLayoutAlgorithm(); - ASSERT_NE(calendarPickerLayoutAlgorithm_, nullptr); - - /** - * @tc.steps: step3. set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::NO_MATCH. - */ - auto layoutProperty = layoutWrapper_->GetLayoutProperty(); - ASSERT_NE(layoutProperty, nullptr); - LayoutPolicyProperty layoutPolicyProperty; - layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; - layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; - layoutProperty->layoutPolicy_ = layoutPolicyProperty; - - /** - * @tc.steps: step4. Set layout constraint to a valid value. - */ - LayoutConstraintF parentLayoutConstraint; - parentLayoutConstraint.maxSize = CONTAINER_SIZE; - parentLayoutConstraint.percentReference = CONTAINER_SIZE; - parentLayoutConstraint.selfIdealSize.SetSize(CONTAINER_SIZE); - parentLayoutConstraint.parentIdealSize.SetSize(CONTAINER_SIZE); - layoutProperty->UpdateLayoutConstraint(parentLayoutConstraint); - - /** - * @tc.steps: step4. Call UpdateFrameSizeWithLayoutPolicy function when layoutPolicy not has value. - * @tc.expected: frameSize will not be changed. - */ - SizeF frameSize = { 1.0f, 1.0f }; - SizeF expectedValue = frameSize; - calendarPickerLayoutAlgorithm_->UpdateFrameSizeWithLayoutPolicy(AceType::RawPtr(layoutWrapper_), frameSize); - EXPECT_EQ(frameSize, expectedValue); -} - -/** - * @tc.name: UpdateFrameSizeWithLayoutPolicy005 - * @tc.desc: Test UpdateFrameSizeWithLayoutPolicy when widthLayoutPolicy_ is not match parent, - * and heightLayoutPolicy_ is match parent. - * @tc.type: FUNC - */ -HWTEST_F(CalendarPickerLayoutAlgorithmTest, UpdateFrameSizeWithLayoutPolicy005, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create CalendarPicker. - */ - CreateCalendarPicker(); - ASSERT_NE(frameNode_, nullptr); - - /** - * @tc.steps: step2. Get CalendarPicker layout algorithm. - */ - GetCalendarPickerLayoutAlgorithm(); - ASSERT_NE(calendarPickerLayoutAlgorithm_, nullptr); - - /** - * @tc.steps: step3. Set widthLayoutPolicy_ to LayoutCalPolicy::NO_MATCH, - * heightLayoutPolicy_ to LayoutCalPolicy::MATCH_PARENT. - */ - auto layoutProperty = layoutWrapper_->GetLayoutProperty(); - ASSERT_NE(layoutProperty, nullptr); - LayoutPolicyProperty layoutPolicyProperty; - layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; - layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; - layoutProperty->layoutPolicy_ = layoutPolicyProperty; - - /** - * @tc.steps: step4. set layout constraint to a valid value. - */ - LayoutConstraintF parentLayoutConstraint; - parentLayoutConstraint.maxSize = CONTAINER_SIZE; - parentLayoutConstraint.percentReference = CONTAINER_SIZE; - parentLayoutConstraint.selfIdealSize.SetSize(CONTAINER_SIZE); - parentLayoutConstraint.parentIdealSize.SetSize(CONTAINER_SIZE); - layoutProperty->UpdateLayoutConstraint(parentLayoutConstraint); - - /** - * @tc.steps: step4. Call UpdateFrameSizeWithLayoutPolicy function when layoutPolicy not has value. - * @tc.expected: frameSize will not be changed. - */ - SizeF frameSize = { 1.0f, 1.0f }; - calendarPickerLayoutAlgorithm_->UpdateFrameSizeWithLayoutPolicy(AceType::RawPtr(layoutWrapper_), frameSize); - EXPECT_EQ(frameSize.Width(), 1.0f); - EXPECT_EQ(frameSize.Height(), CONTAINER_SIZE.Height()); -} - -/** - * @tc.name: UpdateFrameSizeWithLayoutPolicy006 - * @tc.desc: Test UpdateFrameSizeWithLayoutPolicy when widthLayoutPolicy_ is match parent, - * and heightLayoutPolicy_ is not match parent. - * @tc.type: FUNC - */ -HWTEST_F(CalendarPickerLayoutAlgorithmTest, UpdateFrameSizeWithLayoutPolicy006, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create CalendarPicker. - */ - CreateCalendarPicker(); - ASSERT_NE(frameNode_, nullptr); - - /** - * @tc.steps: step2. Get CalendarPicker layout algorithm. - */ - GetCalendarPickerLayoutAlgorithm(); - ASSERT_NE(calendarPickerLayoutAlgorithm_, nullptr); - - /** - * @tc.steps: step3. Set widthLayoutPolicy_ to LayoutCalPolicy::MATCH_PARENT, - * heightLayoutPolicy_ to LayoutCalPolicy::NO_MATCH. - */ - auto layoutProperty = layoutWrapper_->GetLayoutProperty(); - ASSERT_NE(layoutProperty, nullptr); - LayoutPolicyProperty layoutPolicyProperty; - layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; - layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; - layoutProperty->layoutPolicy_ = layoutPolicyProperty; - - /** - * @tc.steps: step4. set layout constraint to a valid value. - */ - LayoutConstraintF parentLayoutConstraint; - parentLayoutConstraint.maxSize = CONTAINER_SIZE; - parentLayoutConstraint.percentReference = CONTAINER_SIZE; - parentLayoutConstraint.selfIdealSize.SetSize(CONTAINER_SIZE); - parentLayoutConstraint.parentIdealSize.SetSize(CONTAINER_SIZE); - layoutProperty->UpdateLayoutConstraint(parentLayoutConstraint); - - /** - * @tc.steps: step4. Call UpdateFrameSizeWithLayoutPolicy function when layoutPolicy not has value. - * @tc.expected: frameSize will not be changed. - */ - SizeF frameSize = { 1.0f, 1.0f }; - calendarPickerLayoutAlgorithm_->UpdateFrameSizeWithLayoutPolicy(AceType::RawPtr(layoutWrapper_), frameSize); - EXPECT_EQ(frameSize.Width(), CONTAINER_SIZE.Width()); - EXPECT_EQ(frameSize.Height(), 1.0f); -} - /** * @tc.name: CalendarPickerContentMeasure001 * @tc.desc: Test CalendarPickerContentMeasure when widthLayoutPolicy_ and heightLayoutPolicy_ are not match parent. @@ -708,7 +444,7 @@ HWTEST_F(CalendarPickerLayoutAlgorithmTest, CalendarPickerContentMeasure003, Tes /** * @tc.steps: step4. Call CalendarPickerContentMeasure function. - * @tc.expected: The value of frameSize will be changed to SELF_IDEAL_SIZE. + * @tc.expected: The value of frameSize will be changed to CONSTRAINT_MAX_SIZE. */ calendarPickerLayoutAlgorithm_->CalendarPickerContentMeasure(AceType::RawPtr(layoutWrapper_)); auto contentWrapper = layoutWrapper_->GetOrCreateChildByIndex(0); @@ -717,7 +453,7 @@ HWTEST_F(CalendarPickerLayoutAlgorithmTest, CalendarPickerContentMeasure003, Tes ASSERT_NE(contentGeometryNode, nullptr); SizeF geometryFrameSize = contentGeometryNode->GetFrameSize(); - EXPECT_EQ(geometryFrameSize, SELF_IDEAL_SIZE); + EXPECT_EQ(geometryFrameSize, CONSTRAINT_MAX_SIZE); } /** @@ -760,9 +496,14 @@ HWTEST_F(CalendarPickerLayoutAlgorithmTest, CalendarPickerContentMeasure004, Tes layoutProperty->UpdateLayoutConstraint(parentLayoutConstraint); layoutProperty->UpdateContentConstraint(); + MeasureProperty constraint; + constraint.selfIdealSize = CONSTRAINT_SELF_SIZE; + constraint.maxSize = CONSTRAINT_SIZE; + layoutProperty->UpdateCalcLayoutProperty(constraint); + /** * @tc.steps: step4. Call CalendarPickerContentMeasure function. - * @tc.expected: The value of frameSize will be changed to SELF_IDEAL_SIZE. + * @tc.expected: The value of frameSize will be changed to CONSTRAINT_MAX_SIZE. */ calendarPickerLayoutAlgorithm_->CalendarPickerContentMeasure(AceType::RawPtr(layoutWrapper_)); auto contentWrapper = layoutWrapper_->GetOrCreateChildByIndex(0); @@ -771,7 +512,7 @@ HWTEST_F(CalendarPickerLayoutAlgorithmTest, CalendarPickerContentMeasure004, Tes ASSERT_NE(contentGeometryNode, nullptr); SizeF geometryFrameSize = contentGeometryNode->GetFrameSize(); - EXPECT_EQ(geometryFrameSize, SELF_IDEAL_SIZE); + EXPECT_EQ(geometryFrameSize, CONSTRAINT_MAX_SIZE); } /** @@ -834,7 +575,7 @@ HWTEST_F(CalendarPickerLayoutAlgorithmTest, CalendarPickerContentMeasure005, Tes /** * @tc.name: CalendarPickerMeasure001 - * @tc.desc: Test CalendarPickerContentMeasure when widthLayoutPolicy_ and heightLayoutPolicy_ are matchParent. + * @tc.desc: Test BeforeCreateLayoutWrapper when widthLayoutPolicy_ and heightLayoutPolicy_ are matchParent. * @tc.type: FUNC */ HWTEST_F(CalendarPickerLayoutAlgorithmTest, CalendarPickerMeasure001, TestSize.Level1) diff --git a/test/unittest/core/pattern/picker/date_picker_column_layout_algorithm_test_ng.cpp b/test/unittest/core/pattern/picker/date_picker_column_layout_algorithm_test_ng.cpp index 7ef0937c53d..92563ebbd20 100644 --- a/test/unittest/core/pattern/picker/date_picker_column_layout_algorithm_test_ng.cpp +++ b/test/unittest/core/pattern/picker/date_picker_column_layout_algorithm_test_ng.cpp @@ -26,6 +26,9 @@ #include "core/components/button/button_theme.h" #include "core/components/dialog/dialog_theme.h" #include "core/components_ng/pattern/picker/datepicker_column_layout_algorithm.h" +#include "core/components_ng/pattern/picker/datepicker_pattern.h" +#include "core/components_ng/pattern/picker/datepicker_model_ng.h" +#include "core/components_ng/pattern/flex/flex_layout_property.h" #undef private #undef protected @@ -34,6 +37,10 @@ using namespace testing; using namespace testing::ext; namespace OHOS::Ace::NG { +namespace { + constexpr float USER_IDEAL_SIZE_0 = 0.0f; + constexpr float USER_IDEAL_SIZE_400 = 400.0f; +} class DatePickerColumnLayoutAlgorithmTest : public testing::Test { public: @@ -354,4 +361,249 @@ HWTEST_F(DatePickerColumnLayoutAlgorithmTest, ReCalcItemHeightScale005, TestSize res = datePickerColumnLayoutAlgorithm.ReCalcItemHeightScale(userSetHeight, isDividerSpacing); EXPECT_EQ(res, expectValue); } + +/** + * @tc.name: BeforeCreateLayoutWrapper001 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is WRAP_CONTENT. + * @tc.type: FUNC + */ +HWTEST_F(DatePickerColumnLayoutAlgorithmTest, BeforeCreateLayoutWrapper001, TestSize.Level1) +{ + /** + * @tc.steps: step1. create DatePicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + DatePickerModel::GetInstance()->CreateDatePicker(theme); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::DATE_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the DatePicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::WRAP_CONTENT. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the DatePicker will be set to 0. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_0)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_0)); +} + +/** + * @tc.name: BeforeCreateLayoutWrapper002 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is FIX_AT_IDEAL_SIZE. + * @tc.type: FUNC + */ +HWTEST_F(DatePickerColumnLayoutAlgorithmTest, BeforeCreateLayoutWrapper002, TestSize.Level1) +{ + /** + * @tc.steps: step1. create DatePicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + DatePickerModel::GetInstance()->CreateDatePicker(theme); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::DATE_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the DatePicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::FIX_AT_IDEAL_SIZE. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the DatePicker will be set to 0. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_0)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_0)); +} + +/** + * @tc.name: BeforeCreateLayoutWrapper003 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is MATCH_PARENT. + * @tc.type: FUNC + */ +HWTEST_F(DatePickerColumnLayoutAlgorithmTest, BeforeCreateLayoutWrapper003, TestSize.Level1) +{ + /** + * @tc.steps: step1. create DatePicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + DatePickerModel::GetInstance()->CreateDatePicker(theme); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::DATE_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the DatePicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::MATCH_PARENT. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the DatePicker will not be changed. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); +} + +/** + * @tc.name: BeforeCreateLayoutWrapper004 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is NO_MATCH. + * @tc.type: FUNC + */ +HWTEST_F(DatePickerColumnLayoutAlgorithmTest, BeforeCreateLayoutWrapper004, TestSize.Level1) +{ + /** + * @tc.steps: step1. create DatePicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + DatePickerModel::GetInstance()->CreateDatePicker(theme); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::DATE_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the DatePicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::NO_MATCH. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the DatePicker will not be changed. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); +} + } // namespace OHOS::Ace::NG diff --git a/test/unittest/core/pattern/text_picker/text_picker_layout_algorithm_test_ng.cpp b/test/unittest/core/pattern/text_picker/text_picker_layout_algorithm_test_ng.cpp index 0704ef0193e..356aa4f8bf0 100644 --- a/test/unittest/core/pattern/text_picker/text_picker_layout_algorithm_test_ng.cpp +++ b/test/unittest/core/pattern/text_picker/text_picker_layout_algorithm_test_ng.cpp @@ -26,6 +26,9 @@ #include "core/components/button/button_theme.h" #include "core/components/dialog/dialog_theme.h" #include "core/components_ng/pattern/text_picker/textpicker_layout_algorithm.h" +#include "core/components_ng/pattern/text_picker/textpicker_pattern.h" +#include "core/components_ng/pattern/text_picker/textpicker_model_ng.h" +#include "core/components_ng/pattern/flex/flex_layout_property.h" #undef private #undef protected @@ -34,6 +37,11 @@ using namespace testing; using namespace testing::ext; namespace OHOS::Ace::NG { +namespace { + constexpr float USER_IDEAL_SIZE_0 = 0.0f; + constexpr float USER_IDEAL_SIZE_400 = 400.0f; + constexpr uint32_t SELECTED_INDEX_1 = 1; +} class TextPickerLayoutAlgorithmTest : public testing::Test { public: @@ -354,4 +362,261 @@ HWTEST_F(TextPickerLayoutAlgorithmTest, ReCalcItemHeightScale005, TestSize.Level res = textPickerLayoutAlgorithm.ReCalcItemHeightScale(userSetHeight, isDividerSpacing); EXPECT_EQ(res, expectValue); } + +/** + * @tc.name: BeforeCreateLayoutWrapper001 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is WRAP_CONTENT. + * @tc.type: FUNC + */ +HWTEST_F(TextPickerLayoutAlgorithmTest, BeforeCreateLayoutWrapper001, TestSize.Level1) +{ + /** + * @tc.steps: step1. create TextPicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + TextPickerModelNG::GetInstance()->Create(theme, TEXT); + std::vector range = { { "", "Text1" }, { "", "Text2" }, { "", "Text3" } }; + TextPickerModelNG::GetInstance()->SetRange(range); + TextPickerModelNG::GetInstance()->SetSelected(SELECTED_INDEX_1); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::TEXT_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the TextPicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::WRAP_CONTENT. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the TextPicker will be set to 0. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_0)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_0)); +} + +/** + * @tc.name: BeforeCreateLayoutWrapper002 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is FIX_AT_IDEAL_SIZE. + * @tc.type: FUNC + */ +HWTEST_F(TextPickerLayoutAlgorithmTest, BeforeCreateLayoutWrapper002, TestSize.Level1) +{ + /** + * @tc.steps: step1. create TextPicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + TextPickerModelNG::GetInstance()->Create(theme, TEXT); + std::vector range = { { "", "Text1" }, { "", "Text2" }, { "", "Text3" } }; + TextPickerModelNG::GetInstance()->SetRange(range); + TextPickerModelNG::GetInstance()->SetSelected(SELECTED_INDEX_1); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::TEXT_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the TextPicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::FIX_AT_IDEAL_SIZE. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the TextPicker will be set to 0. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_0)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_0)); +} + +/** + * @tc.name: BeforeCreateLayoutWrapper003 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is MATCH_PARENT. + * @tc.type: FUNC + */ +HWTEST_F(TextPickerLayoutAlgorithmTest, BeforeCreateLayoutWrapper003, TestSize.Level1) +{ + /** + * @tc.steps: step1. create TextPicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + TextPickerModelNG::GetInstance()->Create(theme, TEXT); + std::vector range = { { "", "Text1" }, { "", "Text2" }, { "", "Text3" } }; + TextPickerModelNG::GetInstance()->SetRange(range); + TextPickerModelNG::GetInstance()->SetSelected(SELECTED_INDEX_1); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::TEXT_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the TextPicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::MATCH_PARENT. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the TextPicker will not be changed. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); +} + +/** + * @tc.name: BeforeCreateLayoutWrapper004 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is NO_MATCH. + * @tc.type: FUNC + */ +HWTEST_F(TextPickerLayoutAlgorithmTest, BeforeCreateLayoutWrapper004, TestSize.Level1) +{ + /** + * @tc.steps: step1. create TextPicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + TextPickerModelNG::GetInstance()->Create(theme, TEXT); + std::vector range = { { "", "Text1" }, { "", "Text2" }, { "", "Text3" } }; + TextPickerModelNG::GetInstance()->SetRange(range); + TextPickerModelNG::GetInstance()->SetSelected(SELECTED_INDEX_1); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::TEXT_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the TextPicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::NO_MATCH. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the TextPicker will not be changed. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); +} + } // namespace OHOS::Ace::NG diff --git a/test/unittest/core/pattern/time_picker/time_picker_column_layout_algorithm_test_ng.cpp b/test/unittest/core/pattern/time_picker/time_picker_column_layout_algorithm_test_ng.cpp index ee2b9c97c09..ddfd854de34 100644 --- a/test/unittest/core/pattern/time_picker/time_picker_column_layout_algorithm_test_ng.cpp +++ b/test/unittest/core/pattern/time_picker/time_picker_column_layout_algorithm_test_ng.cpp @@ -26,6 +26,9 @@ #include "core/components/button/button_theme.h" #include "core/components/dialog/dialog_theme.h" #include "core/components_ng/pattern/time_picker/timepicker_column_layout_algorithm.h" +#include "core/components_ng/pattern/time_picker/timepicker_row_pattern.h" +#include "core/components_ng/pattern/time_picker/timepicker_model_ng.h" +#include "core/components_ng/pattern/flex/flex_layout_property.h" #undef private #undef protected @@ -34,6 +37,10 @@ using namespace testing; using namespace testing::ext; namespace OHOS::Ace::NG { +namespace { + constexpr float USER_IDEAL_SIZE_0 = 0.0f; + constexpr float USER_IDEAL_SIZE_400 = 400.0f; +} class TimePickerColumnLayoutAlgorithmTest : public testing::Test { public: @@ -354,4 +361,249 @@ HWTEST_F(TimePickerColumnLayoutAlgorithmTest, ReCalcItemHeightScale005, TestSize res = timePickerColumnLayoutAlgorithm.ReCalcItemHeightScale(userSetHeight, isDividerSpacing); EXPECT_EQ(res, expectValue); } + +/** + * @tc.name: BeforeCreateLayoutWrapper001 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is WRAP_CONTENT. + * @tc.type: FUNC + */ +HWTEST_F(TimePickerColumnLayoutAlgorithmTest, BeforeCreateLayoutWrapper001, TestSize.Level1) +{ + /** + * @tc.steps: step1. create TimePicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + TimePickerModel::GetInstance()->CreateTimePicker(theme); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::TIME_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the TimePicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::WRAP_CONTENT. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the TimePicker will be set to 0. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_0)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_0)); +} + +/** + * @tc.name: BeforeCreateLayoutWrapper002 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is FIX_AT_IDEAL_SIZE. + * @tc.type: FUNC + */ +HWTEST_F(TimePickerColumnLayoutAlgorithmTest, BeforeCreateLayoutWrapper002, TestSize.Level1) +{ + /** + * @tc.steps: step1. create TimePicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + TimePickerModel::GetInstance()->CreateTimePicker(theme); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::TIME_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the TimePicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::FIX_AT_IDEAL_SIZE. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the TimePicker will be set to 0. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_0)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_0)); +} + +/** + * @tc.name: BeforeCreateLayoutWrapper003 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is MATCH_PARENT. + * @tc.type: FUNC + */ +HWTEST_F(TimePickerColumnLayoutAlgorithmTest, BeforeCreateLayoutWrapper003, TestSize.Level1) +{ + /** + * @tc.steps: step1. create TimePicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + TimePickerModel::GetInstance()->CreateTimePicker(theme); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::TIME_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the TimePicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::MATCH_PARENT. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::MATCH_PARENT; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the TimePicker will not be changed. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); +} + +/** + * @tc.name: BeforeCreateLayoutWrapper004 + * @tc.desc: Test BeforeCreateLayoutWrapper when layoutPolicy is NO_MATCH. + * @tc.type: FUNC + */ +HWTEST_F(TimePickerColumnLayoutAlgorithmTest, BeforeCreateLayoutWrapper004, TestSize.Level1) +{ + /** + * @tc.steps: step1. create TimePicker. + */ + auto theme = MockPipelineContext::GetCurrent()->GetTheme(); + TimePickerModel::GetInstance()->CreateTimePicker(theme); + RefPtr element = ViewStackProcessor::GetInstance()->Finish(); + EXPECT_EQ(element->GetTag(), V2::TIME_PICKER_ETS_TAG); + auto frameNode = AceType::DynamicCast(element); + ASSERT_NE(frameNode, nullptr); + frameNode->MarkModifyDone(); + + auto pickerPattern = frameNode->GetPattern(); + ASSERT_NE(pickerPattern, nullptr); + pickerPattern->OnModifyDone(); + auto layoutAlgorithm = pickerPattern->CreateLayoutAlgorithm(); + + /** + * @tc.steps: step2. Set a specified size for the TimePicker. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + RefPtr layoutWrapper = + AceType::MakeRefPtr(frameNode, geometryNode, frameNode->GetLayoutProperty()); + ASSERT_NE(layoutWrapper, nullptr); + layoutWrapper->SetLayoutAlgorithm(AccessibilityManager::MakeRefPtr(layoutAlgorithm)); + + auto pickerProperty = frameNode->GetLayoutProperty(); + ASSERT_NE(pickerProperty, nullptr); + pickerProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(USER_IDEAL_SIZE_400), + CalcLength(USER_IDEAL_SIZE_400))); + auto layoutProperty = layoutWrapper->GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); + + /** + * @tc.steps: step3. Set widthLayoutPolicy_ and heightLayoutPolicy_ to LayoutCalPolicy::NO_MATCH. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + + /** + * @tc.steps: step4. Call BeforeCreateLayoutWrapper function. + * @tc.expected: The width and height of the TimePicker will not be changed. + */ + pickerPattern->BeforeCreateLayoutWrapper(); + EXPECT_TRUE(layoutProperty->calcLayoutConstraint_->selfIdealSize.has_value()); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Width().value(), CalcLength(USER_IDEAL_SIZE_400)); + EXPECT_EQ(layoutProperty->calcLayoutConstraint_->selfIdealSize->Height().value(), CalcLength(USER_IDEAL_SIZE_400)); +} + } // namespace OHOS::Ace::NG -- Gitee