Fix flaky test in LazyPagingItemsTest

Flaky due to race on LoadStates. We wanted to assert against the first LoadState where refresh is Loading, but sometimes the load finishes too quickly and we collect NotLoading state instead. Fixed by adding load delay.

Test: ./gradlew paging:paging-compose:cC
Fixes: 267374463
Change-Id: I60fdf1aba423b96bdba8148e7742bd68c8eda7a9
diff --git a/paging/paging-compose/src/androidTest/java/androidx/paging/compose/LazyPagingItemsTest.kt b/paging/paging-compose/src/androidTest/java/androidx/paging/compose/LazyPagingItemsTest.kt
index aede27e..50470e1 100644
--- a/paging/paging-compose/src/androidTest/java/androidx/paging/compose/LazyPagingItemsTest.kt
+++ b/paging/paging-compose/src/androidTest/java/androidx/paging/compose/LazyPagingItemsTest.kt
@@ -74,8 +74,9 @@
             initialLoadSize = 3,
             prefetchDistance = 1,
         ),
+        loadDelay: Long = 0,
         pagingSourceFactory: () -> PagingSource<Int, Int> = {
-            TestPagingSource(items = items, loadDelay = 0)
+            TestPagingSource(items = items, loadDelay = loadDelay)
         }
     ): Pager<Int, Int> {
         return Pager(config = config, pagingSourceFactory = pagingSourceFactory)
@@ -120,10 +121,10 @@
         assertThat(loadStates.first()).isEqualTo(expected)
     }
 
-    @Ignore // b/267374463
     @Test
     fun lazyPagingLoadStateAfterRefresh() {
-        val pager = createPager()
+        val pager = createPager(loadDelay = 100)
+
         val loadStates: MutableList<CombinedLoadStates> = mutableListOf()
 
         lateinit var lazyPagingItems: LazyPagingItems<Int>
@@ -132,10 +133,23 @@
             loadStates.add(lazyPagingItems.loadState)
         }
 
-        // we only want loadStates after manual refresh
-        loadStates.clear()
-        lazyPagingItems.refresh()
-        rule.waitForIdle()
+        // wait for both compose and paging to complete
+        rule.waitUntil {
+            rule.waitForIdle()
+            lazyPagingItems.loadState.refresh == LoadState.NotLoading(false)
+        }
+
+        rule.runOnIdle {
+            // we only want loadStates after manual refresh
+            loadStates.clear()
+            lazyPagingItems.refresh()
+        }
+
+        // wait for both compose and paging to complete
+        rule.waitUntil {
+            rule.waitForIdle()
+            lazyPagingItems.loadState.refresh == LoadState.NotLoading(false)
+        }
 
         assertThat(loadStates).isNotEmpty()
         val expected = CombinedLoadStates(