|
16 | 16 |
|
17 | 17 | package androidx.compose.foundation.lazy.list |
18 | 18 |
|
| 19 | +import androidx.compose.foundation.ComposeFoundationFlags.isCacheWindowRefillFixEnabled |
19 | 20 | import androidx.compose.foundation.ExperimentalFoundationApi |
20 | 21 | import androidx.compose.foundation.gestures.Orientation |
21 | 22 | import androidx.compose.foundation.gestures.scrollBy |
@@ -45,6 +46,7 @@ import com.google.common.truth.Truth.assertThat |
45 | 46 | import kotlinx.coroutines.CoroutineScope |
46 | 47 | import kotlinx.coroutines.launch |
47 | 48 | import kotlinx.coroutines.runBlocking |
| 49 | +import org.junit.Assume |
48 | 50 | import org.junit.Test |
49 | 51 | import org.junit.runner.RunWith |
50 | 52 | import org.junit.runners.Parameterized |
@@ -185,7 +187,8 @@ class LazyListCacheWindowTest(orientation: Orientation) : |
185 | 187 | } |
186 | 188 |
|
187 | 189 | @Test |
188 | | - fun datasetChanged_shouldMakeSureNestedItemsChanged() { |
| 190 | + fun datasetChanged_shouldMakeSureNestedItemsChanged_afterScroll() { |
| 191 | + Assume.assumeTrue(isCacheWindowRefillFixEnabled) |
189 | 192 | val items = mutableStateOf(listOf("a", "b", "c", "d", "e")) |
190 | 193 |
|
191 | 194 | rule.setContent { |
@@ -264,6 +267,81 @@ class LazyListCacheWindowTest(orientation: Orientation) : |
264 | 267 | rule.onNodeWithTag("second-nested-e").assertExists() // nested prefetched |
265 | 268 | } |
266 | 269 |
|
| 270 | + @Test |
| 271 | + fun datasetChanged_shouldMakeSureNestedItemsChanged_noScroll() { |
| 272 | + Assume.assumeTrue(isCacheWindowRefillFixEnabled) |
| 273 | + val items = mutableStateOf(listOf("a", "b", "c", "d")) |
| 274 | + |
| 275 | + rule.setContent { |
| 276 | + @OptIn(ExperimentalFoundationApi::class) |
| 277 | + state = rememberLazyListState(cacheWindow = viewportWindow) |
| 278 | + LazyColumnOrRow( |
| 279 | + Modifier.mainAxisSize(itemsSizeDp * 2f) |
| 280 | + .then( |
| 281 | + object : RemeasurementModifier { |
| 282 | + override fun onRemeasurementAvailable(remeasurement: Remeasurement) { |
| 283 | + remeasure = remeasurement |
| 284 | + } |
| 285 | + } |
| 286 | + ), |
| 287 | + state, |
| 288 | + ) { |
| 289 | + items(items.value, key = { it }) { |
| 290 | + if (it == "e" || it == "f") { |
| 291 | + val state = rememberLazyListState(cacheWindow = viewportWindow) |
| 292 | + LazyRow( |
| 293 | + Modifier.mainAxisSize(itemsSizeDp).fillMaxCrossAxis().testTag(it), |
| 294 | + state = state, |
| 295 | + ) { |
| 296 | + item { |
| 297 | + Spacer( |
| 298 | + Modifier.mainAxisSize(itemsSizeDp) |
| 299 | + .fillMaxCrossAxis() |
| 300 | + .testTag("first-nested-$it") |
| 301 | + ) |
| 302 | + } |
| 303 | + |
| 304 | + item { |
| 305 | + Spacer( |
| 306 | + Modifier.mainAxisSize(itemsSizeDp) |
| 307 | + .fillMaxCrossAxis() |
| 308 | + .testTag("second-nested-$it") |
| 309 | + ) |
| 310 | + } |
| 311 | + } |
| 312 | + } else { |
| 313 | + Spacer( |
| 314 | + Modifier.mainAxisSize(itemsSizeDp) |
| 315 | + .fillMaxCrossAxis() |
| 316 | + .testTag(it) |
| 317 | + .layout { measurable, constraints -> |
| 318 | + val placeable = measurable.measure(constraints) |
| 319 | + layout(placeable.width, placeable.height) { |
| 320 | + placeable.place(0, 0) |
| 321 | + } |
| 322 | + } |
| 323 | + ) |
| 324 | + } |
| 325 | + } |
| 326 | + } |
| 327 | + } |
| 328 | + |
| 329 | + rule.onNodeWithTag("a").assertIsDisplayed() // fully visible |
| 330 | + rule.onNodeWithTag("b").assertIsDisplayed() // fully visible |
| 331 | + rule.onNodeWithTag("c").assertExists() // part of the window |
| 332 | + rule.onNodeWithTag("d").assertExists() // part of the window |
| 333 | + |
| 334 | + rule.runOnIdle { items.value = listOf("a", "b", "e", "f", "g", "h") } |
| 335 | + rule.waitForIdle() |
| 336 | + |
| 337 | + rule.onNodeWithTag("e").assertExists() // item e will take place of item c |
| 338 | + rule.onNodeWithTag("first-nested-e").assertExists() // nested prefetched |
| 339 | + rule.onNodeWithTag("second-nested-e").assertExists() // nested prefetched |
| 340 | + rule.onNodeWithTag("f").assertExists() // item f will take place of item d |
| 341 | + rule.onNodeWithTag("first-nested-f").assertExists() // nested prefetched |
| 342 | + rule.onNodeWithTag("second-nested-f").assertExists() // nested prefetched |
| 343 | + } |
| 344 | + |
267 | 345 | @Test |
268 | 346 | fun datasetChanged_noScrollHappened_shouldKeepAroundWithinBounds_notCrash() { |
269 | 347 | val numItems = mutableStateOf(100) |
|
0 commit comments