androidx.paging.testing


Interfaces

LoadErrorHandler

An interface to implement the error recovery strategy when PagingSource returns a LoadResult.Error.

Cmn

Classes

SnapshotLoader

Contains the public APIs for load operations in tests.

Cmn
TestPager

A fake Pager class to simulate how a real Pager and UI would load data from a PagingSource.

Cmn

Enums

ErrorRecovery

The method of recovery when PagingSource returns LoadResult.Error.

Cmn

Extension functions summary

PagingSourceFactory<Int, Value>

Returns a PagingSourceFactory that creates PagingSource instances.

Cmn
PagingSourceFactory<Int, Value>
@VisibleForTesting
<Value : Any> Flow<List<Value>>.asPagingSourceFactory(
    coroutineScope: CoroutineScope
)

Returns a PagingSourceFactory that creates PagingSource instances.

Cmn
suspend List<Value>
@VisibleForTesting
<Value : Any> Flow<PagingData<Value>>.asSnapshot(
    onError: LoadErrorHandler,
    loadOperations: suspend SnapshotLoader<Value>.() -> Unit
)

Runs the SnapshotLoader load operations that are passed in and returns a List of data that would be presented to the UI after all load operations are complete.

Cmn

Extension functions

asPagingSourceFactory

@VisibleForTesting
fun <Value : Any> List<Value>.asPagingSourceFactory(): PagingSourceFactory<Int, Value>

Returns a PagingSourceFactory that creates PagingSource instances.

Can be used as the pagingSourceFactory when constructing a Pager in tests. The same factory should be reused within the lifetime of a ViewModel.

Extension method on a List of data from which a PagingSource will load from. While this factory supports multi-generational operations such as REFRESH, it does not support updating the data source. This means any PagingSources generated by the same factory will load from the exact same list of data.

asPagingSourceFactory

@VisibleForTesting
fun <Value : Any> Flow<List<Value>>.asPagingSourceFactory(
    coroutineScope: CoroutineScope
): PagingSourceFactory<Int, Value>

Returns a PagingSourceFactory that creates PagingSource instances.

Can be used as the pagingSourceFactory when constructing a Pager in tests. The same factory should be reused within the lifetime of a ViewModel.

Extension method on a Flow of list that represents the data source, with each static list representing a generation of data from which a PagingSource will load from. With every emission to the flow, the current PagingSource will be invalidated, thereby triggering a new generation of Paged data.

Supports multiple factories and thus multiple collection on the same flow.

Parameters
coroutineScope: CoroutineScope

the CoroutineScope to collect from the Flow of list.

asSnapshot

@VisibleForTesting
suspend fun <Value : Any> Flow<PagingData<Value>>.asSnapshot(
    onError: LoadErrorHandler = LoadErrorHandler { THROW },
    loadOperations: suspend SnapshotLoader<Value>.() -> Unit = { }
): List<Value>

Runs the SnapshotLoader load operations that are passed in and returns a List of data that would be presented to the UI after all load operations are complete.

Parameters
onError: LoadErrorHandler = LoadErrorHandler { THROW }

The error recovery strategy when PagingSource returns LoadResult.Error. A lambda that returns an ErrorRecovery value. The default strategy is ErrorRecovery.THROW.

loadOperations: suspend SnapshotLoader<Value>.() -> Unit = { }

The block containing SnapshotLoader load operations.