Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
Expand Down Expand Up @@ -1465,7 +1464,7 @@ class PagingDataDifferTest(
@Test
fun refresh_loadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
pagingSources, _, _ ->
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }

// execute queued initial REFRESH
loadDispatcher.scheduler.advanceUntilIdle()
Expand Down Expand Up @@ -1498,7 +1497,7 @@ class PagingDataDifferTest(
differ.addLoadStateListener {
loadStateCallbacks.add(it)
}
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }
// execute initial refresh
loadDispatcher.scheduler.advanceUntilIdle()
assertThat(differ.snapshot()).containsExactlyElementsIn(0 until 9)
Expand Down Expand Up @@ -1545,7 +1544,7 @@ class PagingDataDifferTest(

@Test
fun appendInvalid_loadStates() = runTest { differ, loadDispatcher, pagingSources, _, _ ->
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }

// initial REFRESH
loadDispatcher.scheduler.advanceUntilIdle()
Expand Down Expand Up @@ -1607,7 +1606,7 @@ class PagingDataDifferTest(
@Test
fun prependInvalid_loadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
pagingSources, _, _ ->
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }

// initial REFRESH
loadDispatcher.scheduler.advanceUntilIdle()
Expand Down Expand Up @@ -1661,7 +1660,7 @@ class PagingDataDifferTest(
@Test
fun refreshInvalid_loadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
pagingSources, _, _ ->
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }

// execute queued initial REFRESH load which will return LoadResult.Invalid()
pagingSources[0].nextLoadResult = LoadResult.Invalid()
Expand Down Expand Up @@ -1691,7 +1690,7 @@ class PagingDataDifferTest(

@Test
fun appendError_retryLoadStates() = runTest { differ, loadDispatcher, pagingSources, _, _ ->
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }

// initial REFRESH
loadDispatcher.scheduler.advanceUntilIdle()
Expand Down Expand Up @@ -1742,7 +1741,7 @@ class PagingDataDifferTest(
@Test
fun prependError_retryLoadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
pagingSources, _, _ ->
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }

// initial REFRESH
loadDispatcher.scheduler.advanceUntilIdle()
Expand Down Expand Up @@ -1784,7 +1783,7 @@ class PagingDataDifferTest(

@Test
fun refreshError_retryLoadStates() = runTest { differ, loadDispatcher, pagingSources, _, _ ->
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }

// initial load returns LoadResult.Error
val exception = Throwable()
Expand Down Expand Up @@ -1817,7 +1816,7 @@ class PagingDataDifferTest(
@Test
fun prependError_refreshLoadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
pagingSources, _, _ ->
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }

// initial REFRESH
loadDispatcher.scheduler.advanceUntilIdle()
Expand Down Expand Up @@ -1861,7 +1860,7 @@ class PagingDataDifferTest(
@Test
fun refreshError_refreshLoadStates() = runTest { differ, loadDispatcher, pagingSources,
_, _ ->
val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }

// the initial load will return LoadResult.Error
val exception = Throwable()
Expand Down Expand Up @@ -1903,7 +1902,7 @@ class PagingDataDifferTest(
TestPagingSource(loadDelay = 500, items = emptyList())
}

val collectLoadStates = differ.collectLoadStates()
val collectLoadStates = launch { differ.collectLoadStates() }
val job = launch {
pager.flow.collectLatest {
differ.collectFrom(it)
Expand Down Expand Up @@ -2000,9 +1999,8 @@ class PagingDataDifferTest(

val differ = SimpleDiffer(
differCallback = dummyDifferCallback,
coroutineScope = backgroundScope,
)
differ.collectLoadStates()
backgroundScope.launch { differ.collectLoadStates() }

val job = launch {
pager.collectLatest {
Expand All @@ -2019,9 +2017,8 @@ class PagingDataDifferTest(
// we start a separate differ to recollect on cached Pager.flow
val differ2 = SimpleDiffer(
differCallback = dummyDifferCallback,
coroutineScope = backgroundScope,
)
differ2.collectLoadStates()
backgroundScope.launch { differ2.collectLoadStates() }

val job2 = launch {
pager.collectLatest {
Expand Down Expand Up @@ -2199,7 +2196,7 @@ class PagingDataDifferTest(
).also { pagingSources.add(it) }
}
),
block: (
block: TestScope.(
differ: SimpleDiffer,
loadDispatcher: TestDispatcher,
pagingSources: List<TestPagingSource>,
Expand All @@ -2209,7 +2206,6 @@ class PagingDataDifferTest(
) = testScope.runTest {
val differ = SimpleDiffer(
differCallback = dummyDifferCallback,
coroutineScope = this,
)
val uiReceivers = mutableListOf<TrackableUiReceiverWrapper>()
val hintReceivers = mutableListOf<TrackableHintReceiverWrapper>()
Expand Down Expand Up @@ -2345,11 +2341,9 @@ private class TrackableHintReceiverWrapper(
}
}

@OptIn(ExperimentalCoroutinesApi::class)
private class SimpleDiffer(
differCallback: DifferCallback,
cachedPagingData: PagingData<Int>? = null,
val coroutineScope: CoroutineScope = TestScope(UnconfinedTestDispatcher())
) : PagingDataDiffer<Int>(differCallback = differCallback, cachedPagingData = cachedPagingData) {
override suspend fun presentNewList(
previousList: NullPaddedList<Int>,
Expand All @@ -2371,11 +2365,9 @@ private class SimpleDiffer(
return newCombinedLoadStates
}

fun collectLoadStates(): Job {
return coroutineScope.launch {
nonNullLoadStateFlow.collect { combinedLoadStates ->
_localLoadStates.add(combinedLoadStates)
}
suspend fun collectLoadStates() {
nonNullLoadStateFlow.collect { combinedLoadStates ->
_localLoadStates.add(combinedLoadStates)
}
}
}
Expand Down