Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 0905d0e

Browse files
authored
Merge pull request #1049 from android/paging-test-coroutine-1.6
Update samples to kotlinx.coroutines.test 1.6
2 parents 79e64a6 + c4dfa15 commit 0905d0e

File tree

22 files changed

+79
-89
lines changed

22 files changed

+79
-89
lines changed

BasicRxJavaSample/versions.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ versions.benchmark = "1.1.0-alpha01"
3535
versions.cardview = "1.0.0"
3636
versions.constraint_layout = "2.0.0-alpha2"
3737
versions.core_ktx = "1.1.0"
38-
versions.coroutines = "1.4.1"
38+
versions.coroutines = "1.6.0"
3939
versions.dagger = "2.16"
4040
versions.dexmaker = "2.2.0"
4141
versions.espresso = "3.2.0"
4242
versions.fragment = "1.2.0"
4343
versions.glide = "4.8.0"
4444
versions.hamcrest = "1.3"
4545
versions.junit = "4.12"
46-
versions.kotlin = "1.4.21"
46+
versions.kotlin = "1.6.10"
4747
versions.lifecycle = "2.2.0"
4848
versions.material = "1.0.0"
4949
versions.mockito = "2.25.0"

BasicRxJavaSampleKotlin/versions.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ versions.benchmark = "1.1.0-alpha01"
3535
versions.cardview = "1.0.0"
3636
versions.constraint_layout = "2.0.0-alpha2"
3737
versions.core_ktx = "1.1.0"
38-
versions.coroutines = "1.4.1"
38+
versions.coroutines = "1.6.0"
3939
versions.dagger = "2.16"
4040
versions.dexmaker = "2.2.0"
4141
versions.espresso = "3.2.0"
4242
versions.fragment = "1.2.0"
4343
versions.glide = "4.8.0"
4444
versions.hamcrest = "1.3"
4545
versions.junit = "4.12"
46-
versions.kotlin = "1.4.21"
46+
versions.kotlin = "1.6.10"
4747
versions.lifecycle = "2.2.0"
4848
versions.material = "1.0.0"
4949
versions.mockito = "2.25.0"

BasicSample/versions.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ versions.benchmark = "1.1.0-alpha01"
3535
versions.cardview = "1.0.0"
3636
versions.constraint_layout = "2.0.0-alpha2"
3737
versions.core_ktx = "1.1.0"
38-
versions.coroutines = "1.4.1"
38+
versions.coroutines = "1.6.0"
3939
versions.dagger = "2.16"
4040
versions.dexmaker = "2.2.0"
4141
versions.espresso = "3.2.0"
4242
versions.fragment = "1.2.0"
4343
versions.glide = "4.8.0"
4444
versions.hamcrest = "1.3"
4545
versions.junit = "4.12"
46-
versions.kotlin = "1.4.21"
46+
versions.kotlin = "1.6.10"
4747
versions.lifecycle = "2.2.0"
4848
versions.material = "1.0.0"
4949
versions.mockito = "2.25.0"

GithubBrowserSample/versions.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ versions.benchmark = "1.1.0-alpha01"
3535
versions.cardview = "1.0.0"
3636
versions.constraint_layout = "2.0.0-alpha2"
3737
versions.core_ktx = "1.1.0"
38-
versions.coroutines = "1.4.1"
38+
versions.coroutines = "1.6.0"
3939
versions.dagger = "2.16"
4040
versions.dexmaker = "2.2.0"
4141
versions.espresso = "3.2.0"
4242
versions.fragment = "1.2.0"
4343
versions.glide = "4.8.0"
4444
versions.hamcrest = "1.3"
4545
versions.junit = "4.12"
46-
versions.kotlin = "1.4.21"
46+
versions.kotlin = "1.6.10"
4747
versions.lifecycle = "2.2.0"
4848
versions.material = "1.0.0"
4949
versions.mockito = "2.25.0"

