# TestPager

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/TestPager.kt+class:androidx.paging.testing.TestPager) Added in [3.2.0](https://developer.android.com/jetpack/androidx/releases/paging#3.2.0)

*** ** * ** ***

Kotlin \|[Java](https://developer.android.com/reference/androidx/paging/testing/TestPager "View this page in Java") Common/All
- Cmn

  ```
  @VisibleForTesting
  class TestPager<Key : Any, Value : Any>
  ```

*** ** * ** ***

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.

As Paging's first load is always of type `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#REFRESH`, the first load operation of the `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager` must be a call to `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#refresh(kotlin.Any)`.

This class only supports loads from a single instance of PagingSource. To simulate multi-generational Paging behavior, you must create a new `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager` by supplying a new instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`.

## Summary

| ### Public constructors |
|---|---|
| `<Key : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html, Value : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#TestPager(androidx.paging.PagingConfig,androidx.paging.PagingSource)( config: https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig, pagingSource: https://developer.android.com/reference/kotlin/androidx/paging/PagingSource<Key, Value> )` | Cmn |

| ### Public functions |
|---|---|---|
| `suspend https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult<Key, Value>?` | `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#append()()` Performs a load of `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#APPEND` on the PagingSource. | Cmn |
| `suspend https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page<Key, Value>?` | `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getLastLoadedPage()()` Returns the most recent `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page` loaded from the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`. | Cmn |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page<Key, Value>>` | `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getPages()()` Returns the current list of `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page` loaded so far from the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`. | Cmn |
| `suspend https://developer.android.com/reference/kotlin/androidx/paging/PagingState<Key, Value>` | `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getPagingState(kotlin.Int)(anchorPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingState` to generate a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams#key()` by supplying it to `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)`. | Cmn |
| `suspend https://developer.android.com/reference/kotlin/androidx/paging/PagingState<Key, Value>` | `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getPagingState(kotlin.Function1)(anchorPositionLookup: (item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingState` to generate a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams#key()` by supplying it to `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)`. | Cmn |
| `suspend https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult<Key, Value>?` | `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#prepend()()` Performs a load of `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#PREPEND` on the PagingSource. | Cmn |
| `suspend https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult<Key, Value>` | `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#refresh(kotlin.Any)(initialKey: Key?)` Performs a load of `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#REFRESH` on the PagingSource. | Cmn |

## Public constructors

### TestPager

Cmn

```
<Key : Any, Value : Any> TestPager(
    config: PagingConfig,
    pagingSource: PagingSource<Key, Value>
)
```

| Parameters |
|---|---|
| `config: https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig` | the `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig` to configure this TestPager's loading behavior. |
| `pagingSource: https://developer.android.com/reference/kotlin/androidx/paging/PagingSource<Key, Value>` | the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` to load data from. |

## Public functions

### append

Cmn

```
suspend fun append(): PagingSource.LoadResult<Key, Value>?
```

Performs a load of `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#APPEND` on the PagingSource.

Since Paging's first load is always of `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#REFRESH`, `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#refresh(kotlin.Any)` must always be called first before this `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#append()` is called.

If `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig#maxSize()` is implemented, `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#append()` loads that exceed `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig#maxSize()` will cause pages to be dropped from the front of loaded pages.

Returns the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult` from calling `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#load(androidx.paging.PagingSource.LoadParams)`. If the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams#key()` is null, such as when there is no more data to append, this append will be no-op by returning null.

### getLastLoadedPage

Cmn

```
suspend fun getLastLoadedPage(): PagingSource.LoadResult.Page<Key, Value>?
```

Returns the most recent `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page` loaded from the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`. Null if no pages have been returned from `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`. For example, if PagingSource has only returned `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Error` or `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Invalid`.

### getPages

Cmn

```
suspend fun getPages(): List<PagingSource.LoadResult.Page<Key, Value>>
```

Returns the current list of `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page` loaded so far from the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`.

### getPagingState

Cmn

```
suspend fun getPagingState(anchorPosition: Int): PagingState<Key, Value>
```

Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingState` to generate a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams#key()` by supplying it to `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)`. The key returned from `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)` should be used as the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams.Refresh#key()` when calling `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#refresh(kotlin.Any)` on a new generation of TestPager.

The anchorPosition must be within index of loaded items, which can include placeholders if `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig#enablePlaceholders()` is true. For example:

- No placeholders: If 40 items have been loaded so far , anchorPosition must be in 0 .. 39.

- With placeholders: If there are a total of 100 loadable items, the anchorPosition must be in 0..99.

The `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getPagingState(kotlin.Int)` should be the index that the user has hypothetically scrolled to on the UI. Since the `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#anchorPosition()` in Paging can be based on any item or placeholder currently visible on the screen, the actual value of `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#anchorPosition()` may not exactly match the `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getPagingState(kotlin.Int)` passed to this function even if viewing the same page of data.

Note that when `[PagingConfig.enablePlaceholders] = false`, the `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#anchorPosition()` returned from this function references the absolute index within all loadable data. For example, with items0 - 99: If items20 - 30 were loaded without placeholders, anchorPosition 0 references item20. But once translated into `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#anchorPosition()`, anchorPosition 0 references item0. The `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` is expected to handle this correctly within `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)` when `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig#enablePlaceholders()` = false.

| Parameters |
|---|---|
| `anchorPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the index representing the last accessed item within the items presented on the UI, which may be a placeholder if `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig#enablePlaceholders()` is true. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if anchorPosition is out of bounds. |

### getPagingState

Cmn

```
suspend fun getPagingState(anchorPositionLookup: (item) -> Boolean): PagingState<Key, Value>
```

Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingState` to generate a `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams#key()` by supplying it to `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)`. The key returned from `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)` should be used as the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams.Refresh#key()` when calling `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#refresh(kotlin.Any)` on a new generation of TestPager.

The `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getPagingState(kotlin.Function1)` lambda should return an item that the user has hypothetically scrolled to on the UI. The item must have already been loaded prior to using this helper. To generate a PagingState anchored to a placeholder, use the overloaded `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getPagingState(kotlin.Function1)` function instead. Since the `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#anchorPosition()` in Paging can be based on any item or placeholder currently visible on the screen, the actual value of `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#anchorPosition()` may not exactly match the anchorPosition returned from this function even if viewing the same page of data.

Note that when `[PagingConfig.enablePlaceholders] = false`, the `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#anchorPosition()` returned from this function references the absolute index within all loadable data. For example, with items0 - 99: If items20 - 30 were loaded without placeholders, anchorPosition 0 references item20. But once translated into `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#anchorPosition()`, anchorPosition 0 references item0. The `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` is expected to handle this correctly within `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)` when `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig#enablePlaceholders()` = false.

| Parameters |
|---|---|
| `anchorPositionLookup: (item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | the predicate to match with an item which will serve as the basis for generating the `https://developer.android.com/reference/kotlin/androidx/paging/PagingState`. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if the given predicate fails to match with an item. |

### prepend

Cmn

```
suspend fun prepend(): PagingSource.LoadResult<Key, Value>?
```

Performs a load of `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#PREPEND` on the PagingSource.

Since Paging's first load is always of `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#REFRESH`, `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#refresh(kotlin.Any)` must always be called first before this `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#prepend()` is called.

If `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig#maxSize()` is implemented, `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#prepend()` loads that exceed `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig#maxSize()` will cause pages to be dropped from the end of loaded pages.

Returns the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult` from calling `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#load(androidx.paging.PagingSource.LoadParams)`. If the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams#key()` is null, such as when there is no more data to prepend, this prepend will be no-op by returning null.

### refresh

Cmn

```
suspend fun refresh(initialKey: Key? = null): PagingSource.LoadResult<Key, Value>
```

Performs a load of `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#REFRESH` on the PagingSource.

If initialKey != null, refresh will start loading from the supplied key.

Since Paging's first load is always of `https://developer.android.com/reference/kotlin/androidx/paging/LoadType#REFRESH`, this method must be the very first load operation to be called on the TestPager before either `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#append()` or `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#prepend()` can be called. However, other non-loading operations can still be invoked. For example, you can call `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getLastLoadedPage()` before any load operations.

Returns the LoadResult upon refresh on the `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`.

| Parameters |
|---|---|
| `initialKey: Key? = null` | the `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager` to start loading data from on initial refresh. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | TestPager does not support multi-generational paging behavior. As such, multiple calls to refresh() on this TestPager is illegal. The `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource` passed in to this `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager` will also be invalidated to prevent reuse of this pager for loads. However, other `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager` methods that does not invoke loads can still be called, such as `https://developer.android.com/reference/kotlin/androidx/paging/testing/TestPager#getLastLoadedPage()`. |