# androidx.paging.testing

*** ** * ** ***

Common/All

## Interfaces

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/paging/testing/LoadErrorHandler` | An interface to implement the error recovery strategy when `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Error`. | Cmn |

## Classes

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/paging/testing/SnapshotLoader` | Contains the public APIs for load operations in tests. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager` | A fake `https://developer.android.com/reference/kotlin/androidx/paging/Pager` class to simulate how a real Pager and UI would load data from a PagingSource. | Cmn |

## Enums

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/paging/testing/ErrorRecovery` | The method of recovery when `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` returns `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Error`. | Cmn |

## Extension functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/paging/PagingSourceFactory<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, Value>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/VisibleForTesting <Value : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<Value>.https://developer.android.com/reference/kotlin/androidx/paging/testing/package-summary#(kotlin.collections.List).asPagingSourceFactory()()` Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSourceFactory` that creates `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` instances. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/paging/PagingSourceFactory<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, Value>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/VisibleForTesting <Value : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<Value>>.https://developer.android.com/reference/kotlin/androidx/paging/testing/package-summary#(kotlinx.coroutines.flow.Flow).asPagingSourceFactory(kotlinx.coroutines.CoroutineScope)( coroutineScope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html )` Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSourceFactory` that creates `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` instances. | Cmn |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<Value>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/VisibleForTesting <Value : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<Value>>.https://developer.android.com/reference/kotlin/androidx/paging/testing/package-summary#(kotlinx.coroutines.flow.Flow).asSnapshot(androidx.paging.testing.LoadErrorHandler,kotlin.coroutines.SuspendFunction1)( onError: https://developer.android.com/reference/kotlin/androidx/paging/testing/LoadErrorHandler, loadOperations: suspend https://developer.android.com/reference/kotlin/androidx/paging/testing/SnapshotLoader<Value>.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Runs the `https://developer.android.com/reference/kotlin/androidx/paging/testing/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

### List.asPagingSourceFactory

Cmn Artifact: [androidx.paging:paging-testing](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/testing/StaticListPagingSourceFactory.kt+function:asPagingSourceFactory)

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

Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSourceFactory` that creates `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` instances.

Can be used as the pagingSourceFactory when constructing a `https://developer.android.com/reference/kotlin/androidx/paging/Pager` in tests. The same factory should be reused within the lifetime of a ViewModel.

Extension method on a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html` of data from which a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` will load from. While this factory supports multi-generational operations such as `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#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.

### Flow.asPagingSourceFactory

Cmn Artifact: [androidx.paging:paging-testing](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/testing/StaticListPagingSourceFactory.kt+function:asPagingSourceFactory) Added in [3.2.0](https://developer.android.com/jetpack/androidx/releases/paging#3.2.0)

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

Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSourceFactory` that creates `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` instances.

Can be used as the pagingSourceFactory when constructing a `https://developer.android.com/reference/kotlin/androidx/paging/Pager` in tests. The same factory should be reused within the lifetime of a ViewModel.

Extension method on a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` of list that represents the data source, with each static list representing a generation of data from which a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` will load from. With every emission to the flow, the current `https://developer.android.com/reference/kotlin/androidx/paging/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: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` | the CoroutineScope to collect from the Flow of list. |

### Flow.asSnapshot

Cmn Artifact: [androidx.paging:paging-testing](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/testing/PagerFlowSnapshot.kt+function:asSnapshot)

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

Runs the `https://developer.android.com/reference/kotlin/androidx/paging/testing/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: https://developer.android.com/reference/kotlin/androidx/paging/testing/LoadErrorHandler = LoadErrorHandler { THROW }` | The error recovery strategy when PagingSource returns LoadResult.Error. A lambda that returns an `https://developer.android.com/reference/kotlin/androidx/paging/testing/ErrorRecovery` value. The default strategy is `https://developer.android.com/reference/kotlin/androidx/paging/testing/ErrorRecovery#THROW`. |
| `loadOperations: suspend https://developer.android.com/reference/kotlin/androidx/paging/testing/SnapshotLoader<Value>.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html = {}` | The block containing `https://developer.android.com/reference/kotlin/androidx/paging/testing/SnapshotLoader` load operations. |