From 96a65a1c2b5243bfd467528fb67e6ad65eb5a616 Mon Sep 17 00:00:00 2001 From: Tianer Zhou Date: Thu, 9 May 2024 20:35:24 +0800 Subject: [PATCH] fix animateTo crash Signed-off-by: Tianer Zhou Change-Id: 51b0d21c4a63ad3c3e09b4e4d4f8f6511b68a9c5 --- .../pattern/grid/grid_pattern.cpp | 2 +- .../irregular/grid_irregular_layout_test.cpp | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/frameworks/core/components_ng/pattern/grid/grid_pattern.cpp b/frameworks/core/components_ng/pattern/grid/grid_pattern.cpp index fb33bdef5cb..9d08b3ed036 100644 --- a/frameworks/core/components_ng/pattern/grid/grid_pattern.cpp +++ b/frameworks/core/components_ng/pattern/grid/grid_pattern.cpp @@ -1907,7 +1907,7 @@ float GridPattern::IrregularAnimateToCenter(float mainGap) const } auto size = GridLayoutUtils::GetItemSize(&info, RawPtr(host), *targetIndex_); auto [center, offset] = info.FindItemCenter(it->first, size.rows, mainGap); - return info.GetHeightInRange(0, center, mainGap) + offset - info.lastMainSize_ / 2.0f; + return info.GetTotalHeightFromZeroIndex(center, mainGap) + offset - info.lastMainSize_ / 2.0f; } std::vector> GridPattern::GetVisibleSelectedItems() diff --git a/test/unittest/core/pattern/grid/irregular/grid_irregular_layout_test.cpp b/test/unittest/core/pattern/grid/irregular/grid_irregular_layout_test.cpp index e8cdbae1d54..465ec501e55 100644 --- a/test/unittest/core/pattern/grid/irregular/grid_irregular_layout_test.cpp +++ b/test/unittest/core/pattern/grid/irregular/grid_irregular_layout_test.cpp @@ -1480,6 +1480,37 @@ HWTEST_F(GridIrregularLayoutTest, TemplateChange002, TestSize.Level1) EXPECT_EQ(info.GetIrregularHeight(5.0f), 9 * 300.0f + 8 * 5.0f); } +/** + * @tc.name: GridIrregularLayout::TemplateChange003 + * @tc.desc: Test changing template + * @tc.type: FUNC + */ +HWTEST_F(GridIrregularLayoutTest, TemplateChange003, TestSize.Level1) +{ + Create([](GridModelNG model) { + model.SetColumnsTemplate("1fr 1fr 1fr 1fr 1fr"); + model.SetLayoutOptions(GetOptionDemo13()); + model.SetColumnsGap(Dimension { 1.0f }); + CreateFixedHeightItems(1, 900.0f); + CreateFixedHeightItems(1, 300.0f); + CreateFixedHeightItems(1, 600.0f); + CreateFixedHeightItems(1, 1800.0f); + CreateFixedHeightItems(5, 300.0f); + }); + pattern_->ScrollToIndex(3, false, ScrollAlign::CENTER); + FlushLayoutTask(frameNode_); + EXPECT_EQ(GetChildY(frameNode_, 3), -500.0f); + + layoutProperty_->UpdateColumnsTemplate("1fr 1fr 1fr 1fr 1fr 1fr"); + frameNode_->MarkDirtyNode(PROPERTY_UPDATE_MEASURE); + FlushLayoutTask(frameNode_); + pattern_->ScrollToIndex(3, true, ScrollAlign::CENTER); + FlushLayoutTask(frameNode_); + pattern_->finalPosition_ = Infinity(); + // finalPosition shouldn't be set because targetPos = current pos + EXPECT_EQ(pattern_->finalPosition_, Infinity()); +} + /** * @tc.name: DeleteItem001 * @tc.desc: Test removing item from end -- Gitee