1 /* 2 * Copyright (c) 2025 iSoftStone Information Technology (Group) Co.,Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <optional> 17 18 #include "gtest/gtest.h" 19 20 #define protected public 21 #define private public 22 #include "test/mock/core/pipeline/mock_pipeline_context.h" 23 24 #include "base/memory/ace_type.h" 25 #include "core/components/common/layout/constants.h" 26 #include "core/components_ng/base/frame_node.h" 27 #include "core/components_ng/base/ui_node.h" 28 #include "core/components_ng/layout/drawing_layout_utils.h" 29 #include "core/components_ng/layout/layout_wrapper.h" 30 #include "core/components_ng/layout/layout_wrapper_builder.h" 31 #include "core/components_ng/layout/layout_wrapper_node.h" 32 #include "core/components_ng/pattern/custom/custom_node.h" 33 #include "core/components_ng/property/layout_constraint.h" 34 35 36 #undef private 37 #undef protected 38 39 using namespace testing; 40 using namespace testing::ext; 41 42 namespace OHOS::Ace::NG { 43 class DrawingLayoutUtilsTestNg : public testing::Test { 44 public: SetUpTestSuite()45 static void SetUpTestSuite() 46 { 47 MockPipelineContext::SetUp(); 48 } TearDownTestSuite()49 static void TearDownTestSuite() 50 { 51 MockPipelineContext::TearDown(); 52 } 53 }; 54 55 /** 56 * @tc.name: MeasureLayoutPolicySizeTest001 57 * @tc.desc: Test MeasureLayoutPolicySize 58 * @tc.type: FUNC 59 */ 60 HWTEST_F(DrawingLayoutUtilsTestNg, MeasureLayoutPolicySizeTest001, TestSize.Level1) 61 { 62 auto node = FrameNode::CreateFrameNode(V2::ROOT_ETS_TAG, 0, AceType::MakeRefPtr<Pattern>()); 63 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>(); 64 RefPtr<LayoutProperty> layoutProperty = node->GetLayoutProperty(); 65 LayoutWrapperNode layoutWrapper = LayoutWrapperNode(node, geometryNode, layoutProperty); 66 LayoutConstraintF layoutConstraint; 67 68 /** 69 * @tc.steps1: Width is matchParent 70 * @tc.expected: the return size of MeasureLayoutPolicySize is (300, 1000) 71 */ 72 SizeF size = SizeF(1000.0f, 1000.0f); 73 layoutConstraint.parentIdealSize = OptionalSizeF(300.0f, 400.0f); 74 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::MATCH_PARENT, true); 75 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, false); 76 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 77 EXPECT_EQ(size.Width(), 300); 78 EXPECT_EQ(size.Height(), 1000); 79 80 /** 81 * @tc.steps2: Height is matchParent 82 * @tc.expected: the return size of MeasureLayoutPolicySize is (1000, 400) 83 */ 84 size = SizeF(1000.0f, 1000.0f); 85 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, true); 86 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::MATCH_PARENT, false); 87 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 88 EXPECT_EQ(size.Width(), 1000); 89 EXPECT_EQ(size.Height(), 400); 90 91 /** 92 * @tc.steps3: Width and Height are NO_MATCH 93 * @tc.expected: the return size of MeasureLayoutPolicySize is (1000, 1000) 94 */ 95 size = SizeF(1000.0f, 1000.0f); 96 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, true); 97 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, false); 98 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 99 EXPECT_EQ(size.Width(), 1000); 100 EXPECT_EQ(size.Height(), 1000); 101 102 /** 103 * @tc.steps4: layoutPolicy has no value 104 * @tc.expected: the return size of MeasureLayoutPolicySize is (1000, 1000) 105 */ 106 layoutProperty->layoutPolicy_ = std::nullopt; 107 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 108 EXPECT_EQ(size.Width(), 1000); 109 EXPECT_EQ(size.Height(), 1000); 110 111 /** 112 * @tc.steps5: layoutProperty is nullptr 113 * @tc.expected: the return size of MeasureLayoutPolicySize is (1000, 1000) 114 */ 115 layoutProperty.Reset(); 116 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 117 EXPECT_EQ(size.Width(), 1000); 118 EXPECT_EQ(size.Height(), 1000); 119 } 120 121 /** 122 * @tc.name: MeasureLayoutPolicySizeTest002 123 * @tc.desc: Test MeasureLayoutPolicySize 124 * @tc.type: FUNC 125 */ 126 HWTEST_F(DrawingLayoutUtilsTestNg, MeasureLayoutPolicySizeTest002, TestSize.Level1) 127 { 128 auto node = FrameNode::CreateFrameNode(V2::ROOT_ETS_TAG, 0, AceType::MakeRefPtr<Pattern>()); 129 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>(); 130 RefPtr<LayoutProperty> layoutProperty = node->GetLayoutProperty(); 131 LayoutWrapperNode layoutWrapper = LayoutWrapperNode(node, geometryNode, layoutProperty); 132 LayoutConstraintF layoutConstraint; 133 134 /** 135 * @tc.steps1: Width is WrapContent 136 * @tc.expected: the return size of MeasureLayoutPolicySize is (0, 1000) 137 */ 138 SizeF size = SizeF(1000.0f, 1000.0f); 139 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::WRAP_CONTENT, true); 140 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, false); 141 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 142 EXPECT_EQ(size.Width(), 0); 143 EXPECT_EQ(size.Height(), 1000); 144 145 /** 146 * @tc.steps2: Height is WrapContent 147 * @tc.expected: the return size of MeasureLayoutPolicySize is (1000, 0) 148 */ 149 size = SizeF(1000.0f, 1000.0f); 150 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, true); 151 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::WRAP_CONTENT, false); 152 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 153 EXPECT_EQ(size.Width(), 1000); 154 EXPECT_EQ(size.Height(), 0); 155 156 /** 157 * @tc.steps3: Width is fixIdealSize 158 * @tc.expected: the return size of MeasureLayoutPolicySize is (0, 1000) 159 */ 160 size = SizeF(1000.0f, 1000.0f); 161 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::FIX_AT_IDEAL_SIZE, true); 162 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, false); 163 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 164 EXPECT_EQ(size.Width(), 0); 165 EXPECT_EQ(size.Height(), 1000); 166 167 /** 168 * @tc.steps4: Height is fixIdealSize 169 * @tc.expected: the return size of MeasureLayoutPolicySize is (1000, 0) 170 */ 171 size = SizeF(1000.0f, 1000.0f); 172 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, true); 173 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::FIX_AT_IDEAL_SIZE, false); 174 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 175 EXPECT_EQ(size.Width(), 1000); 176 EXPECT_EQ(size.Height(), 0); 177 178 /** 179 * @tc.steps5: Width and Height are matchParent and parentIdealSize is null 180 * @tc.expected: the return size of MeasureLayoutPolicySize is (1000, 1000) 181 */ 182 size = SizeF(1000.0f, 1000.0f); 183 layoutConstraint.parentIdealSize.Reset(); 184 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::MATCH_PARENT, true); 185 layoutProperty->UpdateLayoutPolicyProperty(LayoutCalPolicy::MATCH_PARENT, false); 186 MeasureLayoutPolicySize(layoutConstraint, &layoutWrapper, size); 187 EXPECT_EQ(size.Width(), 1000); 188 EXPECT_EQ(size.Height(), 1000); 189 } 190 } // namespace OHOS::Ace::NG 191