From 9762bbad85b58cbbdb135fc8214d9f55f8fe9176 Mon Sep 17 00:00:00 2001 From: Nikolay Rykunov Date: Wed, 1 Nov 2023 16:12:39 +0100 Subject: [PATCH] Fix index out of bounds for items call in SelectableLazyColumn --- .../foundation/lazy/SelectableLazyColumn.kt | 2 +- .../lazy/SelectableLazyColumnTest.kt | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 foundation/src/test/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumnTest.kt diff --git a/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt b/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt index fd0d66b2c..5bd87326a 100644 --- a/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt +++ b/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt @@ -125,7 +125,7 @@ fun SelectableLazyColumn( is Entry.Items -> items( count = entry.count, - key = { entry.key(entry.startIndex + it) }, + key = { entry.key(it) }, contentType = { entry.contentType(it) }, ) { index -> val itemScope = SelectableLazyItemScope(entry.key(index) in state.selectedKeys, isFocused) diff --git a/foundation/src/test/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumnTest.kt b/foundation/src/test/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumnTest.kt new file mode 100644 index 000000000..0d9af8ab3 --- /dev/null +++ b/foundation/src/test/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumnTest.kt @@ -0,0 +1,56 @@ +package org.jetbrains.jewel.foundation.lazy + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.requiredHeight +import androidx.compose.foundation.lazy.LazyListState +import androidx.compose.foundation.text.BasicText +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.unit.dp +import kotlinx.coroutines.runBlocking +import org.junit.Rule +import org.junit.Test + +internal class SelectableLazyColumnTest { + + @get:Rule + val composeRule = createComposeRule() + + @Test + fun `column with multiple items`() = runBlocking { + val items1 = (0..10).toList() + val items2 = (11..50).toList() + val scrollState = SelectableLazyListState(LazyListState()) + composeRule.setContent { + Box(modifier = Modifier.requiredHeight(100.dp)) { + SelectableLazyColumn(state = scrollState) { + items( + items1.size, + key = { + items1[it] + }, + ) { + val itemText = "Item ${items1[it]}" + BasicText(itemText, modifier = Modifier.testTag(itemText)) + } + + items( + items2.size, + key = { + items2[it] + }, + ) { + val itemText = "Item ${items2[it]}" + BasicText(itemText, modifier = Modifier.testTag(itemText)) + } + } + } + } + composeRule.awaitIdle() + composeRule.onNodeWithTag("Item 20").assertDoesNotExist() + scrollState.scrollToItem(20) + composeRule.onNodeWithTag("Item 20").assertExists() + } +}