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

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

Merge "Cleans up some uses of test scope and dispatcher" into main

parents 2fc2978b 47ee1fb0
Loading
Loading
Loading
Loading
+19 −15
Original line number Diff line number Diff line
@@ -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
@@ -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() {
@@ -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)
            }

+7 −0
Original line number Diff line number Diff line
@@ -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
@@ -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)
    }