zircles-android/feedback/src/test/java/cash/z/ecc/android/feedback/CoroutineTestRule.kt

31 lines
1008 B
Kotlin

package cash.z.ecc.android.feedback
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
class CoroutinesTestRule(
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {
lateinit var testScope: TestCoroutineScope
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
testScope = TestCoroutineScope()
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
if (testScope.coroutineContext[Job]?.isActive == true) testScope.cancel()
}
}