LiveDataSample/app/src/test/java/com/android/example/livedatabuilder/LiveDataViewModelTest.kt

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import com.android.example.livedatabuilder.util.MainCoroutineRule
2323
import com.android.example.livedatabuilder.util.getOrAwaitValue
2424
import com.android.example.livedatabuilder.util.observeForTesting
2525
import kotlinx.coroutines.ExperimentalCoroutinesApi
26-
import kotlinx.coroutines.test.TestCoroutineDispatcher
26+
import kotlinx.coroutines.test.TestDispatcher
27+
import kotlinx.coroutines.test.advanceUntilIdle
28+
import kotlinx.coroutines.test.runCurrent
29+
import kotlinx.coroutines.test.runTest
30+
import kotlinx.coroutines.yield
2731
import org.junit.Assert.assertEquals
2832
import org.junit.Assert.assertNotEquals
2933
import org.junit.Assert.assertTrue
@@ -49,7 +53,7 @@ class LiveDataViewModelTest {
4953
var mainCoroutineRule = MainCoroutineRule()
5054

5155
// Use a Fake DataSource so we have all necessary control over it
52-
private val fakeDataSource = FakeDataSource()
56+
private val fakeDataSource = FakeDataSource(mainCoroutineRule.testDispatcher)
5357

5458
// Class under test. Uses Dispatchers.Main so that the MainCoroutineRule can control it.
5559
private lateinit var viewModel: LiveDataViewModel
@@ -68,54 +72,56 @@ class LiveDataViewModelTest {
6872
}
6973

7074
@Test
71-
fun currentTimeTransformed() {
75+
fun currentTimeTransformed() = runTest {
7276
// Get the result of a coroutine inside a transformation
7377
val timeTransformed = viewModel.currentTimeTransformed.getOrAwaitValue {
7478
// After observing, advance the clock to avoid the delay calls.
75-
mainCoroutineRule.advanceUntilIdle()
79+
advanceUntilIdle()
7680
}
77-
assertEquals(timeTransformed, Date(FakeDataSource.CURRENT_TIME).toString())
81+
assertEquals(Date(FakeDataSource.CURRENT_TIME).toString(), timeTransformed)
7882
}
7983

8084
@Test
81-
fun getCurrentWeather_loading() {
82-
// Start with a paused dispatcher in the FakeDataSource
83-
fakeDataSource.testDispatcher.pauseDispatcher()
84-
85+
fun getCurrentWeather_loading() = runTest {
8586
// Keep observing currentWeather
8687
viewModel.currentWeather.observeForTesting {
88+
// Yield test thread so that the first LiveData emission can complete
89+
yield()
8790

8891
// Verify that the first value is Loading
89-
assertEquals(viewModel.currentWeather.value, LiveDataViewModel.LOADING_STRING)
92+
assertEquals(LiveDataViewModel.LOADING_STRING, viewModel.currentWeather.value)
9093

91-
// Resume fake dispatcher so it emits a new value
92-
fakeDataSource.testDispatcher.resumeDispatcher()
94+
// Execute all pending coroutines in the viewModel
95+
runCurrent()
9396

9497
// Verify the new value is available
95-
assertEquals(viewModel.currentWeather.value, FakeDataSource.WEATHER_CONDITION)
98+
assertEquals(FakeDataSource.WEATHER_CONDITION, viewModel.currentWeather.value)
9699
}
97100
}
98101

99102
@Test
100-
fun cache_RefreshFromViewModelScope() {
103+
fun cache_RefreshFromViewModelScope() = runTest {
101104
// Get the initial value that comes directly from FakeDataSource
102105
val initialValue = viewModel.cachedValue.getOrAwaitValue()
103106

104107
// Trigger an update, which starts a coroutine that updates the value
105108
viewModel.onRefresh()
106109

110+
// Run pending coroutine in ViewModel
111+
runCurrent()
112+
107113
// Get the new value
108114
val valueAfterRefresh = viewModel.cachedValue.getOrAwaitValue()
109115

110116
// Assert they are different values
111117
assertNotEquals(initialValue, valueAfterRefresh)
112-
assertEquals(initialValue, FakeDataSource.CURRENT_VALUE)
113-
assertEquals(valueAfterRefresh, FakeDataSource.NEW_VALUE)
118+
assertEquals(FakeDataSource.CURRENT_VALUE, initialValue)
119+
assertEquals(FakeDataSource.NEW_VALUE, valueAfterRefresh)
114120
}
115121
}
116122

117123
@ExperimentalCoroutinesApi
118-
class FakeDataSource : DataSource {
124+
class FakeDataSource(private val testDispatcher: TestDispatcher) : DataSource {
119125

120126
companion object {
121127
const val CURRENT_VALUE = "test"
@@ -129,7 +135,6 @@ class FakeDataSource : DataSource {
129135

130136
override fun getCurrentTime(): LiveData<Long> = MutableLiveData<Long>(CURRENT_TIME)
131137

132-
val testDispatcher = TestCoroutineDispatcher()
133138
override fun fetchWeather(): LiveData<String> = liveData(testDispatcher) {
134139
emit(WEATHER_CONDITION)
135140
}

LiveDataSample/app/src/test/java/com/android/example/livedatabuilder/util/LiveDataTestUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fun <T> LiveData<T>.getOrAwaitValue(
5858
/**
5959
* Observes a [LiveData] until the `block` is done executing.
6060
*/
61-
fun <T> LiveData<T>.observeForTesting(block: () -> Unit) {
61+
suspend fun <T> LiveData<T>.observeForTesting(block: suspend () -> Unit) {
6262
val observer = Observer<T> { }
6363
try {
6464
observeForever(observer)

LiveDataSample/app/src/test/java/com/android/example/livedatabuilder/util/MainCoroutineRule.kt

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,17 @@
1616

1717
package com.android.example.livedatabuilder.util
1818

19-
import kotlinx.coroutines.CoroutineDispatcher
2019
import kotlinx.coroutines.Dispatchers
2120
import kotlinx.coroutines.ExperimentalCoroutinesApi
22-
import kotlinx.coroutines.test.TestCoroutineScope
21+
import kotlinx.coroutines.test.StandardTestDispatcher
2322
import kotlinx.coroutines.test.resetMain
2423
import kotlinx.coroutines.test.setMain
2524
import org.junit.rules.TestWatcher
2625
import org.junit.runner.Description
27-
import kotlin.coroutines.ContinuationInterceptor
2826

2927
/**
30-
* Sets the main coroutines dispatcher to a [TestCoroutineScope] for unit testing. A
31-
* [TestCoroutineScope] provides control over the execution of coroutines.
28+
* Sets the main coroutines dispatcher to a [StandardTestDispatcher] for unit testing. A
29+
* [StandardTestDispatcher] provides control over the execution of coroutines.
3230
*
3331
* Declare it as a JUnit Rule:
3432
*
@@ -37,24 +35,16 @@ import kotlin.coroutines.ContinuationInterceptor
3735
* var mainCoroutineRule = MainCoroutineRule()
3836
* ```
3937
*
40-
* Use it directly as a [TestCoroutineScope]:
41-
*
42-
* ```
43-
* mainCoroutineRule.pauseDispatcher()
44-
* ...
45-
* mainCoroutineRule.resumeDispatcher()
46-
* ...
47-
* mainCoroutineRule.runBlockingTest { }
48-
* ...
49-
*
50-
* ```
38+
* Then, use `runTest` to execute your tests.
5139
*/
5240
@ExperimentalCoroutinesApi
53-
class MainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
41+
class MainCoroutineRule : TestWatcher() {
42+
43+
val testDispatcher = StandardTestDispatcher()
5444

5545
override fun starting(description: Description?) {
5646
super.starting(description)
57-
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
47+
Dispatchers.setMain(testDispatcher)
5848
}
5949

6050
override fun finished(description: Description?) {

LiveDataSample/versions.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ versions.benchmark = "1.1.0-alpha01"
3535
versions.cardview = "1.0.0"
3636
versions.constraint_layout = "2.0.0-alpha2"
3737
versions.core_ktx = "1.1.0"
38-
versions.coroutines = "1.4.1"
38+
versions.coroutines = "1.6.0"
3939
versions.dagger = "2.16"
4040
versions.dexmaker = "2.2.0"
4141
versions.espresso = "3.2.0"
4242
versions.fragment = "1.2.0"
4343
versions.glide = "4.8.0"
4444
versions.hamcrest = "1.3"
4545
versions.junit = "4.12"
46-
versions.kotlin = "1.4.21"
46+
versions.kotlin = "1.6.10"
4747
versions.lifecycle = "2.2.0"
4848
versions.material = "1.0.0"
4949
versions.mockito = "2.25.0"

NavigationAdvancedSample/versions.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ versions.benchmark = "1.1.0-alpha01"
3535
versions.cardview = "1.0.0"
3636
versions.constraint_layout = "2.0.0-alpha2"
3737
versions.core_ktx = "1.1.0"
38-
versions.coroutines = "1.4.1"
38+
versions.coroutines = "1.6.0"
3939
versions.dagger = "2.16"
4040
versions.dexmaker = "2.2.0"
4141
versions.espresso = "3.2.0"
4242
versions.fragment = "1.2.0"
4343
versions.glide = "4.8.0"
4444
versions.hamcrest = "1.3"
4545
versions.junit = "4.12"
46-
versions.kotlin = "1.4.21"
46+
versions.kotlin = "1.6.10"
4747
versions.lifecycle = "2.2.0"
4848
versions.material = "1.0.0"
4949
versions.mockito = "2.25.0"

NavigationBasicSample/versions.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ versions.benchmark = "1.1.0-alpha01"
3535
versions.cardview = "1.0.0"
3636
versions.constraint_layout = "2.0.0-alpha2"
3737
versions.core_ktx = "1.1.0"
38-
versions.coroutines = "1.4.1"
38+
versions.coroutines = "1.6.0"
3939
versions.dagger = "2.16"
4040
versions.dexmaker = "2.2.0"
4141
versions.espresso = "3.2.0"
4242
versions.fragment = "1.2.0"
4343
versions.glide = "4.8.0"
4444
versions.hamcrest = "1.3"
4545
versions.junit = "4.12"
46-
versions.kotlin = "1.4.21"
46+
versions.kotlin = "1.6.10"
4747
versions.lifecycle = "2.2.0"
4848
versions.material = "1.0.0"
4949
versions.mockito = "2.25.0"

0 commit comments

Comments
 (0)