diff options
| author | Harsh Shandilya <me@msfjarvis.dev> | 2021-12-09 10:07:54 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-09 04:37:54 +0000 |
| commit | 8db0b67ce9ba4a5e56c04c1bea3a738eacb176cf (patch) | |
| tree | 46700b7a3da32065e073bffbf22765c7e2f30d32 /coroutine-utils-testing/src/main | |
| parent | 933558caf8266677dc26497d7c7b930254f4fb07 (diff) | |
| download | HardenedPass-8db0b67ce9ba4a5e56c04c1bea3a738eacb176cf.tar.gz HardenedPass-8db0b67ce9ba4a5e56c04c1bea3a738eacb176cf.zip | |
Refactor coroutine testing setup (#1583)
* coroutine-utils: init
* coroutine-utils-testing: init
* format-common: switch over to using DispatcherProvider
* Convert Binds method to an extension function
* Add Dispatcher module
Diffstat (limited to 'coroutine-utils-testing/src/main')
| -rw-r--r-- | coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt b/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt new file mode 100644 index 00000000..fa4a2d41 --- /dev/null +++ b/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt @@ -0,0 +1,45 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.test + +import dev.msfjarvis.aps.util.coroutines.DispatcherProvider +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestCoroutineScheduler +import kotlinx.coroutines.test.TestDispatcher +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import org.junit.rules.TestWatcher +import org.junit.runner.Description + +/** + * JUnit [TestWatcher] to correctly handle setting and resetting a given [testDispatcher] for tests. + */ +@ExperimentalCoroutinesApi +public class CoroutineTestRule( + public val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(TestCoroutineScheduler()), +) : TestWatcher() { + + public val testDispatcherProvider: DispatcherProvider = + object : DispatcherProvider { + override fun default(): CoroutineDispatcher = testDispatcher + override fun io(): CoroutineDispatcher = testDispatcher + override fun main(): CoroutineDispatcher = testDispatcher + override fun unconfined(): CoroutineDispatcher = testDispatcher + } + + override fun starting(description: Description?) { + super.starting(description) + Dispatchers.setMain(testDispatcher) + } + + override fun finished(description: Description?) { + super.finished(description) + Dispatchers.resetMain() + } +} |
