diff --git a/composeApp/src/commonMain/kotlin/org/ooni/probe/background/RunBackgroundTask.kt b/composeApp/src/commonMain/kotlin/org/ooni/probe/background/RunBackgroundTask.kt index 8a30e959..734422a4 100644 --- a/composeApp/src/commonMain/kotlin/org/ooni/probe/background/RunBackgroundTask.kt +++ b/composeApp/src/commonMain/kotlin/org/ooni/probe/background/RunBackgroundTask.kt @@ -111,7 +111,7 @@ class RunBackgroundTask( return } - if (getNetworkType() == NetworkType.VPN) { + if (getNetworkType() == NetworkType.VPN && spec == null) { Logger.i("Skipping auto-run tests: VPN enabled") return } diff --git a/composeApp/src/commonTest/kotlin/org/ooni/probe/background/RunBackgroundTaskTest.kt b/composeApp/src/commonTest/kotlin/org/ooni/probe/background/RunBackgroundTaskTest.kt new file mode 100644 index 00000000..c43dfc99 --- /dev/null +++ b/composeApp/src/commonTest/kotlin/org/ooni/probe/background/RunBackgroundTaskTest.kt @@ -0,0 +1,101 @@ +package org.ooni.probe.background + +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.emptyFlow +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import org.ooni.engine.models.NetworkType +import org.ooni.engine.models.TaskOrigin +import org.ooni.probe.data.models.ResultModel +import org.ooni.probe.data.models.RunBackgroundState +import org.ooni.probe.data.models.RunSpecification +import org.ooni.probe.data.models.SettingsKey +import org.ooni.probe.domain.UploadMissingMeasurements +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class RunBackgroundTaskTest { + @Test + fun skipAutoRunIfVpnIsEnabled() = + runTest { + var wasRunDescriptorsCalled = false + val state = MutableStateFlow(RunBackgroundState.Idle()) + val subject = buildSubject( + getNetworkType = { NetworkType.VPN }, + getRunBackgroundState = { state }, + runDescriptors = { + wasRunDescriptorsCalled = true + state.value = RunBackgroundState.RunningTests() + delay(100) + state.value = RunBackgroundState.Idle() + }, + ) + + subject(null).collect() + + assertFalse(wasRunDescriptorsCalled) + } + + @Test + fun runManualRunIfVpnIsEnabled() = + runTest { + var wasRunDescriptorsCalled = false + val state = MutableStateFlow(RunBackgroundState.Idle()) + val subject = buildSubject( + getNetworkType = { NetworkType.VPN }, + getRunBackgroundState = { state }, + runDescriptors = { + wasRunDescriptorsCalled = true + state.value = RunBackgroundState.RunningTests() + delay(100) + state.value = RunBackgroundState.Idle() + }, + ) + + subject( + RunSpecification( + tests = emptyList(), + taskOrigin = TaskOrigin.OoniRun, + isRerun = false, + ), + ).collect() + + assertTrue(wasRunDescriptorsCalled) + } + + private fun buildSubject( + getPreferenceValueByKey: (SettingsKey) -> Flow = { flowOf(true) }, + uploadMissingMeasurements: (ResultModel.Id?) -> Flow = { emptyFlow() }, + checkSkipAutoRunNotUploadedLimit: () -> Flow = { flowOf(false) }, + getNetworkType: () -> NetworkType = { NetworkType.Wifi }, + getAutoRunSpecification: suspend () -> RunSpecification = { + RunSpecification( + tests = emptyList(), + taskOrigin = TaskOrigin.AutoRun, + isRerun = false, + ) + }, + runDescriptors: suspend (RunSpecification) -> Unit = {}, + setRunBackgroundState: ((RunBackgroundState) -> RunBackgroundState) -> Unit = {}, + getRunBackgroundState: () -> Flow = { flowOf(RunBackgroundState.Idle()) }, + addRunCancelListener: (() -> Unit) -> Unit = {}, + clearRunCancelListeners: () -> Unit = {}, + getLatestResult: () -> Flow = { flowOf(null) }, + ) = RunBackgroundTask( + getPreferenceValueByKey = getPreferenceValueByKey, + uploadMissingMeasurements = uploadMissingMeasurements, + checkSkipAutoRunNotUploadedLimit = checkSkipAutoRunNotUploadedLimit, + getNetworkType = getNetworkType, + getAutoRunSpecification = getAutoRunSpecification, + runDescriptors = runDescriptors, + setRunBackgroundState = setRunBackgroundState, + getRunBackgroundState = getRunBackgroundState, + addRunCancelListener = addRunCancelListener, + clearRunCancelListeners = clearRunCancelListeners, + getLatestResult = getLatestResult, + ) +}