fix GridItem focus info and add test

Signed-off-by: Tianer Zhou <zhoutianer@huawei.com>
Change-Id: Iad51c5cb5f9bd44c56e464d22ed38eda46dcb79b
This commit is contained in:
Tianer Zhou 2024-11-20 15:41:33 +08:00
parent cf074d85fb
commit 0668035ef3
3 changed files with 29 additions and 0 deletions

View File

@ -369,6 +369,7 @@ void GridIrregularFiller::SetItemInfo(int32_t idx, int32_t row, int32_t col, Gri
props->UpdateCrossIndex(col);
if (size.rows == 1 && size.columns == 1) {
pattern->ResetGridItemInfo();
return;
}
pattern->SetIrregularItemInfo({ .mainIndex = row,

View File

@ -124,6 +124,7 @@ GridItemModelNG GridTestNg::CreateGridItem(float width, float height, GridItemSt
} else if (height != NULL_VALUE) {
ViewAbstract::SetHeight(CalcLength(height));
}
ViewAbstract::SetFocusable(true);
return itemModel;
}

View File

@ -16,6 +16,7 @@
#include "irregular_matrices.h"
#include "test/unittest/core/pattern/grid/grid_test_ng.h"
#include "core/components_ng/pattern/grid/grid_item_pattern.h"
#include "core/components_ng/pattern/grid/irregular/grid_layout_range_solver.h"
namespace OHOS::Ace::NG {
@ -734,4 +735,30 @@ HWTEST_F(GridLayoutRangeTest, ScrollEnabled001, TestSize.Level1)
EXPECT_TRUE(pattern_->scrollable_);
}
}
/**
* @tc.name: Focus001
* @tc.desc: Test Grid changing focus
* @tc.type: FUNC
*/
HWTEST_F(GridLayoutRangeTest, Focus001, TestSize.Level1)
{
auto model = CreateRepeatGrid(50, [](uint32_t idx) { return 200.0f; });
model.SetColumnsTemplate("1fr 1fr 1fr");
const auto options = GetOptionDemo14();
model.SetLayoutOptions(options);
model.SetCachedCount(1);
CreateDone();
for (int i = 0; i < 10; ++i) {
UpdateCurrentOffset(-200.0f);
for (int i = pattern_->info_.startIndex_; i <= pattern_->info_.endIndex_; ++i) {
bool hasInfo = GetChildPattern<GridItemPattern>(frameNode_, i)->GetIrregularItemInfo().has_value();
if (options.irregularIndexes.count(i)) {
EXPECT_TRUE(hasInfo);
} else {
EXPECT_FALSE(hasInfo);
}
}
}
}
} // namespace OHOS::Ace::NG