Skip to content

Commit d7a8fcd

Browse files
committed
Use existing TestScope instead of creating a new one
1 parent 7174fbf commit d7a8fcd

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

paging/paging-common/src/test/kotlin/androidx/paging/PagingDataDifferTest.kt

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import kotlin.test.assertFalse
3131
import kotlin.test.assertNull
3232
import kotlin.test.assertTrue
3333
import kotlinx.coroutines.CoroutineDispatcher
34-
import kotlinx.coroutines.CoroutineScope
3534
import kotlinx.coroutines.ExperimentalCoroutinesApi
3635
import kotlinx.coroutines.Job
3736
import kotlinx.coroutines.async
@@ -1465,7 +1464,7 @@ class PagingDataDifferTest(
14651464
@Test
14661465
fun refresh_loadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
14671466
pagingSources, _, _ ->
1468-
val collectLoadStates = differ.collectLoadStates()
1467+
val collectLoadStates = launch { differ.collectLoadStates() }
14691468

14701469
// execute queued initial REFRESH
14711470
loadDispatcher.scheduler.advanceUntilIdle()
@@ -1498,7 +1497,7 @@ class PagingDataDifferTest(
14981497
differ.addLoadStateListener {
14991498
loadStateCallbacks.add(it)
15001499
}
1501-
val collectLoadStates = differ.collectLoadStates()
1500+
val collectLoadStates = launch { differ.collectLoadStates() }
15021501
// execute initial refresh
15031502
loadDispatcher.scheduler.advanceUntilIdle()
15041503
assertThat(differ.snapshot()).containsExactlyElementsIn(0 until 9)
@@ -1545,7 +1544,7 @@ class PagingDataDifferTest(
15451544

15461545
@Test
15471546
fun appendInvalid_loadStates() = runTest { differ, loadDispatcher, pagingSources, _, _ ->
1548-
val collectLoadStates = differ.collectLoadStates()
1547+
val collectLoadStates = launch { differ.collectLoadStates() }
15491548

15501549
// initial REFRESH
15511550
loadDispatcher.scheduler.advanceUntilIdle()
@@ -1607,7 +1606,7 @@ class PagingDataDifferTest(
16071606
@Test
16081607
fun prependInvalid_loadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
16091608
pagingSources, _, _ ->
1610-
val collectLoadStates = differ.collectLoadStates()
1609+
val collectLoadStates = launch { differ.collectLoadStates() }
16111610

16121611
// initial REFRESH
16131612
loadDispatcher.scheduler.advanceUntilIdle()
@@ -1661,7 +1660,7 @@ class PagingDataDifferTest(
16611660
@Test
16621661
fun refreshInvalid_loadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
16631662
pagingSources, _, _ ->
1664-
val collectLoadStates = differ.collectLoadStates()
1663+
val collectLoadStates = launch { differ.collectLoadStates() }
16651664

16661665
// execute queued initial REFRESH load which will return LoadResult.Invalid()
16671666
pagingSources[0].nextLoadResult = LoadResult.Invalid()
@@ -1691,7 +1690,7 @@ class PagingDataDifferTest(
16911690

16921691
@Test
16931692
fun appendError_retryLoadStates() = runTest { differ, loadDispatcher, pagingSources, _, _ ->
1694-
val collectLoadStates = differ.collectLoadStates()
1693+
val collectLoadStates = launch { differ.collectLoadStates() }
16951694

16961695
// initial REFRESH
16971696
loadDispatcher.scheduler.advanceUntilIdle()
@@ -1742,7 +1741,7 @@ class PagingDataDifferTest(
17421741
@Test
17431742
fun prependError_retryLoadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
17441743
pagingSources, _, _ ->
1745-
val collectLoadStates = differ.collectLoadStates()
1744+
val collectLoadStates = launch { differ.collectLoadStates() }
17461745

17471746
// initial REFRESH
17481747
loadDispatcher.scheduler.advanceUntilIdle()
@@ -1784,7 +1783,7 @@ class PagingDataDifferTest(
17841783

17851784
@Test
17861785
fun refreshError_retryLoadStates() = runTest { differ, loadDispatcher, pagingSources, _, _ ->
1787-
val collectLoadStates = differ.collectLoadStates()
1786+
val collectLoadStates = launch { differ.collectLoadStates() }
17881787

17891788
// initial load returns LoadResult.Error
17901789
val exception = Throwable()
@@ -1817,7 +1816,7 @@ class PagingDataDifferTest(
18171816
@Test
18181817
fun prependError_refreshLoadStates() = runTest(initialKey = 50) { differ, loadDispatcher,
18191818
pagingSources, _, _ ->
1820-
val collectLoadStates = differ.collectLoadStates()
1819+
val collectLoadStates = launch { differ.collectLoadStates() }
18211820

18221821
// initial REFRESH
18231822
loadDispatcher.scheduler.advanceUntilIdle()
@@ -1861,7 +1860,7 @@ class PagingDataDifferTest(
18611860
@Test
18621861
fun refreshError_refreshLoadStates() = runTest { differ, loadDispatcher, pagingSources,
18631862
_, _ ->
1864-
val collectLoadStates = differ.collectLoadStates()
1863+
val collectLoadStates = launch { differ.collectLoadStates() }
18651864

18661865
// the initial load will return LoadResult.Error
18671866
val exception = Throwable()
@@ -1903,7 +1902,7 @@ class PagingDataDifferTest(
19031902
TestPagingSource(loadDelay = 500, items = emptyList())
19041903
}
19051904

1906-
val collectLoadStates = differ.collectLoadStates()
1905+
val collectLoadStates = launch { differ.collectLoadStates() }
19071906
val job = launch {
19081907
pager.flow.collectLatest {
19091908
differ.collectFrom(it)
@@ -2000,9 +1999,8 @@ class PagingDataDifferTest(
20001999

20012000
val differ = SimpleDiffer(
20022001
differCallback = dummyDifferCallback,
2003-
coroutineScope = backgroundScope,
20042002
)
2005-
differ.collectLoadStates()
2003+
backgroundScope.launch { differ.collectLoadStates() }
20062004

20072005
val job = launch {
20082006
pager.collectLatest {
@@ -2019,9 +2017,8 @@ class PagingDataDifferTest(
20192017
// we start a separate differ to recollect on cached Pager.flow
20202018
val differ2 = SimpleDiffer(
20212019
differCallback = dummyDifferCallback,
2022-
coroutineScope = backgroundScope,
20232020
)
2024-
differ2.collectLoadStates()
2021+
backgroundScope.launch { differ2.collectLoadStates() }
20252022

20262023
val job2 = launch {
20272024
pager.collectLatest {
@@ -2199,7 +2196,7 @@ class PagingDataDifferTest(
21992196
).also { pagingSources.add(it) }
22002197
}
22012198
),
2202-
block: (
2199+
block: TestScope.(
22032200
differ: SimpleDiffer,
22042201
loadDispatcher: TestDispatcher,
22052202
pagingSources: List<TestPagingSource>,
@@ -2209,7 +2206,6 @@ class PagingDataDifferTest(
22092206
) = testScope.runTest {
22102207
val differ = SimpleDiffer(
22112208
differCallback = dummyDifferCallback,
2212-
coroutineScope = this,
22132209
)
22142210
val uiReceivers = mutableListOf<TrackableUiReceiverWrapper>()
22152211
val hintReceivers = mutableListOf<TrackableHintReceiverWrapper>()
@@ -2345,11 +2341,9 @@ private class TrackableHintReceiverWrapper(
23452341
}
23462342
}
23472343

2348-
@OptIn(ExperimentalCoroutinesApi::class)
23492344
private class SimpleDiffer(
23502345
differCallback: DifferCallback,
23512346
cachedPagingData: PagingData<Int>? = null,
2352-
val coroutineScope: CoroutineScope = TestScope(UnconfinedTestDispatcher())
23532347
) : PagingDataDiffer<Int>(differCallback = differCallback, cachedPagingData = cachedPagingData) {
23542348
override suspend fun presentNewList(
23552349
previousList: NullPaddedList<Int>,
@@ -2371,11 +2365,9 @@ private class SimpleDiffer(
23712365
return newCombinedLoadStates
23722366
}
23732367

2374-
fun collectLoadStates(): Job {
2375-
return coroutineScope.launch {
2376-
nonNullLoadStateFlow.collect { combinedLoadStates ->
2377-
_localLoadStates.add(combinedLoadStates)
2378-
}
2368+
suspend fun collectLoadStates() {
2369+
nonNullLoadStateFlow.collect { combinedLoadStates ->
2370+
_localLoadStates.add(combinedLoadStates)
23792371
}
23802372
}
23812373
}

0 commit comments

Comments
 (0)