From 4e0a1268c8ad049889cd6eaf318911767b1c3dfb Mon Sep 17 00:00:00 2001 From: Tianer Zhou Date: Wed, 13 Nov 2024 17:25:21 +0800 Subject: [PATCH] add lazyForEach test Signed-off-by: Tianer Zhou Change-Id: I2bbf0aaf3cafc14398ee38e4865809fcd8dc6f89 --- .gitignore | 2 + .../waterflow/water_flow_regular_test.cpp | 33 ++++++++++- .../pattern/waterflow/water_flow_test_ng.cpp | 57 ++++++++++++------- .../pattern/waterflow/water_flow_test_ng.h | 2 +- 4 files changed, 71 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index e17e90479fc..e5cdc625e23 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ lite/ui/ .clangd __pycache__/ output/ +# placeholder for generated file +frameworks/core/components/theme/theme_constants_defines.h diff --git a/test/unittest/core/pattern/waterflow/water_flow_regular_test.cpp b/test/unittest/core/pattern/waterflow/water_flow_regular_test.cpp index 5b713a3f2ab..305f4eb3fb4 100644 --- a/test/unittest/core/pattern/waterflow/water_flow_regular_test.cpp +++ b/test/unittest/core/pattern/waterflow/water_flow_regular_test.cpp @@ -15,7 +15,6 @@ #include "water_flow_test_ng.h" -#include "core/components_ng/property/property.h" #include "core/components_ng/syntax/if_else_node.h" #define protected public @@ -422,6 +421,38 @@ HWTEST_F(WaterFlowTestNg, Cache003, TestSize.Level1) EXPECT_EQ(GetChildY(frameNode_, 34), -250.0f); } +/** + * @tc.name: CacheScroll001 + * @tc.desc: Layout WaterFlow cache items + * @tc.type: FUNC + */ +HWTEST_F(WaterFlowTestNg, CacheScroll001, TestSize.Level1) +{ + auto model = CreateWaterFlow(); + model.SetCachedCount(10); + model.SetRowsGap(Dimension(10)); + model.SetColumnsGap(Dimension(10)); + CreateItemsInLazyForEach(100, [](int32_t) { return 100.0f; }); + CreateDone(); + + UpdateCurrentOffset(-2000.0f); + EXPECT_EQ(pattern_->layoutInfo_->startIndex_, 18); + EXPECT_EQ(pattern_->layoutInfo_->endIndex_, 25); + EXPECT_EQ(GetChildY(frameNode_, 18), -20.0f); + PipelineContext::GetCurrentContext()->OnIdle(INT64_MAX); + EXPECT_TRUE(GetChildFrameNode(frameNode_, 8)); + EXPECT_FALSE(GetChildFrameNode(frameNode_, 7)); + + UpdateCurrentOffset(200.0f); + EXPECT_EQ(pattern_->layoutInfo_->startIndex_, 16); + EXPECT_EQ(pattern_->layoutInfo_->endIndex_, 23); + EXPECT_FALSE(GetChildFrameNode(frameNode_, 7)); + EXPECT_EQ(GetChildY(frameNode_, 18), 180.0f); + + PipelineContext::GetCurrentContext()->OnIdle(INT64_MAX); + EXPECT_TRUE(GetChildFrameNode(frameNode_, 7)); +} + /** * @tc.name: Remeasure001 * @tc.desc: Test triggering measure multiple times on the same Algo object diff --git a/test/unittest/core/pattern/waterflow/water_flow_test_ng.cpp b/test/unittest/core/pattern/waterflow/water_flow_test_ng.cpp index 53a65e1d16f..57b0c3e3c49 100644 --- a/test/unittest/core/pattern/waterflow/water_flow_test_ng.cpp +++ b/test/unittest/core/pattern/waterflow/water_flow_test_ng.cpp @@ -148,6 +148,41 @@ void WaterFlowTestNg::CreateItemsInRepeat(int32_t itemNumber, std::function&& getHeight) + : itemCnt_(itemCnt), getHeight_(getHeight) + {} + +protected: + int32_t OnGetTotalCount() override + { + return itemCnt_; + } + + std::pair> OnGetChildByIndex( + int32_t index, std::unordered_map& expiringItems) override + { + auto node = AceType::MakeRefPtr( + V2::FLOW_ITEM_ETS_TAG, -1, AceType::MakeRefPtr()); + node->GetLayoutProperty()->UpdateUserDefinedIdealSize( + CalcSize(CalcLength(FILL_LENGTH), CalcLength(getHeight_(index)))); + return { std::to_string(index), node }; + } + +private: + int32_t itemCnt_ = 0; + const std::function getHeight_; +}; + +void WaterFlowTestNg::CreateItemsInLazyForEach(int32_t itemNumber, std::function&& getHeight) +{ + RefPtr mockLazy = AceType::MakeRefPtr(itemNumber, std::move(getHeight)); + ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId()); + LazyForEachModelNG lazyForEachModelNG; + lazyForEachModelNG.Create(mockLazy); +} + WaterFlowItemModelNG WaterFlowTestNg::CreateWaterFlowItem(float mainSize) { ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId()); @@ -188,26 +223,6 @@ void WaterFlowTestNg::CreateRandomWaterFlowItems(int32_t itemNumber) } } -void WaterFlowTestNg::CreateLazyForEachItems(int32_t itemNumber) -{ - auto waterFlowNode = ViewStackProcessor::GetInstance()->GetMainElementNode(); - auto weakWaterFlow = AceType::WeakClaim(AceType::RawPtr(waterFlowNode)); - const RefPtr lazyForEachActuator = AceType::MakeRefPtr(); - ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId()); - LazyForEachModelNG lazyForEachModelNG; - lazyForEachModelNG.Create(lazyForEachActuator); - auto node = ViewStackProcessor::GetInstance()->GetMainElementNode(); - auto lazyForEachNode = AceType::DynamicCast(node); - for (int32_t index = 0; index < itemNumber; index++) { - CreateWaterFlowItem((index & 1) == 0 ? ITEM_MAIN_SIZE : BIG_ITEM_MAIN_SIZE); - auto waterFlowItemNode = ViewStackProcessor::GetInstance()->GetMainElementNode(); - lazyForEachNode->builder_->cachedItems_.try_emplace( - index, LazyForEachChild(std::to_string(index), waterFlowItemNode)); - ViewStackProcessor::GetInstance()->Pop(); - ViewStackProcessor::GetInstance()->StopGetAccessRecording(); - } -} - void WaterFlowTestNg::AddItems(int32_t itemNumber) { for (int i = 0; i < itemNumber; ++i) { @@ -307,7 +322,7 @@ HWTEST_F(WaterFlowTestNg, LazyForeachLayout001, TestSize.Level1) { WaterFlowModelNG model = CreateWaterFlow(); model.SetColumnsTemplate("1fr 1fr"); - CreateLazyForEachItems(); + CreateItemsInLazyForEach(100, [](int32_t index) { return (index & 1) == 0 ? ITEM_MAIN_SIZE : BIG_ITEM_MAIN_SIZE; }); CreateDone(); auto lazyForEachNode = AceType::DynamicCast(frameNode_->GetChildAtIndex(0)); EXPECT_TRUE(IsEqual(GetLazyChildRect(0), RectF(0, 0, 240, 100))); diff --git a/test/unittest/core/pattern/waterflow/water_flow_test_ng.h b/test/unittest/core/pattern/waterflow/water_flow_test_ng.h index 7d7cff37ae8..716c9e2f804 100644 --- a/test/unittest/core/pattern/waterflow/water_flow_test_ng.h +++ b/test/unittest/core/pattern/waterflow/water_flow_test_ng.h @@ -47,11 +47,11 @@ protected: WaterFlowModelNG CreateWaterFlow(); void CreateItemsInRepeat(int32_t itemNumber, std::function&& getSize); + void CreateItemsInLazyForEach(int32_t itemNumber, std::function&& getHeight); void CreateWaterFlowItems(int32_t itemNumber = TOTAL_LINE_NUMBER); WaterFlowItemModelNG CreateWaterFlowItem(float mainSize); void CreateFocusableWaterFlowItems(int32_t itemNumber = TOTAL_LINE_NUMBER); void CreateRandomWaterFlowItems(int32_t itemNumber); - void CreateLazyForEachItems(int32_t itemNumber = TOTAL_LINE_NUMBER); void CreateItemWithHeight(float height); void UpdateCurrentOffset(float offset, int32_t source = SCROLL_FROM_UPDATE); void MouseSelect(Offset start, Offset end);