Skip to content
Closed
Show file tree
Hide file tree
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 @@ -25,7 +25,7 @@ import androidx.paging.LoadType.APPEND
import androidx.paging.LoadType.PREPEND
import androidx.paging.LoadType.REFRESH
import androidx.paging.internal.BUGANIZER_URL
import kotlinx.coroutines.CoroutineDispatcher
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.withContext

Expand All @@ -36,7 +36,7 @@ import kotlinx.coroutines.withContext
@OptIn(DelicateCoroutinesApi::class)
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class LegacyPagingSource<Key : Any, Value : Any>(
private val fetchDispatcher: CoroutineDispatcher,
private val fetchContext: CoroutineContext,
internal val dataSource: DataSource<Key, Value>
) : PagingSource<Key, Value>() {
private var pageSize: Int = PAGE_SIZE_NOT_SET
Expand Down Expand Up @@ -106,7 +106,7 @@ public class LegacyPagingSource<Key : Any, Value : Any>(
pageSize
)

return withContext(fetchDispatcher) {
return withContext(fetchContext) {
dataSource.load(dataSourceParams).run {
LoadResult.Page(
data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public abstract class PagedList<T : Any> internal constructor(
val fetchDispatcher = fetchDispatcher ?: Dispatchers.IO
val pagingSource = pagingSource ?: dataSource?.let { dataSource ->
LegacyPagingSource(
fetchDispatcher = fetchDispatcher,
fetchContext = fetchDispatcher,
dataSource = dataSource
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package androidx.paging

import androidx.paging.PagingSource.LoadResult.Page
import androidx.testutils.DirectDispatcher
import androidx.testutils.TestDispatcher
import com.google.common.truth.Truth.assertThat
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.flow.collectLatest
Expand Down Expand Up @@ -56,7 +56,7 @@ class LegacyPagingSourceTest {

@Test
fun init_invalidDataSource() {
val testDispatcher = DirectDispatcher
val testContext = EmptyCoroutineContext
val dataSource = object : DataSource<Int, Int>(KeyType.ITEM_KEYED) {
var isInvalidCalls = 0

Expand All @@ -74,7 +74,7 @@ class LegacyPagingSourceTest {
}

val pagingSource = LegacyPagingSource(
fetchDispatcher = testDispatcher,
fetchContext = testContext,
dataSource = dataSource,
)

Expand Down Expand Up @@ -111,7 +111,7 @@ class LegacyPagingSourceTest {
override fun getKey(item: String) = item.hashCode()
}
val pagingSource = LegacyPagingSource(
fetchDispatcher = Dispatchers.Unconfined,
fetchContext = Dispatchers.Unconfined,
dataSource
)

Expand Down Expand Up @@ -157,7 +157,7 @@ class LegacyPagingSourceTest {
}
}
val pagingSource = LegacyPagingSource(
fetchDispatcher = Dispatchers.Unconfined,
fetchContext = Dispatchers.Unconfined,
dataSource = dataSource
)

Expand All @@ -180,7 +180,7 @@ class LegacyPagingSourceTest {
@Test
fun positional() {
val pagingSource = LegacyPagingSource(
fetchDispatcher = Dispatchers.Unconfined,
fetchContext = Dispatchers.Unconfined,
dataSource = createTestPositionalDataSource()
)

Expand Down Expand Up @@ -233,7 +233,7 @@ class LegacyPagingSourceTest {
@Test
fun invalidateFromPagingSource() {
val pagingSource = LegacyPagingSource(
fetchDispatcher = Dispatchers.Unconfined,
fetchContext = Dispatchers.Unconfined,
dataSource = createTestPositionalDataSource()
)
val dataSource = pagingSource.dataSource
Expand All @@ -259,7 +259,7 @@ class LegacyPagingSourceTest {
@Test
fun invalidateFromDataSource() {
val pagingSource = LegacyPagingSource(
fetchDispatcher = Dispatchers.Unconfined,
fetchContext = Dispatchers.Unconfined,
dataSource = createTestPositionalDataSource()
)
val dataSource = pagingSource.dataSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.paging
import androidx.paging.LoadType.PREPEND
import androidx.paging.LoadType.REFRESH
import androidx.paging.PageEvent.Drop
import androidx.testutils.DirectDispatcher
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -290,7 +290,7 @@ class PageEventTest {
)
}

private val differ = TestPagingDataDiffer<String>(DirectDispatcher)
private val differ = TestPagingDataDiffer<String>(EmptyCoroutineContext)
private lateinit var pagingData: PagingData<String>

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import androidx.paging.LoadState.NotLoading
import androidx.paging.LoadType.PREPEND
import androidx.paging.PageEvent.Drop
import androidx.paging.PagingSource.LoadResult
import androidx.testutils.DirectDispatcher
import androidx.testutils.MainDispatcherRule
import androidx.testutils.TestDispatcher
import com.google.common.truth.Truth.assertThat
Expand Down Expand Up @@ -2039,11 +2038,6 @@ class PagingDataDifferTest(
}

private fun runTest(
scope: CoroutineScope = CoroutineScope(DirectDispatcher),
differ: SimpleDiffer = SimpleDiffer(
differCallback = dummyDifferCallback,
coroutineScope = scope,
),
loadDispatcher: TestDispatcher = TestDispatcher(),
initialKey: Int? = null,
pagingSources: MutableList<TestPagingSource> = mutableListOf(),
Expand All @@ -2065,11 +2059,15 @@ class PagingDataDifferTest(
uiReceivers: List<TrackableUiReceiverWrapper>,
hintReceivers: List<TrackableHintReceiverWrapper>
) -> Unit
) {
) = testScope.runTest {
val differ = SimpleDiffer(
differCallback = dummyDifferCallback,
coroutineScope = this,
)
val uiReceivers = mutableListOf<TrackableUiReceiverWrapper>()
val hintReceivers = mutableListOf<TrackableHintReceiverWrapper>()

val collection = scope.launch {
val collection = launch {
pager.flow
.map { pagingData ->
PagingData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package androidx.paging

import kotlinx.coroutines.CoroutineDispatcher
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.Dispatchers

class TestPagingDataDiffer<T : Any>(mainDispatcher: CoroutineDispatcher = Dispatchers.Main) :
PagingDataDiffer<T>(noopDifferCallback, mainDispatcher) {
class TestPagingDataDiffer<T : Any>(mainContext: CoroutineContext = Dispatchers.Main) :
PagingDataDiffer<T>(noopDifferCallback, mainContext) {

val currentList: List<T> get() = List(size) { i -> get(i)!! }

Expand Down