Removes use of deprecated runTestBlocking
Bug: 331594637
Flag: NA
Test: changed tests pass
Change-Id: Ide9cdbfe76d9709ba865d0330398bf8a708e38c4
diff --git a/tests/robotests/src/com/android/customization/model/picker/settings/data/repository/ColorContrastSectionRepositoryTest.kt b/tests/robotests/src/com/android/customization/model/picker/settings/data/repository/ColorContrastSectionRepositoryTest.kt
index 54b7c6e..fb9477a 100644
--- a/tests/robotests/src/com/android/customization/model/picker/settings/data/repository/ColorContrastSectionRepositoryTest.kt
+++ b/tests/robotests/src/com/android/customization/model/picker/settings/data/repository/ColorContrastSectionRepositoryTest.kt
@@ -24,8 +24,10 @@
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
-import kotlinx.coroutines.test.runBlockingTest
+import kotlinx.coroutines.test.UnconfinedTestDispatcher
+import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@@ -53,18 +55,21 @@
@OptIn(ExperimentalCoroutinesApi::class)
@Test
- fun contrastFlowEmitsValues() = runBlockingTest {
+ fun contrastFlowEmitsValues() = runTest {
val nextContrastValues = listOf(0.5f, 0.7f, 0.8f)
// Set up a flow to collect all contrast values
val flowCollector = mutableListOf<Float>()
- // Start collecting values from the flow
- val job = launch { underTest.contrast.collect { flowCollector.add(it) } }
+ // Start collecting values from the flow, using an unconfined dispatcher to start collecting
+ // from the flow right away (rather than explicitly calling `runCurrent`)
+ // See https://developer.android.com/kotlin/flow/test#continuous-collection
+ backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
+ underTest.contrast.toList(flowCollector)
+ }
nextContrastValues.forEach { uiModeManager.setContrast(it) }
// Ignore the first contrast value from constructing the repository
val collectedValues = flowCollector.drop(1)
assertThat(collectedValues).containsExactlyElementsIn(nextContrastValues)
- job.cancel()
}
}
diff --git a/tests/robotests/src/com/android/customization/model/picker/settings/domain/interactor/ColorContrastSectionInteractorTest.kt b/tests/robotests/src/com/android/customization/model/picker/settings/domain/interactor/ColorContrastSectionInteractorTest.kt
index 2319eed..afa6427 100644
--- a/tests/robotests/src/com/android/customization/model/picker/settings/domain/interactor/ColorContrastSectionInteractorTest.kt
+++ b/tests/robotests/src/com/android/customization/model/picker/settings/domain/interactor/ColorContrastSectionInteractorTest.kt
@@ -19,12 +19,12 @@
import androidx.test.filters.SmallTest
import com.android.customization.picker.settings.domain.interactor.ColorContrastSectionInteractor
import com.android.wallpaper.testing.FakeUiModeManager
+import com.google.common.truth.Truth.assertThat
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlinx.coroutines.flow.first
-import kotlinx.coroutines.test.runBlockingTest
-import org.junit.Assert.assertEquals
+import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@@ -46,12 +46,12 @@
}
@Test
- fun contrastEmitCorrectValuesFromRepository() = runBlockingTest {
+ fun contrastEmitCorrectValuesFromRepository() = runTest {
val expectedContrast = 1.5f
uiModeManager.setContrast(expectedContrast)
val result = interactor.contrast.first()
- assertEquals(expectedContrast, result)
+ assertThat(result).isEqualTo(expectedContrast)
}
}
diff --git a/tests/robotests/src/com/android/customization/model/picker/settings/ui/viewmodel/ColorContrastSectionViewModelTest.kt b/tests/robotests/src/com/android/customization/model/picker/settings/ui/viewmodel/ColorContrastSectionViewModelTest.kt
index c06f8a3..bf02273 100644
--- a/tests/robotests/src/com/android/customization/model/picker/settings/ui/viewmodel/ColorContrastSectionViewModelTest.kt
+++ b/tests/robotests/src/com/android/customization/model/picker/settings/ui/viewmodel/ColorContrastSectionViewModelTest.kt
@@ -27,7 +27,7 @@
import javax.inject.Inject
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
-import kotlinx.coroutines.test.runBlockingTest
+import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
@@ -52,7 +52,7 @@
}
@Test
- fun summaryEmitsCorrectDataValueForStandard() = runBlockingTest {
+ fun summaryEmitsCorrectDataValueForStandard() = runTest {
uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.STANDARD.value)
val expected =
ColorContrastSectionDataViewModel(
@@ -66,7 +66,7 @@
}
@Test
- fun summaryEmitsCorrectDataValueForMedium() = runBlockingTest {
+ fun summaryEmitsCorrectDataValueForMedium() = runTest {
uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.MEDIUM.value)
val expected =
ColorContrastSectionDataViewModel(
@@ -80,7 +80,7 @@
}
@Test
- fun summaryEmitsCorrectDataValueForHigh() = runBlockingTest {
+ fun summaryEmitsCorrectDataValueForHigh() = runTest {
uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.HIGH.value)
val expected =
ColorContrastSectionDataViewModel(
@@ -94,7 +94,7 @@
}
@Test(expected = IllegalArgumentException::class)
- fun summaryThrowsIllegalArgumentExceptionForInvalidValue() = runBlockingTest {
+ fun summaryThrowsIllegalArgumentExceptionForInvalidValue() = runTest {
uiModeManager.setContrast(999f)
viewModel.summary.collect() // This should throw an IllegalArgumentException