Loading tests/robotests/src/com/android/customization/model/picker/settings/data/repository/ColorContrastSectionRepositoryTest.kt +19 −15 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ import javax.inject.Inject import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.runTest import org.junit.Before Loading @@ -37,11 +38,13 @@ import org.robolectric.RobolectricTestRunner @HiltAndroidTest @SmallTest @RunWith(RobolectricTestRunner::class) @OptIn(ExperimentalCoroutinesApi::class) class ColorContrastSectionRepositoryTest { @get:Rule var hiltRule = HiltAndroidRule(this) @Inject lateinit var uiModeManager: FakeUiModeManager @Inject lateinit var underTest: ColorContrastSectionRepository @Inject lateinit var testScope: TestScope @Before fun setUp() { Loading @@ -55,14 +58,15 @@ class ColorContrastSectionRepositoryTest { @OptIn(ExperimentalCoroutinesApi::class) @Test fun contrastFlowEmitsValues() = runTest { fun contrastFlowEmitsValues() = testScope.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, using an unconfined dispatcher to start collecting // from the flow right away (rather than explicitly calling `runCurrent`) // 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)) { backgroundScope.launch(UnconfinedTestDispatcher()) { underTest.contrast.toList(flowCollector) } Loading tests/robotests/src/com/android/customization/model/picker/settings/ui/viewmodel/ColorContrastSectionViewModelTest.kt +7 −0 Original line number Diff line number Diff line Loading @@ -25,9 +25,13 @@ import com.android.wallpaper.testing.FakeUiModeManager import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest import javax.inject.Inject import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.TestDispatcher import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.setMain import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Rule Loading @@ -42,12 +46,15 @@ class ColorContrastSectionViewModelTest { private lateinit var viewModel: ColorContrastSectionViewModel @Inject lateinit var testDispatcher: TestDispatcher @Inject lateinit var uiModeManager: FakeUiModeManager @Inject lateinit var viewModelFactory: ColorContrastSectionViewModel.Factory @OptIn(ExperimentalCoroutinesApi::class) @Before fun setUp() { hiltRule.inject() Dispatchers.setMain(testDispatcher) viewModel = viewModelFactory.create(ColorContrastSectionViewModel::class.java) } Loading Loading
tests/robotests/src/com/android/customization/model/picker/settings/data/repository/ColorContrastSectionRepositoryTest.kt +19 −15 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ import javax.inject.Inject import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.runTest import org.junit.Before Loading @@ -37,11 +38,13 @@ import org.robolectric.RobolectricTestRunner @HiltAndroidTest @SmallTest @RunWith(RobolectricTestRunner::class) @OptIn(ExperimentalCoroutinesApi::class) class ColorContrastSectionRepositoryTest { @get:Rule var hiltRule = HiltAndroidRule(this) @Inject lateinit var uiModeManager: FakeUiModeManager @Inject lateinit var underTest: ColorContrastSectionRepository @Inject lateinit var testScope: TestScope @Before fun setUp() { Loading @@ -55,14 +58,15 @@ class ColorContrastSectionRepositoryTest { @OptIn(ExperimentalCoroutinesApi::class) @Test fun contrastFlowEmitsValues() = runTest { fun contrastFlowEmitsValues() = testScope.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, using an unconfined dispatcher to start collecting // from the flow right away (rather than explicitly calling `runCurrent`) // 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)) { backgroundScope.launch(UnconfinedTestDispatcher()) { underTest.contrast.toList(flowCollector) } Loading
tests/robotests/src/com/android/customization/model/picker/settings/ui/viewmodel/ColorContrastSectionViewModelTest.kt +7 −0 Original line number Diff line number Diff line Loading @@ -25,9 +25,13 @@ import com.android.wallpaper.testing.FakeUiModeManager import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest import javax.inject.Inject import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.TestDispatcher import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.setMain import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Rule Loading @@ -42,12 +46,15 @@ class ColorContrastSectionViewModelTest { private lateinit var viewModel: ColorContrastSectionViewModel @Inject lateinit var testDispatcher: TestDispatcher @Inject lateinit var uiModeManager: FakeUiModeManager @Inject lateinit var viewModelFactory: ColorContrastSectionViewModel.Factory @OptIn(ExperimentalCoroutinesApi::class) @Before fun setUp() { hiltRule.inject() Dispatchers.setMain(testDispatcher) viewModel = viewModelFactory.create(ColorContrastSectionViewModel::class.java) } Loading