# 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)

*** ** * ** ***


```
@VisibleForTesting
public final class TestPager<Key extends Object, Value extends Object>
```

<br />

*** ** * ** ***

A fake `https://developer.android.com/reference/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/androidx/paging/LoadType#REFRESH`, the first load operation of the `https://developer.android.com/reference/androidx/paging/testing/TestPager` must be a call to `https://developer.android.com/reference/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/androidx/paging/testing/TestPager` by supplying a new instance of `https://developer.android.com/reference/androidx/paging/PagingSource`.

## Summary

| ### Public constructors |
|---|
| `<Key extends https://developer.android.com/reference/java/lang/Object.html, Value extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/paging/testing/TestPager#TestPager(androidx.paging.PagingConfig,androidx.paging.PagingSource)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingConfig config, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingSource<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value> pagingSource )` |

| ### Public methods |
|---|---|
| `final https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>` | `https://developer.android.com/reference/androidx/paging/testing/TestPager#append()()` Performs a load of `https://developer.android.com/reference/androidx/paging/LoadType#APPEND` on the PagingSource. |
| `final https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Page<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>` | `https://developer.android.com/reference/androidx/paging/testing/TestPager#getLastLoadedPage()()` Returns the most recent `https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Page` loaded from the `https://developer.android.com/reference/androidx/paging/PagingSource`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Page<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>>` | `https://developer.android.com/reference/androidx/paging/testing/TestPager#getPages()()` Returns the current list of `https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Page` loaded so far from the `https://developer.android.com/reference/androidx/paging/PagingSource`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingState<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>` | `https://developer.android.com/reference/androidx/paging/testing/TestPager#getPagingState(kotlin.Int)(int anchorPosition)` Returns a `https://developer.android.com/reference/androidx/paging/PagingState` to generate a `https://developer.android.com/reference/androidx/paging/PagingSource.LoadParams#key()` by supplying it to `https://developer.android.com/reference/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingState<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>` | `https://developer.android.com/reference/androidx/paging/testing/TestPager#getPagingState(kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull item, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> anchorPositionLookup )` Returns a `https://developer.android.com/reference/androidx/paging/PagingState` to generate a `https://developer.android.com/reference/androidx/paging/PagingSource.LoadParams#key()` by supplying it to `https://developer.android.com/reference/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)`. |
| `final https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>` | `https://developer.android.com/reference/androidx/paging/testing/TestPager#prepend()()` Performs a load of `https://developer.android.com/reference/androidx/paging/LoadType#PREPEND` on the PagingSource. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>` | `https://developer.android.com/reference/androidx/paging/testing/TestPager#refresh(kotlin.Any)(Key initialKey)` Performs a load of `https://developer.android.com/reference/androidx/paging/LoadType#REFRESH` on the PagingSource. |

## Public constructors

### TestPager

```
public <Key extends Object, Value extends Object> TestPager(
    @NonNull PagingConfig config,
    @NonNull PagingSource<@NonNull Key, @NonNull Value> pagingSource
)
```

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

## Public methods

### append

```
public final PagingSource.LoadResult<@NonNull Key, @NonNull Value> append()
```

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

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

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

Returns the `https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult` from calling `https://developer.android.com/reference/androidx/paging/PagingSource#load(androidx.paging.PagingSource.LoadParams)`. If the `https://developer.android.com/reference/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

```
public final PagingSource.LoadResult.Page<@NonNull Key, @NonNull Value> getLastLoadedPage()
```

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

### getPages

```
public final @NonNull List<@NonNull PagingSource.LoadResult.Page<@NonNull Key, @NonNull Value>> getPages()
```

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

### getPagingState

```
public final @NonNull PagingState<@NonNull Key, @NonNull Value> getPagingState(int anchorPosition)
```

Returns a `https://developer.android.com/reference/androidx/paging/PagingState` to generate a `https://developer.android.com/reference/androidx/paging/PagingSource.LoadParams#key()` by supplying it to `https://developer.android.com/reference/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)`. The key returned from `https://developer.android.com/reference/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)` should be used as the `https://developer.android.com/reference/androidx/paging/PagingSource.LoadParams.Refresh#key()` when calling `https://developer.android.com/reference/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/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/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/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/androidx/paging/PagingState#anchorPosition()` may not exactly match the `https://developer.android.com/reference/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/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/androidx/paging/PagingState#anchorPosition()`, anchorPosition 0 references item0. The `https://developer.android.com/reference/androidx/paging/PagingSource` is expected to handle this correctly within `https://developer.android.com/reference/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)` when `https://developer.android.com/reference/androidx/paging/PagingConfig#enablePlaceholders()` = false.

| Parameters |
|---|---|
| `int anchorPosition` | 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/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

```
public final @NonNull PagingState<@NonNull Key, @NonNull Value> getPagingState(
    @NonNull Function1<@NonNull item, @NonNull Boolean> anchorPositionLookup
)
```

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

The `https://developer.android.com/reference/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/androidx/paging/testing/TestPager#getPagingState(kotlin.Function1)` function instead. Since the `https://developer.android.com/reference/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/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/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/androidx/paging/PagingState#anchorPosition()`, anchorPosition 0 references item0. The `https://developer.android.com/reference/androidx/paging/PagingSource` is expected to handle this correctly within `https://developer.android.com/reference/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)` when `https://developer.android.com/reference/androidx/paging/PagingConfig#enablePlaceholders()` = false.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull item, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> anchorPositionLookup` | the predicate to match with an item which will serve as the basis for generating the `https://developer.android.com/reference/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

```
public final PagingSource.LoadResult<@NonNull Key, @NonNull Value> prepend()
```

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

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

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

Returns the `https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult` from calling `https://developer.android.com/reference/androidx/paging/PagingSource#load(androidx.paging.PagingSource.LoadParams)`. If the `https://developer.android.com/reference/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

```
public final @NonNull PagingSource.LoadResult<@NonNull Key, @NonNull Value> refresh(Key initialKey)
```

Performs a load of `https://developer.android.com/reference/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/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/androidx/paging/testing/TestPager#append()` or `https://developer.android.com/reference/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/androidx/paging/testing/TestPager#getLastLoadedPage()` before any load operations.

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

| Parameters |
|---|---|
| `Key initialKey` | the `https://developer.android.com/reference/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/androidx/paging/PagingSource` passed in to this `https://developer.android.com/reference/androidx/paging/testing/TestPager` will also be invalidated to prevent reuse of this pager for loads. However, other `https://developer.android.com/reference/androidx/paging/testing/TestPager` methods that does not invoke loads can still be called, such as `https://developer.android.com/reference/androidx/paging/testing/TestPager#getLastLoadedPage()`. |