Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aea650e5 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Removes use of deprecated runTestBlocking" into main

parents 79fa4091 4777c893
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -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
@@ -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()
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -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
@@ -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)
    }
}
+5 −5
Original line number Diff line number Diff line
@@ -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
@@ -52,7 +52,7 @@ class ColorContrastSectionViewModelTest {
    }

    @Test
    fun summaryEmitsCorrectDataValueForStandard() = runBlockingTest {
    fun summaryEmitsCorrectDataValueForStandard() = runTest {
        uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.STANDARD.value)
        val expected =
            ColorContrastSectionDataViewModel(
@@ -66,7 +66,7 @@ class ColorContrastSectionViewModelTest {
    }

    @Test
    fun summaryEmitsCorrectDataValueForMedium() = runBlockingTest {
    fun summaryEmitsCorrectDataValueForMedium() = runTest {
        uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.MEDIUM.value)
        val expected =
            ColorContrastSectionDataViewModel(
@@ -80,7 +80,7 @@ class ColorContrastSectionViewModelTest {
    }

    @Test
    fun summaryEmitsCorrectDataValueForHigh() = runBlockingTest {
    fun summaryEmitsCorrectDataValueForHigh() = runTest {
        uiModeManager.setContrast(ColorContrastSectionViewModel.ContrastValue.HIGH.value)
        val expected =
            ColorContrastSectionDataViewModel(
@@ -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