diff --git a/frameworks/core/components_ng/pattern/grid/irregular/grid_irregular_filler.cpp b/frameworks/core/components_ng/pattern/grid/irregular/grid_irregular_filler.cpp index fb5d3cda23f515b5c0abadac296097c8844ea5d6..e214ba391f5671999d1ff535afe27b5354f57002 100644 --- a/frameworks/core/components_ng/pattern/grid/irregular/grid_irregular_filler.cpp +++ b/frameworks/core/components_ng/pattern/grid/irregular/grid_irregular_filler.cpp @@ -154,14 +154,15 @@ bool GridIrregularFiller::FindNextItem(int32_t target) } // to handle empty tiles in the middle of matrix, check next row auto nextRow = mat.find(posY_ + 1); - if (nextRow != mat.end()) { + while (nextRow != mat.end()) { for (const auto [col, item] : nextRow->second) { if (item == target) { - ++posY_; + posY_ = nextRow->first; posX_ = col; return true; } } + ++nextRow; } return false; } diff --git a/frameworks/core/components_ng/pattern/grid/irregular/grid_irregular_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/grid/irregular/grid_irregular_layout_algorithm.cpp index f571f2ed57733e6509f8f2c3650cb1d297205142..5a378bf0fc8db15afebe7b5fbdb75d559e0f030a 100644 --- a/frameworks/core/components_ng/pattern/grid/irregular/grid_irregular_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/grid/irregular/grid_irregular_layout_algorithm.cpp @@ -151,6 +151,14 @@ void GridIrregularLayoutAlgorithm::CheckForReset(int32_t lastCrossCount) return; } + if (wrapper_->GetLayoutProperty()->GetPropertyChangeFlag() & PROPERTY_UPDATE_BY_CHILD_REQUEST) { + postJumpOffset_ = info.currentOffset_; + info.lineHeightMap_.clear(); + PrepareJumpOnReset(info); + ResetLayoutRange(info); + return; + } + int32_t updateIdx = wrapper_->GetHostNode()->GetChildrenUpdated(); if (updateIdx != -1) { auto it = info.FindInMatrix(updateIdx); @@ -161,6 +169,7 @@ void GridIrregularLayoutAlgorithm::CheckForReset(int32_t lastCrossCount) PrepareJumpOnReset(info); ResetLayoutRange(info); } + wrapper_->GetHostNode()->ChildrenUpdatedFrom(-1); } } @@ -171,19 +180,33 @@ void GridIrregularLayoutAlgorithm::MeasureOnOffset(float mainSize) return; } + GridIrregularFiller filler(&gridLayoutInfo_, wrapper_); + GridIrregularFiller::FillParameters params { crossLens_, crossGap_, mainGap_ }; + + bool backward = info.currentOffset_ > 0.0f; + if (backward) { + filler.MeasureBackward(params, + info.currentOffset_ + info.lineHeightMap_.at(info.startMainLineIndex_) + mainGap_, + info.startMainLineIndex_); + } + GridLayoutRangeSolver solver(&info, wrapper_); auto res = solver.FindStartingRow(mainGap_); info.startMainLineIndex_ = res.row; info.currentOffset_ = res.pos; - // on init, gridMatrix_ is empty const auto row = info.gridMatrix_.find(res.row); info.startIndex_ = (row != info.gridMatrix_.end()) ? row->second.at(0) : 0; - GridIrregularFiller filler(&gridLayoutInfo_, wrapper_); - auto fillRes = filler.Fill({ crossLens_, crossGap_, mainGap_ }, mainSize - res.pos, info.startMainLineIndex_); - info.endMainLineIndex_ = fillRes.endMainLineIndex; - info.endIndex_ = fillRes.endIndex; + if (backward) { + auto [endLine, endIdx] = solver.SolveForwardForEndIdx(mainGap_, mainSize - res.pos, res.row); + info.endMainLineIndex_ = endLine; + info.endIndex_ = endIdx; + } else { + auto fillRes = filler.Fill(params, mainSize - res.pos, info.startMainLineIndex_); + info.endMainLineIndex_ = fillRes.endMainLineIndex; + info.endIndex_ = fillRes.endIndex; + } } bool GridIrregularLayoutAlgorithm::TrySkipping(float mainSize) diff --git a/frameworks/core/components_ng/pattern/grid/irregular/grid_layout_range_solver.cpp b/frameworks/core/components_ng/pattern/grid/irregular/grid_layout_range_solver.cpp index 5c8e615007417a8d179ece964ecf66b62b602ed6..74590fa26d1fe30c20f557cac96130d803240461 100644 --- a/frameworks/core/components_ng/pattern/grid/irregular/grid_layout_range_solver.cpp +++ b/frameworks/core/components_ng/pattern/grid/irregular/grid_layout_range_solver.cpp @@ -119,11 +119,16 @@ std::pair GridLayoutRangeSolver::AddNextRows(float mainGap, int3 std::pair GridLayoutRangeSolver::SolveForwardForEndIdx(float mainGap, float targetLen, int32_t line) { float len = 0.0f; - while (len < targetLen && line <= info_->lineHeightMap_.rbegin()->first) { - len += info_->lineHeightMap_.at(line++) + mainGap; + auto it = info_->lineHeightMap_.find(line); + if (it == info_->lineHeightMap_.end()) { + return { -1, -1 }; } - --line; - return { line, info_->FindEndIdx(line).itemIdx }; + + for (; len < targetLen && it != info_->lineHeightMap_.end(); ++it) { + len += it->second + mainGap; + } + --it; + return { it->first, info_->FindEndIdx(it->first).itemIdx }; } Result GridLayoutRangeSolver::SolveBackward(float mainGap, float targetLen, int32_t idx) diff --git a/frameworks/core/components_ng/pattern/grid/irregular/grid_layout_range_solver.h b/frameworks/core/components_ng/pattern/grid/irregular/grid_layout_range_solver.h index 3ddcb120b35c4b8213eec48b4f5ef1df5622fdd5..cb12fb5dde58a8641eeb6469a0a3fedadc273819 100644 --- a/frameworks/core/components_ng/pattern/grid/irregular/grid_layout_range_solver.h +++ b/frameworks/core/components_ng/pattern/grid/irregular/grid_layout_range_solver.h @@ -61,16 +61,6 @@ public: */ RangeInfo FindRangeOnJump(int32_t jumpLineIdx, float mainGap); -private: - /** - * @brief Find the starting row after offsetting by [targetLen] going forward (scrolling down). - * - * @param mainGap The gap length between rows. - * @param targetLen The target length to offset. - * @param idx The index of the current starting row - */ - StartingRowInfo SolveForward(float mainGap, float targetLen, int32_t idx); - /** * @brief Solves the forward end index for a given target length and starting line index. * @@ -81,6 +71,16 @@ private: */ std::pair SolveForwardForEndIdx(float mainGap, float targetLen, int32_t line); +private: + /** + * @brief Find the starting row after offsetting by [targetLen] going forward (scrolling down). + * + * @param mainGap The gap length between rows. + * @param targetLen The target length to offset. + * @param idx The index of the current starting row + */ + StartingRowInfo SolveForward(float mainGap, float targetLen, int32_t idx); + /** * @brief Adds the next rows to the layout in SolveForward. * diff --git a/test/unittest/core/pattern/grid/BUILD.gn b/test/unittest/core/pattern/grid/BUILD.gn index c1eea275a9e7f45437bd5886c36bfb3d505a91f5..59384fa1b5c772a2d17c290dcff4ce60f02676d1 100644 --- a/test/unittest/core/pattern/grid/BUILD.gn +++ b/test/unittest/core/pattern/grid/BUILD.gn @@ -21,11 +21,12 @@ ace_unittest("grid_test_ng") { "$ace_root/frameworks/bridge/js_frontend/engine/common/js_constants.cpp", "grid_attr_test_ng.cpp", "grid_common_test_ng.cpp", - "grid_irregular_layout_test.cpp", "grid_layout_test_ng.cpp", "grid_scroller_test_ng.cpp", "grid_test_ng.cpp", - "irregular_matrics.cpp", + "irregular/grid_irregular_filler_test.cpp", + "irregular/grid_irregular_layout_test.cpp", + "irregular/irregular_matrics.cpp", "layout_info_test.cpp", ] } diff --git a/test/unittest/core/pattern/grid/grid_layout_test_ng.cpp b/test/unittest/core/pattern/grid/grid_layout_test_ng.cpp index 22e8f742a50a33b9a9fd05df9c948915d0da5fdf..c1139cc68557a9d494e19edafae2c295452309b4 100644 --- a/test/unittest/core/pattern/grid/grid_layout_test_ng.cpp +++ b/test/unittest/core/pattern/grid/grid_layout_test_ng.cpp @@ -14,7 +14,6 @@ */ #include "grid_test_ng.h" -#include "irregular_matrices.h" #include "core/components_ng/pattern/grid/grid_layout/grid_layout_algorithm.h" #include "core/components_ng/pattern/grid/irregular/grid_layout_utils.h" diff --git a/test/unittest/core/pattern/grid/irregular/grid_irregular_filler_test.cpp b/test/unittest/core/pattern/grid/irregular/grid_irregular_filler_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7154e3c3f9c54db0d5eb3ce21c8259daf0a136a5 --- /dev/null +++ b/test/unittest/core/pattern/grid/irregular/grid_irregular_filler_test.cpp @@ -0,0 +1,608 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "irregular_matrices.h" +#include "test/unittest/core/pattern/grid/grid_test_ng.h" + +#include "core/components_ng/pattern/grid/irregular/grid_irregular_filler.h" + +namespace OHOS::Ace::NG { +class GridIrregularFillerTest : public GridTestNg {}; + +/** + * @tc.name: IrregularFiller::AdvancePos001 + * @tc.desc: Test IrregularFiller::AdvancePos + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, AdvancePos001, TestSize.Level1) +{ + // empty matrix + GridLayoutInfo info; + info.crossCount_ = 2; + GridIrregularFiller filler(&info, nullptr); + EXPECT_FALSE(filler.AdvancePos()); + + filler.posX_ = 1; + filler.posY_ = 0; + EXPECT_FALSE(filler.AdvancePos()); + EXPECT_EQ(filler.posX_, 0); + EXPECT_EQ(filler.posY_, 1); + + // init matrix + info.gridMatrix_[0][0] = 1; + info.gridMatrix_[0][1] = -1; + info.gridMatrix_[1][0] = -1; + EXPECT_FALSE(filler.AdvancePos()); + EXPECT_EQ(filler.posX_, 1); + EXPECT_EQ(filler.posY_, 1); + + // reset pos and make [1][1] available + filler.posX_ = 0; + filler.posY_ = 1; + info.gridMatrix_[1][1] = -1; + EXPECT_TRUE(filler.AdvancePos()); +} + +/** + * @tc.name: IrregularFiller::FindNextItem001 + * @tc.desc: Test IrregularFiller::FindNextItem + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, FindNextItem001, TestSize.Level1) +{ + // empty matrix + GridLayoutInfo info; + info.crossCount_ = 2; + { + GridIrregularFiller filler(&info, nullptr); + + EXPECT_FALSE(filler.FindNextItem(0)); + } + + info.gridMatrix_[0][0] = 1; + info.gridMatrix_[0][1] = 2; + info.gridMatrix_[1][0] = 3; + info.gridMatrix_[1][1] = -1; + { + GridIrregularFiller filler(&info, nullptr); + + EXPECT_TRUE(filler.FindNextItem(1)); + EXPECT_EQ(filler.posX_, 0); + EXPECT_EQ(filler.posY_, 0); + + EXPECT_TRUE(filler.FindNextItem(2)); + EXPECT_EQ(filler.posX_, 1); + EXPECT_EQ(filler.posY_, 0); + + EXPECT_TRUE(filler.FindNextItem(3)); + EXPECT_EQ(filler.posX_, 0); + EXPECT_EQ(filler.posY_, 1); + + EXPECT_FALSE(filler.FindNextItem(4)); + } + + info.gridMatrix_[0][1] = -1; + info.gridMatrix_[1][0] = 2; + info.gridMatrix_[1].erase(1); + { + GridIrregularFiller filler(&info, nullptr); + + EXPECT_TRUE(filler.FindNextItem(1)); + EXPECT_EQ(filler.posX_, 0); + EXPECT_EQ(filler.posY_, 0); + + EXPECT_TRUE(filler.FindNextItem(2)); + EXPECT_EQ(filler.posX_, 0); + EXPECT_EQ(filler.posY_, 1); + + EXPECT_FALSE(filler.FindNextItem(3)); + EXPECT_EQ(filler.posX_, 1); + EXPECT_EQ(filler.posY_, 1); + } +} + +/** + * @tc.name: IrregularFiller::UpdateLength001 + * @tc.desc: Test IrregularFiller::UpdateLength + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, UpdateLength001, TestSize.Level1) +{ + GridLayoutInfo info; + info.lineHeightMap_[0] = 50.0f; + info.lineHeightMap_[1] = 30.0f; + + GridIrregularFiller filler(&info, nullptr); + float len = -5.0f; + int32_t row = 0; + EXPECT_TRUE(filler.UpdateLength(len, 50.0f, row, 2, 5.0f)); + EXPECT_EQ(len, 50.0f); + + len = -5.0f; + row = 0; + EXPECT_FALSE(filler.UpdateLength(len, 100.0f, row, 2, 5.0f)); + EXPECT_EQ(len, 85.0f); + + info.lineHeightMap_[2] = 50.0f; + row = 2; + EXPECT_TRUE(filler.UpdateLength(len, 100.0f, row, 3, 10.0f)); + EXPECT_EQ(len, 85.0f + 50.0f + 10.0f); + + len = 85.0f; + row = 2; + EXPECT_FALSE(filler.UpdateLength(len, 200.0f, row, 3, 10.0f)); + EXPECT_EQ(len, 85.0f + 50.0f + 10.0f); +} + +/** + * @tc.name: IrregularFiller::FillOne001 + * @tc.desc: Test IrregularFiller::FillOne + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, FillOne001, TestSize.Level1) +{ + GridLayoutOptions option; + option.irregularIndexes = { + 0, // [2 x 1] + 1, // [1 x 2] + 2 // [2 x 1] + }; + auto onGetIrregularSizeByIndex = [](int32_t index) -> GridItemSize { + if (index == 1) { + return { .rows = 2, .columns = 1 }; + } + return { .rows = 1, .columns = 2 }; + }; + + option.getSizeByIndex = std::move(onGetIrregularSizeByIndex); + Create([option](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr"); + model.SetLayoutOptions(option); + }); + + GridLayoutInfo info; + info.crossCount_ = 2; + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + + filler.FillOne(0); + EXPECT_EQ(info.gridMatrix_.at(0).at(0), 0); + EXPECT_EQ(info.gridMatrix_.at(0).at(1), 0); + EXPECT_EQ(filler.posX_, 0); + EXPECT_EQ(filler.posY_, 0); + + filler.FillOne(1); + EXPECT_EQ(info.gridMatrix_.at(1).at(0), 1); + EXPECT_EQ(info.gridMatrix_.at(1).size(), 1); + EXPECT_EQ(info.gridMatrix_.at(2).at(0), -1); + EXPECT_EQ(info.gridMatrix_.at(2).size(), 1); + EXPECT_TRUE(info.gridMatrix_.find(3) == info.gridMatrix_.end()); + EXPECT_EQ(filler.posX_, 0); + EXPECT_EQ(filler.posY_, 1); + + filler.FillOne(2); + EXPECT_EQ(info.gridMatrix_.at(3).at(0), 2); + EXPECT_EQ(info.gridMatrix_.at(3).at(1), -2); + EXPECT_TRUE(info.gridMatrix_.find(4) == info.gridMatrix_.end()); + EXPECT_EQ(filler.posX_, 0); + EXPECT_EQ(filler.posY_, 3); +} + +/** + * @tc.name: IrregularFiller::FillOne002 + * @tc.desc: Test IrregularFiller::FillOne with 3 columns + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, FillOne002, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo10()); + }); + + GridLayoutInfo info; + info.crossCount_ = 3; + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + + for (int i = 0; i < 8; ++i) { + filler.FillOne(i); + } + + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_10); +} + +/** + * @tc.name: IrregularFiller::MeasureItem001 + * @tc.desc: Test IrregularFiller::MeasureItem + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, MeasureItem001, TestSize.Level1) +{ + GridLayoutOptions option; + option.irregularIndexes = { + 0, + }; + auto onGetIrregularSizeByIndex = [](int32_t index) -> GridItemSize { return { .columns = 1, .rows = 2 }; }; + option.getSizeByIndex = std::move(onGetIrregularSizeByIndex); + Create([option](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr 1fr"); + model.SetLayoutOptions(option); + CreateRowItem(10); + }); + + GridLayoutInfo info; + info.crossCount_ = 4; + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + + info.endIndex_ = 0; + + GridIrregularFiller::FillParameters params { + .crossLens = { 50.0f, 50.0f, 100.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f + }; + filler.MeasureItem(params, 0, 0, 0); + + EXPECT_TRUE(info.lineHeightMap_.find(0) != info.lineHeightMap_.end()); + EXPECT_TRUE(info.lineHeightMap_.find(1) != info.lineHeightMap_.end()); + auto child = frameNode_->GetChildByIndex(0); + ASSERT_TRUE(child); + auto constraint = *child->GetGeometryNode()->GetParentLayoutConstraint(); + EXPECT_EQ(constraint.maxSize.Width(), 50.0f); + EXPECT_EQ(*constraint.selfIdealSize.Width(), 50.0f); + EXPECT_EQ(constraint.percentReference.Width(), 50.0f); +} + +/** + * @tc.name: IrregularFiller::MeasureItem002 + * @tc.desc: Test IrregularFiller::MeasureItem + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, MeasureItem002, TestSize.Level1) +{ + GridLayoutOptions option; + option.irregularIndexes = { + 0, + }; + Create([option](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr 1fr"); + model.SetLayoutOptions(option); + CreateRowItem(10); + }); + + GridLayoutInfo info; + info.crossCount_ = 4; + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + + info.endIndex_ = 0; + filler.posY_ = 0; + + GridIrregularFiller::FillParameters params { + .crossLens = { 50.0f, 50.0f, 100.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f + }; + filler.MeasureItem(params, 0, 0, 0); + + EXPECT_TRUE(info.lineHeightMap_.find(0) != info.lineHeightMap_.end()); + EXPECT_TRUE(info.lineHeightMap_.find(1) == info.lineHeightMap_.end()); + auto child = frameNode_->GetChildByIndex(0); + ASSERT_TRUE(child); + auto constraint = *child->GetGeometryNode()->GetParentLayoutConstraint(); + EXPECT_EQ(constraint.maxSize.Width(), 315.0f); + EXPECT_EQ(*constraint.selfIdealSize.Width(), 315.0f); + EXPECT_EQ(constraint.percentReference.Width(), 315.0f); +} + +/** + * @tc.name: IrregularFiller::Fill001 + * @tc.desc: Test IrregularFiller::Fill + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, Fill001, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo9()); + CreateRowItem(10); + }); + + GridLayoutInfo info; + info.crossCount_ = 3; + info.childrenCount_ = 10; + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + + auto res = filler.Fill({ .crossLens = { 50.0f, 50.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 0); + + EXPECT_EQ(res.length, 6.0f); + + EXPECT_EQ(res.endIndex, 9); + EXPECT_EQ(res.endMainLineIndex, 6); + + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_9); +} + +/** + * @tc.name: IrregularFiller::Fill002 + * @tc.desc: Test IrregularFiller::Fill + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, Fill002, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo11()); + CreateRowItem(10); + }); + + GridLayoutInfo info; + info.crossCount_ = 3; + info.childrenCount_ = 10; + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + + auto res = filler.Fill({ .crossLens = { 50.0f, 50.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 0); + + EXPECT_EQ(res.length, 6.0f); + + EXPECT_EQ(res.endIndex, 9); + EXPECT_EQ(res.endMainLineIndex, 6); + + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_11); + + // call Fill on an already filled matrix + res = filler.Fill({ .crossLens = { 50.0f, 50.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 0); + + EXPECT_EQ(res.length, 6.0f); + + EXPECT_EQ(res.endIndex, 9); + EXPECT_EQ(res.endMainLineIndex, 6); + + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_11); +} + +/** + * @tc.name: IrregularFiller::Fill003 + * @tc.desc: Test IrregularFiller::Fill + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, Fill003, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo5()); + CreateFixedItem(11); + }); + + GridLayoutInfo info; + info.crossCount_ = 2; + info.childrenCount_ = 11; + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + + auto res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 600.0f, 0); + + EXPECT_EQ(res.length, 602.0f); + + EXPECT_EQ(res.endIndex, 3); + EXPECT_EQ(res.endMainLineIndex, 5); + + res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 2); + + EXPECT_EQ(res.length, 1004.0f); + + EXPECT_EQ(res.endIndex, 8); + EXPECT_EQ(res.endMainLineIndex, 10); + + res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 7); + + EXPECT_EQ(res.length, 803.0f); + + EXPECT_EQ(res.endIndex, 10); + EXPECT_EQ(res.endMainLineIndex, 11); + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_5); +} + +/** + * @tc.name: IrregularFiller::Fill004 + * @tc.desc: Test IrregularFiller::Fill + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, Fill004, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetLayoutOptions(GetOptionDemo2()); + CreateFixedItem(8); + model.SetColumnsTemplate("1fr 1fr 1fr"); + }); + + GridLayoutInfo info; + info.crossCount_ = 3; + info.childrenCount_ = 8; + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + + auto res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 300.0f, 0); + + EXPECT_EQ(res.length, 401.0f); + + EXPECT_EQ(res.endIndex, 3); + EXPECT_EQ(res.endMainLineIndex, 2); + + // call Fill on an already filled matrix + res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 500.0f, 2); + + EXPECT_EQ(res.length, 602.0f); + + EXPECT_EQ(res.endIndex, 7); + EXPECT_EQ(res.endMainLineIndex, 4); + + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_2); +} + +/** + * @tc.name: IrregularFiller::Fill005 + * @tc.desc: Test IrregularFiller::Fill + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, Fill005, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetLayoutOptions(GetOptionDemo10()); + CreateFixedItem(8); + model.SetColumnsTemplate("1fr 1fr 1fr"); + }); + + GridLayoutInfo info; + info.crossCount_ = 3; + info.childrenCount_ = 8; + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + + auto res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1500.0f, 0); + + EXPECT_EQ(res.length, 1104.5f); + + EXPECT_EQ(res.endIndex, 7); + EXPECT_EQ(res.endMainLineIndex, 5); + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_10); + + // call Fill on an already filled matrix + res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1500.0f, 3); + + EXPECT_EQ(res.length, 501.5f); + + EXPECT_EQ(res.endIndex, 7); + EXPECT_EQ(res.endMainLineIndex, 5); + + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_10); +} + +/** + * @tc.name: GridIrregularLayout::FillMatrixOnly001 + * @tc.desc: Test GridIrregularFiller::FillMatrixOnly + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, FillMatrixOnly001, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo8()); + CreateColItem(7); + }); + + GridLayoutInfo info; + info.crossCount_ = 3; + // partially filled + info.gridMatrix_ = { + { 0, { { 0, 0 }, { 1, 0 }, { 2, 1 } } }, // 0 | 0 | 1 + }; + info.childrenCount_ = 7; + + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + EXPECT_EQ(filler.FillMatrixOnly(6), 4); + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_8); +} + +/** + * @tc.name: GridIrregularLayout::MeasureBackward001 + * @tc.desc: Test GridIrregularFiller::MeasureBackward + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, MeasureBackward001, TestSize.Level1) +{ + GridLayoutInfo info; + info.gridMatrix_ = MATRIX_DEMO_8; + info.crossCount_ = 3; + + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo8()); + CreateRowItem(10); + }); + + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + float len = filler.MeasureBackward({ { 50.0f, 50.0f, 50.0f }, 5.0f, 5.0f }, 1000.0f, 5); + + EXPECT_EQ(len, 30.0f); + EXPECT_EQ(info.lineHeightMap_.size(), 6); +} + +/** + * @tc.name: GridIrregularFiller::FillMatrixByLine001 + * @tc.desc: Test GridIrregularFiller::FillMatrixByLine + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, FillMatrixByLine001, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo2()); + CreateFixedItem(8); + }); + + GridLayoutInfo info; + info.crossCount_ = 3; + info.childrenCount_ = 8; + info.gridMatrix_ = MATRIX_DEMO_2; + + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + int32_t idx = filler.FillMatrixByLine(0, 3); + EXPECT_EQ(idx, 4); + + idx = filler.FillMatrixByLine(3, 10); + EXPECT_EQ(idx, 7); + + info.gridMatrix_.clear(); + idx = filler.FillMatrixByLine(0, 3); + EXPECT_EQ(idx, 4); + idx = filler.FillMatrixByLine(2, 5); + EXPECT_EQ(idx, 7); + idx = filler.FillMatrixByLine(5, 5); + EXPECT_EQ(idx, 7); + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_2); + + info.gridMatrix_.clear(); + idx = filler.FillMatrixByLine(0, 10); + EXPECT_EQ(idx, 7); + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_2); +} + +/** + * @tc.name: GridIrregularFiller::FillMatrixByLine002 + * @tc.desc: Test GridIrregularFiller::FillMatrixByLine + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularFillerTest, FillMatrixByLine002, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo5()); + CreateFixedItem(11); + }); + + GridLayoutInfo info; + info.crossCount_ = 2; + info.childrenCount_ = 11; + info.gridMatrix_ = MATRIX_DEMO_5; + + GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); + int32_t idx = filler.FillMatrixByLine(2, 4); + EXPECT_EQ(idx, 4); + + idx = filler.FillMatrixByLine(3, 10); + EXPECT_EQ(idx, 9); + + idx = filler.FillMatrixByLine(4, 8); + EXPECT_EQ(idx, 7); + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_5); + + info.gridMatrix_.clear(); + idx = filler.FillMatrixByLine(5, 13); + EXPECT_EQ(idx, 10); + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_5); +} +} // namespace OHOS::Ace::NG \ No newline at end of file diff --git a/test/unittest/core/pattern/grid/grid_irregular_layout_test.cpp b/test/unittest/core/pattern/grid/irregular/grid_irregular_layout_test.cpp similarity index 74% rename from test/unittest/core/pattern/grid/grid_irregular_layout_test.cpp rename to test/unittest/core/pattern/grid/irregular/grid_irregular_layout_test.cpp index b90c44812b8d23dd92e9be3f3f2ccdf5626f458e..6ee1dea9893ffa8fa91ebc7f74f1ac25e5014510 100644 --- a/test/unittest/core/pattern/grid/grid_irregular_layout_test.cpp +++ b/test/unittest/core/pattern/grid/irregular/grid_irregular_layout_test.cpp @@ -13,441 +13,17 @@ * limitations under the License. */ -#include "grid_test_ng.h" #include "irregular_matrices.h" +#include "test/unittest/core/pattern/grid/grid_test_ng.h" #include "core/components/scroll/scroll_controller_base.h" #include "core/components_ng/pattern/grid/grid_layout_info.h" -#include "core/components_ng/pattern/grid/irregular/grid_irregular_filler.h" #include "core/components_ng/pattern/grid/irregular/grid_irregular_layout_algorithm.h" #include "core/components_ng/pattern/grid/irregular/grid_layout_range_solver.h" namespace OHOS::Ace::NG { class GridIrregularLayoutTest : public GridTestNg {}; -/** - * @tc.name: IrregularFiller::AdvancePos001 - * @tc.desc: Test IrregularFiller::AdvancePos - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, AdvancePos001, TestSize.Level1) -{ - // empty matrix - GridLayoutInfo info; - info.crossCount_ = 2; - GridIrregularFiller filler(&info, nullptr); - EXPECT_FALSE(filler.AdvancePos()); - - filler.posX_ = 1; - filler.posY_ = 0; - EXPECT_FALSE(filler.AdvancePos()); - EXPECT_EQ(filler.posX_, 0); - EXPECT_EQ(filler.posY_, 1); - - // init matrix - info.gridMatrix_[0][0] = 1; - info.gridMatrix_[0][1] = -1; - info.gridMatrix_[1][0] = -1; - EXPECT_FALSE(filler.AdvancePos()); - EXPECT_EQ(filler.posX_, 1); - EXPECT_EQ(filler.posY_, 1); - - // reset pos and make [1][1] available - filler.posX_ = 0; - filler.posY_ = 1; - info.gridMatrix_[1][1] = -1; - EXPECT_TRUE(filler.AdvancePos()); -} - -/** - * @tc.name: IrregularFiller::FindNextItem001 - * @tc.desc: Test IrregularFiller::FindNextItem - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, FindNextItem001, TestSize.Level1) -{ - // empty matrix - GridLayoutInfo info; - info.crossCount_ = 2; - { - GridIrregularFiller filler(&info, nullptr); - - EXPECT_FALSE(filler.FindNextItem(0)); - } - - info.gridMatrix_[0][0] = 1; - info.gridMatrix_[0][1] = 2; - info.gridMatrix_[1][0] = 3; - info.gridMatrix_[1][1] = -1; - { - GridIrregularFiller filler(&info, nullptr); - - EXPECT_TRUE(filler.FindNextItem(1)); - EXPECT_EQ(filler.posX_, 0); - EXPECT_EQ(filler.posY_, 0); - - EXPECT_TRUE(filler.FindNextItem(2)); - EXPECT_EQ(filler.posX_, 1); - EXPECT_EQ(filler.posY_, 0); - - EXPECT_TRUE(filler.FindNextItem(3)); - EXPECT_EQ(filler.posX_, 0); - EXPECT_EQ(filler.posY_, 1); - - EXPECT_FALSE(filler.FindNextItem(4)); - } - - info.gridMatrix_[0][1] = -1; - info.gridMatrix_[1][0] = 2; - info.gridMatrix_[1].erase(1); - { - GridIrregularFiller filler(&info, nullptr); - - EXPECT_TRUE(filler.FindNextItem(1)); - EXPECT_EQ(filler.posX_, 0); - EXPECT_EQ(filler.posY_, 0); - - EXPECT_TRUE(filler.FindNextItem(2)); - EXPECT_EQ(filler.posX_, 0); - EXPECT_EQ(filler.posY_, 1); - - EXPECT_FALSE(filler.FindNextItem(3)); - EXPECT_EQ(filler.posX_, 1); - EXPECT_EQ(filler.posY_, 1); - } -} - -/** - * @tc.name: IrregularFiller::UpdateLength001 - * @tc.desc: Test IrregularFiller::UpdateLength - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, UpdateLength001, TestSize.Level1) -{ - GridLayoutInfo info; - info.lineHeightMap_[0] = 50.0f; - info.lineHeightMap_[1] = 30.0f; - - GridIrregularFiller filler(&info, nullptr); - float len = -5.0f; - int32_t row = 0; - EXPECT_TRUE(filler.UpdateLength(len, 50.0f, row, 2, 5.0f)); - EXPECT_EQ(len, 50.0f); - - len = -5.0f; - row = 0; - EXPECT_FALSE(filler.UpdateLength(len, 100.0f, row, 2, 5.0f)); - EXPECT_EQ(len, 85.0f); - - info.lineHeightMap_[2] = 50.0f; - row = 2; - EXPECT_TRUE(filler.UpdateLength(len, 100.0f, row, 3, 10.0f)); - EXPECT_EQ(len, 85.0f + 50.0f + 10.0f); - - len = 85.0f; - row = 2; - EXPECT_FALSE(filler.UpdateLength(len, 200.0f, row, 3, 10.0f)); - EXPECT_EQ(len, 85.0f + 50.0f + 10.0f); -} - -/** - * @tc.name: IrregularFiller::FillOne001 - * @tc.desc: Test IrregularFiller::FillOne - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, FillOne001, TestSize.Level1) -{ - GridLayoutOptions option; - option.irregularIndexes = { - 0, // [2 x 1] - 1, // [1 x 2] - 2 // [2 x 1] - }; - auto onGetIrregularSizeByIndex = [](int32_t index) -> GridItemSize { - if (index == 1) { - return { .rows = 2, .columns = 1 }; - } - return { .rows = 1, .columns = 2 }; - }; - - option.getSizeByIndex = std::move(onGetIrregularSizeByIndex); - Create([option](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr"); - model.SetLayoutOptions(option); - }); - - GridLayoutInfo info; - info.crossCount_ = 2; - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - - filler.FillOne(0); - EXPECT_EQ(info.gridMatrix_.at(0).at(0), 0); - EXPECT_EQ(info.gridMatrix_.at(0).at(1), 0); - EXPECT_EQ(filler.posX_, 0); - EXPECT_EQ(filler.posY_, 0); - - filler.FillOne(1); - EXPECT_EQ(info.gridMatrix_.at(1).at(0), 1); - EXPECT_EQ(info.gridMatrix_.at(1).size(), 1); - EXPECT_EQ(info.gridMatrix_.at(2).at(0), -1); - EXPECT_EQ(info.gridMatrix_.at(2).size(), 1); - EXPECT_TRUE(info.gridMatrix_.find(3) == info.gridMatrix_.end()); - EXPECT_EQ(filler.posX_, 0); - EXPECT_EQ(filler.posY_, 1); - - filler.FillOne(2); - EXPECT_EQ(info.gridMatrix_.at(3).at(0), 2); - EXPECT_EQ(info.gridMatrix_.at(3).at(1), -2); - EXPECT_TRUE(info.gridMatrix_.find(4) == info.gridMatrix_.end()); - EXPECT_EQ(filler.posX_, 0); - EXPECT_EQ(filler.posY_, 3); -} - -/** - * @tc.name: IrregularFiller::FillOne002 - * @tc.desc: Test IrregularFiller::FillOne with 3 columns - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, FillOne002, TestSize.Level1) -{ - Create([](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr 1fr"); - model.SetLayoutOptions(GetOptionDemo10()); - }); - - GridLayoutInfo info; - info.crossCount_ = 3; - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - - for (int i = 0; i < 8; ++i) { - filler.FillOne(i); - } - - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_10); -} - -/** - * @tc.name: IrregularFiller::MeasureItem001 - * @tc.desc: Test IrregularFiller::MeasureItem - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, MeasureItem001, TestSize.Level1) -{ - GridLayoutOptions option; - option.irregularIndexes = { - 0, - }; - auto onGetIrregularSizeByIndex = [](int32_t index) -> GridItemSize { return { .columns = 1, .rows = 2 }; }; - option.getSizeByIndex = std::move(onGetIrregularSizeByIndex); - Create([option](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr 1fr 1fr"); - model.SetLayoutOptions(option); - CreateRowItem(10); - }); - - GridLayoutInfo info; - info.crossCount_ = 4; - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - - info.endIndex_ = 0; - - GridIrregularFiller::FillParameters params { - .crossLens = { 50.0f, 50.0f, 100.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f - }; - filler.MeasureItem(params, 0, 0, 0); - - EXPECT_TRUE(info.lineHeightMap_.find(0) != info.lineHeightMap_.end()); - EXPECT_TRUE(info.lineHeightMap_.find(1) != info.lineHeightMap_.end()); - auto child = frameNode_->GetChildByIndex(0); - ASSERT_TRUE(child); - auto constraint = *child->GetGeometryNode()->GetParentLayoutConstraint(); - EXPECT_EQ(constraint.maxSize.Width(), 50.0f); - EXPECT_EQ(*constraint.selfIdealSize.Width(), 50.0f); - EXPECT_EQ(constraint.percentReference.Width(), 50.0f); -} - -/** - * @tc.name: IrregularFiller::MeasureItem002 - * @tc.desc: Test IrregularFiller::MeasureItem - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, MeasureItem002, TestSize.Level1) -{ - GridLayoutOptions option; - option.irregularIndexes = { - 0, - }; - Create([option](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr 1fr 1fr"); - model.SetLayoutOptions(option); - CreateRowItem(10); - }); - - GridLayoutInfo info; - info.crossCount_ = 4; - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - - info.endIndex_ = 0; - filler.posY_ = 0; - - GridIrregularFiller::FillParameters params { - .crossLens = { 50.0f, 50.0f, 100.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f - }; - filler.MeasureItem(params, 0, 0, 0); - - EXPECT_TRUE(info.lineHeightMap_.find(0) != info.lineHeightMap_.end()); - EXPECT_TRUE(info.lineHeightMap_.find(1) == info.lineHeightMap_.end()); - auto child = frameNode_->GetChildByIndex(0); - ASSERT_TRUE(child); - auto constraint = *child->GetGeometryNode()->GetParentLayoutConstraint(); - EXPECT_EQ(constraint.maxSize.Width(), 315.0f); - EXPECT_EQ(*constraint.selfIdealSize.Width(), 315.0f); - EXPECT_EQ(constraint.percentReference.Width(), 315.0f); -} - -/** - * @tc.name: IrregularFiller::Fill001 - * @tc.desc: Test IrregularFiller::Fill - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, Fill001, TestSize.Level1) -{ - Create([](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr 1fr"); - model.SetLayoutOptions(GetOptionDemo9()); - CreateRowItem(10); - }); - - GridLayoutInfo info; - info.crossCount_ = 3; - info.childrenCount_ = 10; - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - - auto res = filler.Fill({ .crossLens = { 50.0f, 50.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 0); - - EXPECT_EQ(res.length, 6.0f); - - EXPECT_EQ(res.endIndex, 9); - EXPECT_EQ(res.endMainLineIndex, 6); - - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_9); -} - -/** - * @tc.name: IrregularFiller::Fill002 - * @tc.desc: Test IrregularFiller::Fill - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, Fill002, TestSize.Level1) -{ - Create([](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr 1fr"); - model.SetLayoutOptions(GetOptionDemo11()); - CreateRowItem(10); - }); - - GridLayoutInfo info; - info.crossCount_ = 3; - info.childrenCount_ = 10; - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - - auto res = filler.Fill({ .crossLens = { 50.0f, 50.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 0); - - EXPECT_EQ(res.length, 6.0f); - - EXPECT_EQ(res.endIndex, 9); - EXPECT_EQ(res.endMainLineIndex, 6); - - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_11); - - // call Fill on an already filled matrix - res = filler.Fill({ .crossLens = { 50.0f, 50.0f, 100.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 0); - - EXPECT_EQ(res.length, 6.0f); - - EXPECT_EQ(res.endIndex, 9); - EXPECT_EQ(res.endMainLineIndex, 6); - - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_11); -} - -/** - * @tc.name: IrregularFiller::Fill003 - * @tc.desc: Test IrregularFiller::Fill - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, Fill003, TestSize.Level1) -{ - Create([](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr"); - model.SetLayoutOptions(GetOptionDemo5()); - CreateFixedItem(11); - }); - - GridLayoutInfo info; - info.crossCount_ = 2; - info.childrenCount_ = 11; - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - - auto res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 600.0f, 0); - - EXPECT_EQ(res.length, 602.0f); - - EXPECT_EQ(res.endIndex, 3); - EXPECT_EQ(res.endMainLineIndex, 5); - - res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 2); - - EXPECT_EQ(res.length, 1004.0f); - - EXPECT_EQ(res.endIndex, 8); - EXPECT_EQ(res.endMainLineIndex, 10); - - res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 1000.0f, 7); - - EXPECT_EQ(res.length, 803.0f); - - EXPECT_EQ(res.endIndex, 10); - EXPECT_EQ(res.endMainLineIndex, 11); - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_5); -} - -/** - * @tc.name: IrregularFiller::Fill004 - * @tc.desc: Test IrregularFiller::Fill - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, Fill004, TestSize.Level1) -{ - Create([](GridModelNG model) { - model.SetLayoutOptions(GetOptionDemo2()); - CreateFixedItem(8); - model.SetColumnsTemplate("1fr 1fr 1fr"); - }); - - GridLayoutInfo info; - info.crossCount_ = 3; - info.childrenCount_ = 8; - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - - auto res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 300.0f, 0); - - EXPECT_EQ(res.length, 401.0f); - - EXPECT_EQ(res.endIndex, 3); - EXPECT_EQ(res.endMainLineIndex, 2); - - // call Fill on an already filled matrix - res = filler.Fill({ .crossLens = { 50.0f, 50.0f }, .crossGap = 5.0f, .mainGap = 1.0f }, 500.0f, 2); - - EXPECT_EQ(res.length, 602.0f); - - EXPECT_EQ(res.endIndex, 7); - EXPECT_EQ(res.endMainLineIndex, 4); - - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_2); -} - /** * @tc.name: LayoutRangeSolver::AddNextRow001 * @tc.desc: Test LayoutRangeSolver::AddNextRow @@ -1252,6 +828,67 @@ HWTEST_F(GridIrregularLayoutTest, TestReset003, TestSize.Level1) EXPECT_EQ(info.gridMatrix_, cmp); } +/** + * @tc.name: GridIrregularLayout::TestReset004 + * @tc.desc: Test measuring by child request (child update) + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularLayoutTest, TestReset004, TestSize.Level1) +{ + GridLayoutInfo oldInfo; + oldInfo.crossCount_ = 3; + oldInfo.childrenCount_ = 8; + oldInfo.gridMatrix_ = MATRIX_DEMO_10; + oldInfo.currentOffset_ = -1.0f; + oldInfo.startMainLineIndex_ = 1; + oldInfo.startIndex_ = 2; + oldInfo.endMainLineIndex_ = 5; + oldInfo.endIndex_ = 7; + oldInfo.lineHeightMap_ = { + { 0, 50.0f }, + { 1, 50.0f }, + { 2, 50.0f }, + { 3, 50.0f }, + { 4, 50.0f }, + { 5, 50.0f }, + { 6, 50.0f }, + { 7, 50.0f }, + }; + // changing cross count, should jump to the current startIndex + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo10()); + model.SetColumnsGap(Dimension { 5.0f }); + model.SetRowsGap(Dimension { 1.0f }); + CreateFixedItem(8); + }); + LayoutConstraintF constraint { .maxSize = { 610.0f, 600.0f }, .percentReference = { 610.0f, 600.0f } }; + layoutProperty_->layoutConstraint_ = constraint; + + auto algo = AceType::MakeRefPtr(oldInfo); + algo->wrapper_ = AceType::RawPtr(frameNode_); + frameNode_->GetLayoutProperty()->propertyChangeFlag_ = PROPERTY_UPDATE_BY_CHILD_REQUEST; + + algo->Measure(AceType::RawPtr(frameNode_)); + + // item height updated to 200.0f + auto& info = algo->gridLayoutInfo_; + EXPECT_EQ(info.childrenCount_, 8); + EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_10); + EXPECT_EQ(info.startIndex_, 2); + EXPECT_EQ(info.endIndex_, 6); + EXPECT_EQ(info.startMainLineIndex_, 1); + EXPECT_EQ(info.endMainLineIndex_, 3); + EXPECT_EQ(info.currentOffset_, -1.0f); + + // offset upwards after reset + info.currentOffset_ = 50.0f; + algo->Measure(AceType::RawPtr(frameNode_)); + EXPECT_EQ(info.startMainLineIndex_, 0); + EXPECT_EQ(info.startIndex_, 0); + EXPECT_EQ(info.currentOffset_, -151.0f); +} + /** * @tc.name: GridIrregularLayout::Layout001 * @tc.desc: Test GridIrregularLayout::Layout @@ -1289,57 +926,6 @@ HWTEST_F(GridIrregularLayoutTest, Layout001, TestSize.Level1) EXPECT_TRUE(info.reachEnd_); EXPECT_TRUE(info.offsetEnd_); } - -/** - * @tc.name: GridIrregularLayout::FillMatrixOnly001 - * @tc.desc: Test GridIrregularFiller::FillMatrixOnly - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, FillMatrixOnly001, TestSize.Level1) -{ - Create([](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr 1fr"); - model.SetLayoutOptions(GetOptionDemo8()); - CreateColItem(7); - }); - - GridLayoutInfo info; - info.crossCount_ = 3; - // partially filled - info.gridMatrix_ = { - { 0, { { 0, 0 }, { 1, 0 }, { 2, 1 } } }, // 0 | 0 | 1 - }; - info.childrenCount_ = 7; - - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - EXPECT_EQ(filler.FillMatrixOnly(6), 4); - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_8); -} - -/** - * @tc.name: GridIrregularLayout::MeasureBackward001 - * @tc.desc: Test GridIrregularFiller::MeasureBackward - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, MeasureBackward001, TestSize.Level1) -{ - GridLayoutInfo info; - info.gridMatrix_ = MATRIX_DEMO_8; - info.crossCount_ = 3; - - Create([](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr 1fr"); - model.SetLayoutOptions(GetOptionDemo8()); - CreateRowItem(10); - }); - - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - float len = filler.MeasureBackward({ { 50.0f, 50.0f, 50.0f }, 5.0f, 5.0f }, 1000.0f, 5); - - EXPECT_EQ(len, 30.0f); - EXPECT_EQ(info.lineHeightMap_.size(), 6); -} - /** * @tc.name: GridIrregularLayout::FindJumpLineIndex001 * @tc.desc: Test GridLayoutRangeFinder::FindJumpLineIndex @@ -1733,81 +1319,6 @@ HWTEST_F(GridIrregularLayoutTest, TrySkipping001, TestSize.Level1) EXPECT_EQ(info.scrollAlign_, ScrollAlign::START); } -/** - * @tc.name: GridIrregularFiller::FillMatrixByLine001 - * @tc.desc: Test GridIrregularFiller::FillMatrixByLine - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, FillMatrixByLine001, TestSize.Level1) -{ - Create([](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr 1fr"); - model.SetLayoutOptions(GetOptionDemo2()); - CreateFixedItem(8); - }); - - GridLayoutInfo info; - info.crossCount_ = 3; - info.childrenCount_ = 8; - info.gridMatrix_ = MATRIX_DEMO_2; - - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - int32_t idx = filler.FillMatrixByLine(0, 3); - EXPECT_EQ(idx, 4); - - idx = filler.FillMatrixByLine(3, 10); - EXPECT_EQ(idx, 7); - - info.gridMatrix_.clear(); - idx = filler.FillMatrixByLine(0, 3); - EXPECT_EQ(idx, 4); - idx = filler.FillMatrixByLine(2, 5); - EXPECT_EQ(idx, 7); - idx = filler.FillMatrixByLine(5, 5); - EXPECT_EQ(idx, 7); - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_2); - - info.gridMatrix_.clear(); - idx = filler.FillMatrixByLine(0, 10); - EXPECT_EQ(idx, 7); - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_2); -} - -/** - * @tc.name: GridIrregularFiller::FillMatrixByLine002 - * @tc.desc: Test GridIrregularFiller::FillMatrixByLine - * @tc.type: FUNC - */ -HWTEST_F(GridIrregularLayoutTest, FillMatrixByLine002, TestSize.Level1) -{ - Create([](GridModelNG model) { - model.SetColumnsTemplate("1fr 1fr"); - model.SetLayoutOptions(GetOptionDemo5()); - CreateFixedItem(11); - }); - - GridLayoutInfo info; - info.crossCount_ = 2; - info.childrenCount_ = 11; - info.gridMatrix_ = MATRIX_DEMO_5; - - GridIrregularFiller filler(&info, AceType::RawPtr(frameNode_)); - int32_t idx = filler.FillMatrixByLine(2, 4); - EXPECT_EQ(idx, 4); - - idx = filler.FillMatrixByLine(3, 10); - EXPECT_EQ(idx, 9); - - idx = filler.FillMatrixByLine(4, 8); - EXPECT_EQ(idx, 7); - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_5); - - info.gridMatrix_.clear(); - idx = filler.FillMatrixByLine(5, 13); - EXPECT_EQ(idx, 10); - EXPECT_EQ(info.gridMatrix_, MATRIX_DEMO_5); -} - /** * @tc.name: GridIrregularLayout::TransformAutoScrollAlign001 * @tc.desc: Test IrregularLayout::TransformAutoScrollAlign diff --git a/test/unittest/core/pattern/grid/irregular_matrices.h b/test/unittest/core/pattern/grid/irregular/irregular_matrices.h similarity index 100% rename from test/unittest/core/pattern/grid/irregular_matrices.h rename to test/unittest/core/pattern/grid/irregular/irregular_matrices.h diff --git a/test/unittest/core/pattern/grid/irregular_matrics.cpp b/test/unittest/core/pattern/grid/irregular/irregular_matrics.cpp similarity index 100% rename from test/unittest/core/pattern/grid/irregular_matrics.cpp rename to test/unittest/core/pattern/grid/irregular/irregular_matrics.cpp diff --git a/test/unittest/core/pattern/grid/layout_info_test.cpp b/test/unittest/core/pattern/grid/layout_info_test.cpp index 9f7dc37fb6505e403f997114b3fa2af6f8cea453..92bde658d4ecdf777a7397b86f2b16cbd7b61bcb 100644 --- a/test/unittest/core/pattern/grid/layout_info_test.cpp +++ b/test/unittest/core/pattern/grid/layout_info_test.cpp @@ -15,7 +15,7 @@ #include "gtest/gtest.h" #define private public -#include "irregular_matrices.h" +#include "test/unittest/core/pattern/grid/irregular/irregular_matrices.h" using namespace testing; using namespace testing::ext;