1 /* 2 * Copyright (c) 2025 Huawei Device 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 "gtest/gtest.h" 17 18 #define private public 19 #define protected public 20 #include "interfaces/inner_api/ace_kit/src/view/frame_node_impl.h" 21 #include "test/mock/core/common/mock_container.h" 22 #include "test/mock/core/pipeline/mock_pipeline_context.h" 23 #include "test/unittest/interfaces/ace_kit/mock/mock_ace_kit_pattern.h" 24 #include "test/unittest/interfaces/ace_kit/mock/mock_ace_kit_property.h" 25 #include "ui/base/geometry/ng/size_t.h" 26 #include "ui/properties/dirty_flag.h" 27 #include "ui/view/frame_node.h" 28 #include "ui/view/layout/box_layout_algorithm.h" 29 #include "ui/view_factory/abstract_view_factory.h" 30 31 #include "core/components_ng/property/layout_constraint.h" 32 33 using namespace testing; 34 using namespace testing::ext; 35 using namespace OHOS::Ace::Kit; 36 namespace OHOS::Ace { 37 class BoxLayoutAlgorithmTest : public testing::Test { 38 public: SetUpTestSuite()39 static void SetUpTestSuite() 40 { 41 NG::MockPipelineContext::SetUp(); 42 MockContainer::SetUp(); 43 MockContainer::Current()->pipelineContext_ = PipelineBase::GetCurrentContext(); 44 } TearDownTestSuite()45 static void TearDownTestSuite() 46 { 47 MockContainer::Current()->pipelineContext_ = nullptr; 48 NG::MockPipelineContext::TearDown(); 49 } SetUp()50 void SetUp() {}; TearDown()51 void TearDown() {}; 52 }; 53 54 /** 55 * @tc.name: BoxLayoutAlgorithmTest001 56 * @tc.desc: 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(BoxLayoutAlgorithmTest, BoxLayoutAlgorithmTest001, TestSize.Level1) 60 { 61 const std::string tag = "BOX_TEST1"; 62 const int32_t id = 1; 63 auto mockPattern = AceType::MakeRefPtr<MockAceKitPattern>(); 64 auto frameNode = AbstractViewFactory::CreateFrameNode(tag, id, mockPattern); 65 EXPECT_NE(frameNode, nullptr); 66 auto frameNodeImpl = AceType::DynamicCast<FrameNodeImpl>(frameNode); 67 ASSERT_TRUE(frameNodeImpl); 68 auto* aceNode = frameNodeImpl->GetAceNodePtr(); 69 ASSERT_TRUE(aceNode); 70 71 NG::LayoutConstraintF layoutConstraint { .maxSize = NG::SizeF(100, 100), 72 .selfIdealSize = NG::OptionalSizeF(50, 50) }; 73 auto layoutProperty = aceNode->GetLayoutProperty(); 74 ASSERT_TRUE(layoutProperty); 75 layoutProperty->UpdateLayoutConstraint(layoutConstraint); 76 layoutProperty->UpdateContentConstraint(); 77 78 auto layoutAlgorithm = mockPattern->CreateLayoutAlgorithm(); 79 ASSERT_TRUE(layoutAlgorithm); 80 LayoutConstraintInfo constraint { .maxWidth = 100.0f, .maxHeight = 100.0f }; 81 layoutAlgorithm->Measure(constraint); 82 auto geometryNode = aceNode->GetGeometryNode(); 83 auto frameSize = geometryNode->GetFrameSize(); 84 EXPECT_TRUE(NearEqual(frameSize.Width(), layoutConstraint.selfIdealSize.Width().value())); 85 EXPECT_TRUE(NearEqual(frameSize.Height(), layoutConstraint.selfIdealSize.Height().value())); 86 } 87 88 /** 89 * @tc.name: BoxLayoutAlgorithmTest002 90 * @tc.desc: 91 * @tc.type: FUNC 92 */ 93 HWTEST_F(BoxLayoutAlgorithmTest, BoxLayoutAlgorithmTest002, TestSize.Level1) 94 { 95 const std::string tag = "BOX_TEST2"; 96 const int32_t id = 2; 97 auto mockPattern = AceType::MakeRefPtr<MockAceKitPattern>(); 98 auto frameNode = AbstractViewFactory::CreateFrameNode(tag, id, mockPattern); 99 EXPECT_NE(frameNode, nullptr); 100 auto frameNodeImpl = AceType::DynamicCast<FrameNodeImpl>(frameNode); 101 ASSERT_TRUE(frameNodeImpl); 102 auto* aceNode = frameNodeImpl->GetAceNodePtr(); 103 ASSERT_TRUE(aceNode); 104 105 NG::LayoutConstraintF layoutConstraint { .maxSize = NG::SizeF(100, 100), 106 .selfIdealSize = NG::OptionalSizeF(50, 50) }; 107 auto layoutProperty = aceNode->GetLayoutProperty(); 108 ASSERT_TRUE(layoutProperty); 109 layoutProperty->UpdateLayoutConstraint(layoutConstraint); 110 layoutProperty->UpdateContentConstraint(); 111 112 auto layoutAlgorithm = mockPattern->CreateLayoutAlgorithm(); 113 ASSERT_TRUE(layoutAlgorithm); 114 BoxLayoutAlgorithm::PerformMeasureSelf(nullptr); 115 BoxLayoutAlgorithm::PerformMeasureSelf(AceType::RawPtr(frameNode)); 116 117 auto geometryNode = aceNode->GetGeometryNode(); 118 auto frameSize = geometryNode->GetFrameSize(); 119 EXPECT_TRUE(NearEqual(frameSize.Width(), layoutConstraint.selfIdealSize.Width().value())); 120 EXPECT_TRUE(NearEqual(frameSize.Height(), layoutConstraint.selfIdealSize.Height().value())); 121 122 BoxLayoutAlgorithm::PerformLayout(nullptr); 123 BoxLayoutAlgorithm::PerformMeasureSelfWithChildList(nullptr); 124 } 125 } // namespace OHOS::Ace 126