Loading tests/robotests/src/com/android/customization/model/picker/settings/data/repository/ColorContrastSectionRepositoryTest.kt +10 −5 Original line number Diff line number Diff line Loading @@ -24,8 +24,10 @@ import dagger.hilt.android.testing.HiltAndroidRule 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 Loading Loading @@ -53,18 +55,21 @@ class ColorContrastSectionRepositoryTest { @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() } } tests/robotests/src/com/android/customization/model/picker/settings/domain/interactor/ColorContrastSectionInteractorTest.kt +4 −4 Original line number Diff line number Diff line Loading @@ -19,12 +19,12 @@ package com.android.customization.model.picker.settings.domain.interactor 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 Loading @@ -46,12 +46,12 @@ class ColorContrastSectionInteractorTest { } @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) } } tests/robotests/src/com/android/customization/model/picker/settings/ui/viewmodel/ColorContrastSectionViewModelTest.kt +5 −5 Original line number Diff line number Diff line Loading @@ -27,7 +27,7 @@ import dagger.hilt.android.testing.HiltAndroidTest 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 Loading @@ -52,7 +52,7 @@ class ColorContrastSectionViewModelTest { } @Test fun summaryEmitsCorrectDataValueForStandard() = runBlockingTest { fun summaryEmitsCorrectDataValueForStandard() = runTest { uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.STANDARD.value) val expected = ColorContrastSectionDataViewModel( Loading @@ -66,7 +66,7 @@ class ColorContrastSectionViewModelTest { } @Test fun summaryEmitsCorrectDataValueForMedium() = runBlockingTest { fun summaryEmitsCorrectDataValueForMedium() = runTest { uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.MEDIUM.value) val expected = ColorContrastSectionDataViewModel( Loading @@ -80,7 +80,7 @@ class ColorContrastSectionViewModelTest { } @Test fun summaryEmitsCorrectDataValueForHigh() = runBlockingTest { fun summaryEmitsCorrectDataValueForHigh() = runTest { uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.HIGH.value) val expected = ColorContrastSectionDataViewModel( Loading @@ -94,7 +94,7 @@ class ColorContrastSectionViewModelTest { } @Test(expected = IllegalArgumentException::class) fun summaryThrowsIllegalArgumentExceptionForInvalidValue() = runBlockingTest { fun summaryThrowsIllegalArgumentExceptionForInvalidValue() = runTest { uiModeManager.setContrast(999f) viewModel.summary.collect() // This should throw an IllegalArgumentException Loading Loading
tests/robotests/src/com/android/customization/model/picker/settings/data/repository/ColorContrastSectionRepositoryTest.kt +10 −5 Original line number Diff line number Diff line Loading @@ -24,8 +24,10 @@ import dagger.hilt.android.testing.HiltAndroidRule 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 Loading Loading @@ -53,18 +55,21 @@ class ColorContrastSectionRepositoryTest { @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() } }
tests/robotests/src/com/android/customization/model/picker/settings/domain/interactor/ColorContrastSectionInteractorTest.kt +4 −4 Original line number Diff line number Diff line Loading @@ -19,12 +19,12 @@ package com.android.customization.model.picker.settings.domain.interactor 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 Loading @@ -46,12 +46,12 @@ class ColorContrastSectionInteractorTest { } @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) } }
tests/robotests/src/com/android/customization/model/picker/settings/ui/viewmodel/ColorContrastSectionViewModelTest.kt +5 −5 Original line number Diff line number Diff line Loading @@ -27,7 +27,7 @@ import dagger.hilt.android.testing.HiltAndroidTest 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 Loading @@ -52,7 +52,7 @@ class ColorContrastSectionViewModelTest { } @Test fun summaryEmitsCorrectDataValueForStandard() = runBlockingTest { fun summaryEmitsCorrectDataValueForStandard() = runTest { uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.STANDARD.value) val expected = ColorContrastSectionDataViewModel( Loading @@ -66,7 +66,7 @@ class ColorContrastSectionViewModelTest { } @Test fun summaryEmitsCorrectDataValueForMedium() = runBlockingTest { fun summaryEmitsCorrectDataValueForMedium() = runTest { uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.MEDIUM.value) val expected = ColorContrastSectionDataViewModel( Loading @@ -80,7 +80,7 @@ class ColorContrastSectionViewModelTest { } @Test fun summaryEmitsCorrectDataValueForHigh() = runBlockingTest { fun summaryEmitsCorrectDataValueForHigh() = runTest { uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.HIGH.value) val expected = ColorContrastSectionDataViewModel( Loading @@ -94,7 +94,7 @@ class ColorContrastSectionViewModelTest { } @Test(expected = IllegalArgumentException::class) fun summaryThrowsIllegalArgumentExceptionForInvalidValue() = runBlockingTest { fun summaryThrowsIllegalArgumentExceptionForInvalidValue() = runTest { uiModeManager.setContrast(999f) viewModel.summary.collect() // This should throw an IllegalArgumentException Loading