mirror of
https://github.com/bitfireAT/davx5-ose.git
synced 2025-12-23 23:17:50 -05:00
[DI] Make backing field of TestDispatcher + scheduler private
This commit is contained in:
@@ -12,9 +12,6 @@ import androidx.test.runner.AndroidJUnitRunner
|
||||
import at.bitfire.davdroid.di.TestCoroutineDispatchersModule
|
||||
import at.bitfire.davdroid.sync.SyncAdapterService
|
||||
import dagger.hilt.android.testing.HiltTestApplication
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.setMain
|
||||
|
||||
@Suppress("unused")
|
||||
class HiltTestRunner : AndroidJUnitRunner() {
|
||||
@@ -22,7 +19,6 @@ class HiltTestRunner : AndroidJUnitRunner() {
|
||||
override fun newApplication(cl: ClassLoader, name: String, context: Context): Application =
|
||||
super.newApplication(cl, HiltTestApplication::class.java.name, context)
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override fun onCreate(arguments: Bundle?) {
|
||||
super.onCreate(arguments)
|
||||
|
||||
@@ -34,7 +30,7 @@ class HiltTestRunner : AndroidJUnitRunner() {
|
||||
SyncAdapterService.syncActive.set(false)
|
||||
|
||||
// set main dispatcher for tests (especially runTest)
|
||||
Dispatchers.setMain(TestCoroutineDispatchersModule.standardTestDispatcher)
|
||||
TestCoroutineDispatchersModule.initMainDispatcher()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,8 +10,10 @@ import dagger.Provides
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import dagger.hilt.testing.TestInstallIn
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||
import kotlinx.coroutines.test.TestCoroutineScheduler
|
||||
import kotlinx.coroutines.test.setMain
|
||||
|
||||
/**
|
||||
* Provides test dispatchers to be injected instead of the normal ones.
|
||||
@@ -26,8 +28,7 @@ import kotlinx.coroutines.test.TestCoroutineScheduler
|
||||
)
|
||||
object TestCoroutineDispatchersModule {
|
||||
|
||||
val scheduler = TestCoroutineScheduler()
|
||||
val standardTestDispatcher = StandardTestDispatcher(scheduler)
|
||||
private val standardTestDispatcher = StandardTestDispatcher()
|
||||
|
||||
@Provides
|
||||
@DefaultDispatcher
|
||||
@@ -45,4 +46,14 @@ object TestCoroutineDispatchersModule {
|
||||
@SyncDispatcher
|
||||
fun syncDispatcher(): CoroutineDispatcher = standardTestDispatcher
|
||||
|
||||
/**
|
||||
* Sets the [standardTestDispatcher] as [Dispatchers.Main] so that test dispatchers
|
||||
* created in the future use the same scheduler. See [StandardTestDispatcher] docs
|
||||
* for more information.
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
fun initMainDispatcher() {
|
||||
Dispatchers.setMain(standardTestDispatcher)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user