diff --git a/frameworks/core/components_ng/pattern/rating/rating_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/rating/rating_layout_algorithm.cpp index 6d2c9fd3d77e3ec2a4331c9ace35e1baccf7b4f2..5a5d78baec7b065ba7f8d5bcaf91b27f5d5c6919 100644 --- a/frameworks/core/components_ng/pattern/rating/rating_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/rating/rating_layout_algorithm.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -26,11 +26,8 @@ std::optional RatingLayoutAlgorithm::MeasureContent( auto pattern = host->GetPattern(); CHECK_NULL_RETURN(pattern, std::nullopt); if (pattern->UseContentModifier()) { - if (host->GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) { - host->GetGeometryNode()->ResetContent(); - } else { - host->GetGeometryNode()->Reset(); - } + host->GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN) ? host->GetGeometryNode()->ResetContent() + : host->GetGeometryNode()->Reset(); return std::nullopt; } // case 1: rating component is set with valid size, return contentConstraint.selfIdealSize as component size @@ -46,10 +43,22 @@ std::optional RatingLayoutAlgorithm::MeasureContent( auto stars = ratingLayoutProperty->GetStarsValue(ratingTheme->GetStarNum()); CHECK_EQUAL_RETURN(stars, 0, std::nullopt); + bool indicator = ratingLayoutProperty->GetIndicator().value_or(false); + // Rating use the mini size specified in the theme, when it is used as indicator. + auto defaultHeight = + indicator ? ratingTheme->GetRatingMiniHeight().ConvertToPx() : ratingTheme->GetRatingHeight().ConvertToPx(); + auto defaultWidth = static_cast(defaultHeight * stars); + auto layoutPolicy = ratingLayoutProperty->GetLayoutPolicyProperty(); if (layoutPolicy.has_value() && layoutPolicy->IsMatch()) { return LayoutPolicyIsMatchParent(contentConstraint, layoutPolicy, stars); } + if (layoutPolicy.has_value() && layoutPolicy->IsFix()) { + return LayoutPolicyIsFixAtIdelSize(contentConstraint, layoutPolicy, stars, defaultWidth, defaultHeight); + } + if (layoutPolicy.has_value() && layoutPolicy->IsWrap()) { + return LayoutPolicyIsWrapContent(contentConstraint, layoutPolicy, stars, defaultWidth, defaultHeight); + } // case 2: rating component is only set with valid width or height // return height = width / stars, or width = height * stars. if (contentConstraint.selfIdealSize.Width() && !contentConstraint.selfIdealSize.Height()) { @@ -62,13 +71,7 @@ std::optional RatingLayoutAlgorithm::MeasureContent( } // case 3: Using the theme's height and width by default if rating component is not set size. - SizeF componentSize; - bool indicator = ratingLayoutProperty->GetIndicator().value_or(false); - // Rating use the mini size specified in the theme, when it is used as indicator. - auto height = - indicator ? ratingTheme->GetRatingMiniHeight().ConvertToPx() : ratingTheme->GetRatingHeight().ConvertToPx(); - componentSize.SetHeight(static_cast(height)); - componentSize.SetWidth(static_cast(height * stars)); + SizeF componentSize(static_cast(defaultWidth), static_cast(defaultHeight)); return contentConstraint.Constrain(componentSize); } @@ -141,4 +144,72 @@ std::optional RatingLayoutAlgorithm::LayoutPolicyIsMatchParent(const Layo return SizeF(width, height); } +std::optional RatingLayoutAlgorithm::LayoutPolicyIsFixAtIdelSize(const LayoutConstraintF& contentConstraint, + std::optional layoutPolicy, int32_t stars, float defaultWidth, float defaultHeight) +{ + CHECK_NULL_RETURN(layoutPolicy, std::nullopt); + float width = 0.0f; + float height = 0.0f; + if (stars <= 0) { + return SizeF(width, height); + } + auto parentWidth = contentConstraint.parentIdealSize.Width().value_or(0.0f); + auto parentHeight = contentConstraint.parentIdealSize.Height().value_or(0.0f); + if (layoutPolicy->IsWidthFix() && layoutPolicy->IsHeightFix()) { + height = defaultHeight; + width = defaultWidth; + } else if (layoutPolicy->IsWidthFix()) { + if (contentConstraint.selfIdealSize.Height()) { + height = contentConstraint.selfIdealSize.Height().value(); + } else { + height = std::min(defaultHeight, parentHeight); + } + width = height * static_cast(stars); + } else if (layoutPolicy->IsHeightFix()) { + if (contentConstraint.selfIdealSize.Width()) { + width = contentConstraint.selfIdealSize.Width().value(); + } else { + width = std::min(defaultWidth, parentWidth); + } + height = width / static_cast(stars); + } + return SizeF(width, height); +} + +std::optional RatingLayoutAlgorithm::LayoutPolicyIsWrapContent(const LayoutConstraintF& contentConstraint, + std::optional layoutPolicy, int32_t stars, float defaultWidth, float defaultHeight) +{ + CHECK_NULL_RETURN(layoutPolicy, std::nullopt); + float width = 0.0f; + float height = 0.0f; + if (stars <= 0) { + return SizeF(width, height); + } + auto parentHeight = contentConstraint.parentIdealSize.Height().value_or(0.0f); + auto parentWidth = contentConstraint.parentIdealSize.Width().value_or(0.0f); + auto selfHeight = contentConstraint.selfIdealSize.Height().value_or(0.0f); + auto selfWidth = contentConstraint.selfIdealSize.Width().value_or(0.0f); + + if (layoutPolicy->IsWidthWrap() && layoutPolicy->IsHeightWrap()) { + height = std::min(defaultHeight, parentHeight); + width = std::min(height * static_cast(stars), parentWidth); + } else if (layoutPolicy->IsWidthWrap()) { + if (!contentConstraint.selfIdealSize.Height().has_value()) { + height = std::min(defaultHeight, parentHeight); + width = std::min(height * static_cast(stars), parentWidth); + } else { + height = selfHeight; + width = std::min(height * static_cast(stars), parentWidth); + } + } else if (layoutPolicy->IsHeightWrap()) { + if (!contentConstraint.selfIdealSize.Width().has_value()) { + width = std::min(defaultWidth, parentWidth); + height = std::min(width / static_cast(stars), parentHeight); + } else { + width = selfWidth; + height = std::min(width / static_cast(stars), parentHeight); + } + } + return SizeF(width, height); +} } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/rating/rating_layout_algorithm.h b/frameworks/core/components_ng/pattern/rating/rating_layout_algorithm.h index 0bf69dab61ffa4e26c6c9f1db1b987daed5cbeb5..d87e897fb4eefb770814af3c1831e129fb1b1b5a 100644 --- a/frameworks/core/components_ng/pattern/rating/rating_layout_algorithm.h +++ b/frameworks/core/components_ng/pattern/rating/rating_layout_algorithm.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -53,6 +53,12 @@ public: std::optional LayoutPolicyIsMatchParent(const LayoutConstraintF& contentConstraint, std::optional layoutPolicy, int32_t stars); + std::optional LayoutPolicyIsFixAtIdelSize(const LayoutConstraintF& contentConstraint, + std::optional layoutPolicy, int32_t stars, float defaultWidth = 0.0f, + float defaultHeight = 0.0f); + std::optional LayoutPolicyIsWrapContent(const LayoutConstraintF& contentConstraint, + std::optional layoutPolicy, int32_t stars, float defaultWidth = 0.0f, + float defaultHeight = 0.0f); private: RefPtr foregroundLoadingCtx_; diff --git a/frameworks/core/components_ng/pattern/rating/rating_pattern.h b/frameworks/core/components_ng/pattern/rating/rating_pattern.h index ea6f11a81de45d967b73c01475d86895dfe4d410..bb99c012576015e25bcf83a5673a6a611fa0d88a 100644 --- a/frameworks/core/components_ng/pattern/rating/rating_pattern.h +++ b/frameworks/core/components_ng/pattern/rating/rating_pattern.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -115,6 +115,11 @@ public: return true; } + bool IsEnableFix() override + { + return true; + } + void LoadForeground(const RefPtr& layoutProperty, const RefPtr& ratingTheme, const RefPtr& iconTheme); void LoadSecondary(const RefPtr& layoutProperty, const RefPtr& ratingTheme, diff --git a/frameworks/core/components_ng/pattern/slider/slider_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/slider/slider_layout_algorithm.cpp index 1cb0b1a18ac025dda50ebc383d545fdb22f47cde..5788276ac16ef737bbf2f914094c17c5672a5af8 100644 --- a/frameworks/core/components_ng/pattern/slider/slider_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/slider/slider_layout_algorithm.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -128,6 +128,9 @@ std::optional SliderLayoutAlgorithm::MeasureContent( auto sliderWidth = CalculateSliderWidth(width, height, direction, hotBlockShadowWidth, mode); float sliderLength = CalculateSliderLength(width, height, direction, mode, (pattern->HasPrefix() || pattern->HasSuffix())); + if (layoutPolicy.has_value() && (layoutPolicy->IsFix() || layoutPolicy->IsWrap())) { + return SizeF(0.0, 0.0); + } return direction == Axis::HORIZONTAL ? SizeF(sliderLength, sliderWidth) : SizeF(sliderWidth, sliderLength); } diff --git a/frameworks/core/components_ng/pattern/slider/slider_pattern.h b/frameworks/core/components_ng/pattern/slider/slider_pattern.h index ebef86008c56675c90892b18d152656896aff478..a3c9db72c1b949114366183ae7ee94155c35a376 100644 --- a/frameworks/core/components_ng/pattern/slider/slider_pattern.h +++ b/frameworks/core/components_ng/pattern/slider/slider_pattern.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -150,6 +150,11 @@ public: return true; } + bool IsEnableFix() override + { + return true; + } + std::string ProvideRestoreInfo() override; void OnRestoreInfo(const std::string& restoreInfo) override; OffsetF CalculateGlobalSafeOffset(); diff --git a/test/unittest/core/pattern/rating/rating_test_ng.cpp b/test/unittest/core/pattern/rating/rating_test_ng.cpp index 219411441a3550ab438b96b5503588ecd3e2f5ae..f184f27dd975c3102c2d1f9b88155122db856e17 100644 --- a/test/unittest/core/pattern/rating/rating_test_ng.cpp +++ b/test/unittest/core/pattern/rating/rating_test_ng.cpp @@ -51,6 +51,7 @@ const bool RATING_INDICATOR = true; const bool RATING_INDICATOR_FALSE = false; const int32_t DEFAULT_STAR_NUM = 5; const int32_t RATING_STAR_NUM = 10; +const int32_t RATING_STAR_NUM_ZERO = 0; constexpr double RATING_SCORE = 3.0; constexpr double RATING_LAST_SCORE = 5.0; const std::string RATING_SCORE_STRING = ""; @@ -108,6 +109,8 @@ constexpr int32_t OFFSET_FIRST = 10; constexpr int32_t OFFSET_SECOND = 20; const SizeF TEST_SIZE_0 = SizeF(0.0f, 0.0f); const SizeF TEST_SIZE_200 = SizeF(200.0f, 200.0f); +const SizeF TEST_SIZE_100 = SizeF(100.0f, 100.0f); +const SizeF TEST_SIZE_10 = SizeF(10.0f, 10.0f); constexpr float TEST_WIDTH_50 = 50.0f; constexpr float TEST_HEIGHT_60 = 60.0f; } // namespace @@ -2341,6 +2344,385 @@ HWTEST_F(RatingTestNg, MeasureTest001, TestSize.Level1) EXPECT_EQ(ret->Height(), TEST_SIZE_200.Height()); } +/** + * @tc.name: MeasureTest002 + * @tc.desc: Test Rating MeasureContent. + * @tc.type: FUNC + */ +HWTEST_F(RatingTestNg, MeasureTest002, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create LayoutWrapperNode and RatingLayoutAlgorithm. + */ + RatingModelNG rating; + rating.Create(); + rating.SetIndicator(RATING_INDICATOR); + rating.SetStepSize(DEFAULT_STEP_SIZE); + rating.SetStars(RATING_STAR_NUM); + auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); + EXPECT_TRUE(frameNode != nullptr && frameNode->GetTag() == V2::RATING_ETS_TAG); + auto ratingPattern = frameNode->GetPattern(); + ASSERT_NE(ratingPattern, nullptr); + ratingPattern->SetRatingScore(RATING_SCORE); + auto ratingLayoutProperty = AceType::MakeRefPtr(); + ratingLayoutProperty->UpdateStars(DEFAULT_STAR_NUM); + ASSERT_NE(ratingLayoutProperty, nullptr); + LayoutWrapperNode layoutWrapper = LayoutWrapperNode(frameNode, nullptr, ratingLayoutProperty); + auto ratingLayoutAlgorithm = AceType::MakeRefPtr(nullptr, nullptr, nullptr, nullptr); + ASSERT_NE(ratingLayoutAlgorithm, nullptr); + + /** + * @tc.steps: step2. call MeasureContent function. + * @tc.expected: ret is not equal to TEST_SIZE_200. + */ + auto layoutProperty = layoutWrapper.GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + LayoutPolicyProperty layoutPolicyProperty; + LayoutConstraintF contentConstraint; + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_200); + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + auto ratingTheme = AceType::MakeRefPtr(); + ASSERT_NE(ratingTheme, nullptr); + auto ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_NE(ret, TEST_SIZE_200); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_NE(ret, TEST_SIZE_200); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, TEST_SIZE_0); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + contentConstraint.selfIdealSize.SetSize(TEST_SIZE_200); + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, TEST_SIZE_200); +} + +/** + * @tc.name: MeasureTest003 + * @tc.desc: Test Rating MeasureContent. + * @tc.type: FUNC + */ +HWTEST_F(RatingTestNg, MeasureTest003, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create LayoutWrapperNode and RatingLayoutAlgorithm. + */ + RatingModelNG rating; + rating.Create(); + rating.SetIndicator(RATING_INDICATOR); + rating.SetStepSize(DEFAULT_STEP_SIZE); + rating.SetStars(RATING_STAR_NUM); + auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); + EXPECT_TRUE(frameNode != nullptr && frameNode->GetTag() == V2::RATING_ETS_TAG); + auto ratingPattern = frameNode->GetPattern(); + ASSERT_NE(ratingPattern, nullptr); + ratingPattern->SetRatingScore(RATING_SCORE); + auto ratingLayoutProperty = AceType::MakeRefPtr(); + ratingLayoutProperty->UpdateStars(DEFAULT_STAR_NUM); + ASSERT_NE(ratingLayoutProperty, nullptr); + LayoutWrapperNode layoutWrapper = LayoutWrapperNode(frameNode, nullptr, ratingLayoutProperty); + auto ratingLayoutAlgorithm = AceType::MakeRefPtr(nullptr, nullptr, nullptr, nullptr); + ASSERT_NE(ratingLayoutAlgorithm, nullptr); + + /** + * @tc.steps: step2. call MeasureContent function. + * @tc.expected: ret is not equal to TEST_SIZE_200. + */ + auto layoutProperty = layoutWrapper.GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + LayoutPolicyProperty layoutPolicyProperty; + LayoutConstraintF contentConstraint; + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_200); + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + auto ratingTheme = AceType::MakeRefPtr(); + ASSERT_NE(ratingTheme, nullptr); + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + auto ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_NE(ret, TEST_SIZE_200); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + contentConstraint.selfIdealSize.SetSize(TEST_SIZE_200); + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, TEST_SIZE_200); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + contentConstraint.selfIdealSize.SetSize(TEST_SIZE_200); + ret = ratingLayoutAlgorithm->LayoutPolicyIsFixAtIdelSize(contentConstraint, layoutPolicyProperty, DEFAULT_STAR_NUM, + TEST_SIZE_200.Width(), TEST_SIZE_200.Height()); + EXPECT_NE(ret, TEST_SIZE_200); +} + +/** + * @tc.name: MeasureTest004 + * @tc.desc: Test Rating MeasureContent. + * @tc.type: FUNC + */ +HWTEST_F(RatingTestNg, MeasureTest004, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create LayoutWrapperNode and RatingLayoutAlgorithm. + */ + RatingModelNG rating; + rating.Create(); + rating.SetIndicator(RATING_INDICATOR); + rating.SetStepSize(DEFAULT_STEP_SIZE); + rating.SetStars(RATING_STAR_NUM); + auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); + EXPECT_TRUE(frameNode != nullptr && frameNode->GetTag() == V2::RATING_ETS_TAG); + auto ratingPattern = frameNode->GetPattern(); + ASSERT_NE(ratingPattern, nullptr); + ratingPattern->SetRatingScore(RATING_SCORE); + auto ratingLayoutProperty = AceType::MakeRefPtr(); + ratingLayoutProperty->UpdateStars(DEFAULT_STAR_NUM); + ASSERT_NE(ratingLayoutProperty, nullptr); + LayoutWrapperNode layoutWrapper = LayoutWrapperNode(frameNode, nullptr, ratingLayoutProperty); + auto ratingLayoutAlgorithm = AceType::MakeRefPtr(nullptr, nullptr, nullptr, nullptr); + ASSERT_NE(ratingLayoutAlgorithm, nullptr); + + /** + * @tc.steps: step2. call MeasureContent function. + * @tc.expected: ret is not equal to TEST_SIZE_200. + */ + auto layoutProperty = layoutWrapper.GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + LayoutPolicyProperty layoutPolicyProperty; + LayoutConstraintF contentConstraint; + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_200); + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + auto ratingTheme = AceType::MakeRefPtr(); + ASSERT_NE(ratingTheme, nullptr); + ratingTheme->ratingMiniHeight_ = Dimension(TEST_WIDTH_50); + ratingTheme->ratingHeight_ = Dimension(TEST_WIDTH_50); + auto ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_NE(ret, TEST_SIZE_200); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_NE(ret, TEST_SIZE_200); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_10); + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, TEST_SIZE_0); +} + +/** + * @tc.name: MeasureTest005 + * @tc.desc: Test Rating MeasureContent. + * @tc.type: FUNC + */ +HWTEST_F(RatingTestNg, MeasureTest005, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create LayoutWrapperNode and RatingLayoutAlgorithm. + */ + RatingModelNG rating; + rating.Create(); + rating.SetIndicator(RATING_INDICATOR); + rating.SetStepSize(DEFAULT_STEP_SIZE); + rating.SetStars(RATING_STAR_NUM); + auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); + EXPECT_TRUE(frameNode != nullptr && frameNode->GetTag() == V2::RATING_ETS_TAG); + auto ratingPattern = frameNode->GetPattern(); + ASSERT_NE(ratingPattern, nullptr); + ratingPattern->SetRatingScore(RATING_SCORE); + auto ratingLayoutProperty = AceType::MakeRefPtr(); + ratingLayoutProperty->UpdateStars(DEFAULT_STAR_NUM); + ASSERT_NE(ratingLayoutProperty, nullptr); + LayoutWrapperNode layoutWrapper = LayoutWrapperNode(frameNode, nullptr, ratingLayoutProperty); + auto ratingLayoutAlgorithm = AceType::MakeRefPtr(nullptr, nullptr, nullptr, nullptr); + ASSERT_NE(ratingLayoutAlgorithm, nullptr); + + /** + * @tc.steps: step2. call MeasureContent function. + * @tc.expected: ret is not equal to TEST_SIZE_200. + */ + auto layoutProperty = layoutWrapper.GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + auto ratingTheme = AceType::MakeRefPtr(); + ASSERT_NE(ratingTheme, nullptr); + ratingTheme->ratingMiniHeight_ = Dimension(TEST_WIDTH_50); + ratingTheme->ratingHeight_ = Dimension(TEST_WIDTH_50); + LayoutPolicyProperty layoutPolicyProperty; + LayoutConstraintF contentConstraint; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_200); + auto ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_NE(ret, TEST_SIZE_200); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_10); + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, TEST_SIZE_0); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + contentConstraint.selfIdealSize.SetSize(TEST_SIZE_200); + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, TEST_SIZE_200); +} + +/** + * @tc.name: MeasureTest006 + * @tc.desc: Test Rating MeasureContent. + * @tc.type: FUNC + */ +HWTEST_F(RatingTestNg, MeasureTest006, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create LayoutWrapperNode and RatingLayoutAlgorithm. + */ + RatingModelNG rating; + rating.Create(); + rating.SetIndicator(RATING_INDICATOR); + rating.SetStepSize(DEFAULT_STEP_SIZE); + rating.SetStars(RATING_STAR_NUM); + auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); + EXPECT_TRUE(frameNode != nullptr && frameNode->GetTag() == V2::RATING_ETS_TAG); + auto ratingPattern = frameNode->GetPattern(); + ASSERT_NE(ratingPattern, nullptr); + ratingPattern->SetRatingScore(RATING_SCORE); + auto ratingLayoutProperty = AceType::MakeRefPtr(); + ratingLayoutProperty->UpdateStars(DEFAULT_STAR_NUM); + ASSERT_NE(ratingLayoutProperty, nullptr); + LayoutWrapperNode layoutWrapper = LayoutWrapperNode(frameNode, nullptr, ratingLayoutProperty); + auto ratingLayoutAlgorithm = AceType::MakeRefPtr(nullptr, nullptr, nullptr, nullptr); + ASSERT_NE(ratingLayoutAlgorithm, nullptr); + + /** + * @tc.steps: step2. call MeasureContent function. + * @tc.expected: ret is not equal to TEST_SIZE_200. + */ + auto layoutProperty = layoutWrapper.GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + auto ratingTheme = AceType::MakeRefPtr(); + ASSERT_NE(ratingTheme, nullptr); + LayoutPolicyProperty layoutPolicyProperty; + LayoutConstraintF contentConstraint; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::NO_MATCH; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + ratingTheme->ratingMiniHeight_ = Dimension(TEST_WIDTH_50); + ratingTheme->ratingHeight_ = Dimension(TEST_WIDTH_50); + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_200); + auto ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, TEST_SIZE_0); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_10); + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_NE(ret, TEST_SIZE_200); + + /** + * @tc.steps: step3. call MeasureContent function. + * @tc.expected: ret is equal to TEST_SIZE_200. + */ + contentConstraint.selfIdealSize.SetSize(TEST_SIZE_200); + ret = ratingLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, TEST_SIZE_200); +} + +/** + * @tc.name: MeasureTest007 + * @tc.desc: Test Rating MeasureContent. + * @tc.type: FUNC + */ +HWTEST_F(RatingTestNg, MeasureTest007, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create LayoutWrapperNode and RatingLayoutAlgorithm. + */ + RatingModelNG rating; + rating.Create(); + rating.SetIndicator(RATING_INDICATOR); + rating.SetStepSize(DEFAULT_STEP_SIZE); + rating.SetStars(RATING_STAR_NUM); + auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); + EXPECT_TRUE(frameNode != nullptr && frameNode->GetTag() == V2::RATING_ETS_TAG); + auto ratingPattern = frameNode->GetPattern(); + ASSERT_NE(ratingPattern, nullptr); + ratingPattern->SetRatingScore(RATING_SCORE); + auto ratingLayoutProperty = AceType::MakeRefPtr(); + ratingLayoutProperty->UpdateStars(DEFAULT_STAR_NUM); + ASSERT_NE(ratingLayoutProperty, nullptr); + LayoutWrapperNode layoutWrapper = LayoutWrapperNode(frameNode, nullptr, ratingLayoutProperty); + auto ratingLayoutAlgorithm = AceType::MakeRefPtr(nullptr, nullptr, nullptr, nullptr); + ASSERT_NE(ratingLayoutAlgorithm, nullptr); + + /** + * @tc.steps: step2. call MeasureContent function. + * @tc.expected: ret is not equal to TEST_SIZE_0. + */ + auto layoutProperty = layoutWrapper.GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + LayoutPolicyProperty layoutPolicyProperty; + LayoutConstraintF contentConstraint; + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_200); + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + auto ratingTheme = AceType::MakeRefPtr(); + ASSERT_NE(ratingTheme, nullptr); + auto ret = ratingLayoutAlgorithm->LayoutPolicyIsWrapContent( + contentConstraint, layoutPolicyProperty, DEFAULT_STAR_NUM, TEST_SIZE_200.Width(), TEST_SIZE_200.Height()); + EXPECT_EQ(ret, TEST_SIZE_0); + + /** + * @tc.steps: step2. call MeasureContent function. + * @tc.expected: ret is not equal to TEST_SIZE_0. + */ + ret = ratingLayoutAlgorithm->LayoutPolicyIsWrapContent( + contentConstraint, layoutPolicyProperty, RATING_STAR_NUM_ZERO, TEST_SIZE_200.Width(), TEST_SIZE_200.Height()); + EXPECT_EQ(ret, TEST_SIZE_0); +} + /** * @tc.name: LayoutPolicyIsMatchParentTest001 * @tc.desc: Test Rating LayoutPolicyIsMatchParent. diff --git a/test/unittest/core/pattern/slider/slider_test_ng.cpp b/test/unittest/core/pattern/slider/slider_test_ng.cpp index f3e87f274461c8204db587fbb1c31609e9c305bb..da72af29d76e89ac79df1691221aee50158c75b0 100644 --- a/test/unittest/core/pattern/slider/slider_test_ng.cpp +++ b/test/unittest/core/pattern/slider/slider_test_ng.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -2342,6 +2342,68 @@ HWTEST_F(SliderTestNg, SliderTestNgMeasureContent0001, TestSize.Level1) EXPECT_EQ(ret->Width(), TEST_SIZE_200.Width()); } +/** + * @tc.name: SliderTestNgMeasureContent0002 + * @tc.desc: Test Slider MeasureContent. + * @tc.type: FUNC + */ +HWTEST_F(SliderTestNg, SliderTestNgMeasureContent0002, TestSize.Level1) +{ + /** + * @tc.steps: step1. Init Slider node. + */ + SliderModelNG sliderModelNG; + constexpr float stepValue = 10.0f; + constexpr float startValue = 70.0f; + sliderModelNG.Create(startValue, stepValue, MIN, MAX); + auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); + ASSERT_NE(frameNode, nullptr); + + /** + * @tc.steps: step2. Create LayoutWrapperNode and set sliderLayoutAlgorithm. + */ + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + LayoutWrapperNode layoutWrapper = LayoutWrapperNode(frameNode, geometryNode, frameNode->GetLayoutProperty()); + auto sliderPattern = frameNode->GetPattern(); + ASSERT_NE(sliderPattern, nullptr); + auto sliderLayoutAlgorithm = AceType::MakeRefPtr(); + ASSERT_NE(sliderLayoutAlgorithm, nullptr); + layoutWrapper.SetLayoutAlgorithm(AceType::MakeRefPtr(sliderLayoutAlgorithm)); + + /** + * @tc.steps: step3. set widthLayoutPolicy_ and heightLayoutPolicy_ to null. + * @tc.expected: step3. ret Width is equal to TEST_SIZE_200 Width. + */ + LayoutConstraintF contentConstraint; + contentConstraint.parentIdealSize.SetSize(TEST_SIZE_200); + auto layoutProperty = layoutWrapper.GetLayoutProperty(); + ASSERT_NE(layoutProperty, nullptr); + auto ret = sliderLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_NE(ret, TEST_SIZE_200); + + /** + * @tc.steps: step3. set widthLayoutPolicy_ and heightLayoutPolicy_ to FIX_AT_IDEAL_SIZE. + * @tc.expected: step3. ret Width is equal to TEST_SIZE_200 Width. + */ + LayoutPolicyProperty layoutPolicyProperty; + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::FIX_AT_IDEAL_SIZE; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + ret = sliderLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, BLOCK_SIZE_F_ZREO); + + /** + * @tc.steps: step3. set widthLayoutPolicy_ and heightLayoutPolicy_ to FIX_AT_IDEAL_SIZE. + * @tc.expected: step3. ret Width is equal to TEST_SIZE_200 Width. + */ + layoutPolicyProperty.widthLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutPolicyProperty.heightLayoutPolicy_ = LayoutCalPolicy::WRAP_CONTENT; + layoutProperty->layoutPolicy_ = layoutPolicyProperty; + ret = sliderLayoutAlgorithm->MeasureContent(contentConstraint, &layoutWrapper); + EXPECT_EQ(ret, BLOCK_SIZE_F_ZREO); +} + /** * @tc.name: ColorTypeToStringTest001 * @tc.desc: test ColorTypeToString.