add lazyForEach test

Signed-off-by: Tianer Zhou <zhoutianer@huawei.com>
Change-Id: I2bbf0aaf3cafc14398ee38e4865809fcd8dc6f89
This commit is contained in:
Tianer Zhou 2024-11-13 17:25:21 +08:00
parent a963a96a11
commit 4e0a1268c8
4 changed files with 71 additions and 23 deletions

2
.gitignore vendored
View File

@ -17,3 +17,5 @@ lite/ui/
.clangd .clangd
__pycache__/ __pycache__/
output/ output/
# placeholder for generated file
frameworks/core/components/theme/theme_constants_defines.h

View File

@ -15,7 +15,6 @@
#include "water_flow_test_ng.h" #include "water_flow_test_ng.h"
#include "core/components_ng/property/property.h"
#include "core/components_ng/syntax/if_else_node.h" #include "core/components_ng/syntax/if_else_node.h"
#define protected public #define protected public
@ -422,6 +421,38 @@ HWTEST_F(WaterFlowTestNg, Cache003, TestSize.Level1)
EXPECT_EQ(GetChildY(frameNode_, 34), -250.0f); 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.name: Remeasure001
* @tc.desc: Test triggering measure multiple times on the same Algo object * @tc.desc: Test triggering measure multiple times on the same Algo object

View File

@ -148,6 +148,41 @@ void WaterFlowTestNg::CreateItemsInRepeat(int32_t itemNumber, std::function<floa
repeatModel.Create(itemNumber, {}, createFunc, updateFunc, getKeys, getTypes, [](uint32_t start, uint32_t end) {}); repeatModel.Create(itemNumber, {}, createFunc, updateFunc, getKeys, getTypes, [](uint32_t start, uint32_t end) {});
} }
class WaterFlowMockLazy : public Framework::MockLazyForEachBuilder {
public:
WaterFlowMockLazy(int32_t itemCnt, std::function<float(int32_t)>&& getHeight)
: itemCnt_(itemCnt), getHeight_(getHeight)
{}
protected:
int32_t OnGetTotalCount() override
{
return itemCnt_;
}
std::pair<std::string, RefPtr<NG::UINode>> OnGetChildByIndex(
int32_t index, std::unordered_map<std::string, NG::LazyForEachCacheChild>& expiringItems) override
{
auto node = AceType::MakeRefPtr<WaterFlowItemNode>(
V2::FLOW_ITEM_ETS_TAG, -1, AceType::MakeRefPtr<WaterFlowItemPattern>());
node->GetLayoutProperty()->UpdateUserDefinedIdealSize(
CalcSize(CalcLength(FILL_LENGTH), CalcLength(getHeight_(index))));
return { std::to_string(index), node };
}
private:
int32_t itemCnt_ = 0;
const std::function<float(int32_t)> getHeight_;
};
void WaterFlowTestNg::CreateItemsInLazyForEach(int32_t itemNumber, std::function<float(int32_t)>&& getHeight)
{
RefPtr<LazyForEachActuator> mockLazy = AceType::MakeRefPtr<WaterFlowMockLazy>(itemNumber, std::move(getHeight));
ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
LazyForEachModelNG lazyForEachModelNG;
lazyForEachModelNG.Create(mockLazy);
}
WaterFlowItemModelNG WaterFlowTestNg::CreateWaterFlowItem(float mainSize) WaterFlowItemModelNG WaterFlowTestNg::CreateWaterFlowItem(float mainSize)
{ {
ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId()); 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> lazyForEachActuator = AceType::MakeRefPtr<Framework::MockLazyForEachBuilder>();
ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
LazyForEachModelNG lazyForEachModelNG;
lazyForEachModelNG.Create(lazyForEachActuator);
auto node = ViewStackProcessor::GetInstance()->GetMainElementNode();
auto lazyForEachNode = AceType::DynamicCast<LazyForEachNode>(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) void WaterFlowTestNg::AddItems(int32_t itemNumber)
{ {
for (int i = 0; i < itemNumber; ++i) { for (int i = 0; i < itemNumber; ++i) {
@ -307,7 +322,7 @@ HWTEST_F(WaterFlowTestNg, LazyForeachLayout001, TestSize.Level1)
{ {
WaterFlowModelNG model = CreateWaterFlow(); WaterFlowModelNG model = CreateWaterFlow();
model.SetColumnsTemplate("1fr 1fr"); model.SetColumnsTemplate("1fr 1fr");
CreateLazyForEachItems(); CreateItemsInLazyForEach(100, [](int32_t index) { return (index & 1) == 0 ? ITEM_MAIN_SIZE : BIG_ITEM_MAIN_SIZE; });
CreateDone(); CreateDone();
auto lazyForEachNode = AceType::DynamicCast<LazyForEachNode>(frameNode_->GetChildAtIndex(0)); auto lazyForEachNode = AceType::DynamicCast<LazyForEachNode>(frameNode_->GetChildAtIndex(0));
EXPECT_TRUE(IsEqual(GetLazyChildRect(0), RectF(0, 0, 240, 100))); EXPECT_TRUE(IsEqual(GetLazyChildRect(0), RectF(0, 0, 240, 100)));

View File

@ -47,11 +47,11 @@ protected:
WaterFlowModelNG CreateWaterFlow(); WaterFlowModelNG CreateWaterFlow();
void CreateItemsInRepeat(int32_t itemNumber, std::function<float(uint32_t)>&& getSize); void CreateItemsInRepeat(int32_t itemNumber, std::function<float(uint32_t)>&& getSize);
void CreateItemsInLazyForEach(int32_t itemNumber, std::function<float(int32_t)>&& getHeight);
void CreateWaterFlowItems(int32_t itemNumber = TOTAL_LINE_NUMBER); void CreateWaterFlowItems(int32_t itemNumber = TOTAL_LINE_NUMBER);
WaterFlowItemModelNG CreateWaterFlowItem(float mainSize); WaterFlowItemModelNG CreateWaterFlowItem(float mainSize);
void CreateFocusableWaterFlowItems(int32_t itemNumber = TOTAL_LINE_NUMBER); void CreateFocusableWaterFlowItems(int32_t itemNumber = TOTAL_LINE_NUMBER);
void CreateRandomWaterFlowItems(int32_t itemNumber); void CreateRandomWaterFlowItems(int32_t itemNumber);
void CreateLazyForEachItems(int32_t itemNumber = TOTAL_LINE_NUMBER);
void CreateItemWithHeight(float height); void CreateItemWithHeight(float height);
void UpdateCurrentOffset(float offset, int32_t source = SCROLL_FROM_UPDATE); void UpdateCurrentOffset(float offset, int32_t source = SCROLL_FROM_UPDATE);
void MouseSelect(Offset start, Offset end); void MouseSelect(Offset start, Offset end);